mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-02 09:33:10 +02:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
545a5aec5f | ||
|
63298f36a8 | ||
|
a667c4aa6a | ||
|
cd5e897ac3 | ||
|
390671e131 | ||
|
0b76c56333 | ||
|
5f35226926 | ||
|
11f961cae6 | ||
|
660b1d6de8 | ||
|
3d81e2ef70 | ||
|
454e8e2f87 | ||
|
89c292c041 | ||
|
a1d9787309 | ||
|
f1abc38c17 | ||
|
7a64dad935 | ||
|
9d4ca96528 |
@@ -1,3 +1,11 @@
|
||||
### 1.1.0 (2013-08-31)
|
||||
|
||||
* Fix for collapseWhitespace()
|
||||
* Added isHexadecimal()
|
||||
* Added constructor to Stringy\Stringy
|
||||
* Added isSerialized()
|
||||
* Added isJson()
|
||||
|
||||
### 1.0.0 (2013-08-1)
|
||||
|
||||
* 1.0.0 release
|
||||
|
58
README.md
58
README.md
@@ -3,7 +3,6 @@
|
||||
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
||||
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
[](https://packagist.org/packages/danielstjules/stringy)
|
||||
|
||||
* [Requiring/Loading](#requiringloading)
|
||||
* [OO and Procedural](#oo-and-procedural)
|
||||
@@ -24,7 +23,10 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
||||
* [isAlpha](#isalpha)
|
||||
* [isAlphanumeric](#isalphanumeric)
|
||||
* [isBlank](#isblank)
|
||||
* [isHexadecimal](#ishexadecimal)
|
||||
* [isJson](#isjson)
|
||||
* [isLowerCase](#islowercase)
|
||||
* [isSerialized](#isserialized)
|
||||
* [isUpperCase](#isuppercase)
|
||||
* [last](#last)
|
||||
* [length](#length)
|
||||
@@ -98,7 +100,7 @@ of the former is the following:
|
||||
|
||||
```php
|
||||
use Stringy\Stringy as S;
|
||||
echo S::create("Fòô Bàř", 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
`Stringy\Stringy` contains a __toString() method, which returns the current
|
||||
@@ -109,8 +111,8 @@ Using the static wrapper, an alternative is the following:
|
||||
|
||||
```php
|
||||
use Stringy\StaticStringy as S;
|
||||
$string = S::collapseWhitespace("Fòô Bàř", 'UTF-8');
|
||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
|
||||
$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
|
||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
## Methods
|
||||
@@ -152,10 +154,11 @@ S::camelize('Camel-Case'); // 'camelCase'
|
||||
|
||||
$stringy->collapseWhitespace()
|
||||
|
||||
S::collapseWhitespace(string $str)
|
||||
S::collapseWhitespace(string $str [, string $encoding ])
|
||||
|
||||
Trims the string and replaces consecutive whitespace characters with a
|
||||
single space. This includes tabs and newline characters.
|
||||
single space. This includes tabs and newline characters, as well as
|
||||
multibyte whitespace such as the thin space and ideographic space.
|
||||
|
||||
```php
|
||||
S::create(' Ο συγγραφέας ')->collapseWhitespace();
|
||||
@@ -337,6 +340,32 @@ S::create("\n\t \v\f")->isBlank();
|
||||
S::isBlank("\n\t \v\f"); // true
|
||||
```
|
||||
|
||||
#### isHexadecimal
|
||||
|
||||
$stringy->isHexadecimal()
|
||||
|
||||
S::isHexadecimal(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('A102F')->isHexadecimal();
|
||||
S::isHexadecimal('A102F'); // true
|
||||
```
|
||||
|
||||
#### isJson
|
||||
|
||||
$stringy->isJson()
|
||||
|
||||
S::isJson(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string is JSON, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('{"foo":"bar"}')->isJson();
|
||||
S::isJson('{"foo":"bar"}'); // true
|
||||
```
|
||||
|
||||
#### isLowerCase
|
||||
|
||||
$stringy->isLowerCase()
|
||||
@@ -350,6 +379,19 @@ S::create('fòô bàř', 'UTF-8')->isLowerCase();
|
||||
S::isLowerCase('fòô bàř', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### isSerialized
|
||||
|
||||
$stringy->isSerialized()
|
||||
|
||||
S::isSerialized(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string is serialized, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8')->isSerialized();
|
||||
S::isSerialized('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### isUpperCase
|
||||
|
||||
$stringy->isUpperCase()
|
||||
@@ -359,8 +401,8 @@ S::isUpperCase(string $str [, string $encoding ])
|
||||
Returns true if the string contains only upper case chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('FÒÔBÀŘ',, 'UTF-8')->isUpperCase();
|
||||
S::isUpperCase('FÒÔBÀŘ',, 'UTF-8'); // true
|
||||
S::create('FÒÔBÀŘ', 'UTF-8')->isUpperCase();
|
||||
S::isUpperCase('FÒÔBÀŘ', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### last
|
||||
|
@@ -142,14 +142,15 @@ class StaticStringy
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This includes tabs and newline characters.
|
||||
* single space. This includes tabs and newline characters, as well as
|
||||
* multibyte whitespace such as the thin space and ideographic space.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
*/
|
||||
public static function collapseWhitespace($str)
|
||||
public static function collapseWhitespace($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str)->collapseWhitespace()->str;
|
||||
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -623,6 +624,18 @@ class StaticStringy
|
||||
return Stringy::create($str, $encoding)->isBlank();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is JSON, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str is JSON
|
||||
*/
|
||||
public static function isJson($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only lower case chars, false otherwise.
|
||||
*
|
||||
@@ -635,6 +648,18 @@ class StaticStringy
|
||||
return Stringy::create($str, $encoding)->isLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is serialized, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str is serialized
|
||||
*/
|
||||
public static function isSerialized($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isSerialized();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only upper case chars, false otherwise.
|
||||
*
|
||||
@@ -647,6 +672,18 @@ class StaticStringy
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only hexadecimal characters
|
||||
*/
|
||||
public static function isHexadecimal($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isHexadecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of occurrences of $substring in the given string.
|
||||
* An alias for mb_substr_count()
|
||||
|
@@ -8,6 +8,20 @@ class Stringy
|
||||
|
||||
public $encoding;
|
||||
|
||||
/**
|
||||
* Initializes a Stringy object and assigns both str and encoding properties
|
||||
* the supplied values. If $encoding is not specified, it defaults to
|
||||
* mb_internal_encoding().
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
*/
|
||||
public function __construct($str, $encoding = null)
|
||||
{
|
||||
$this->str = $str;
|
||||
$this->encoding = $encoding ?: mb_internal_encoding();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Stringy object and assigns both str and encoding properties
|
||||
* the supplied values. If $encoding is not specified, it defaults to
|
||||
@@ -19,13 +33,7 @@ class Stringy
|
||||
*/
|
||||
public static function create($str, $encoding = null)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$stringyObj = new self();
|
||||
$stringyObj->str = $str;
|
||||
$stringyObj->encoding = $encoding;
|
||||
|
||||
return $stringyObj;
|
||||
return new self($str, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,16 +257,21 @@ class Stringy
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This includes tabs and newline characters.
|
||||
* single space. This includes tabs and newline characters, as well as
|
||||
* multibyte whitespace such as the thin space and ideographic space.
|
||||
*
|
||||
* @return Stringy Object with a trimmed $str and condensed whitespace
|
||||
*/
|
||||
public function collapseWhitespace()
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$stringy->str = preg_replace('/\s+/u', ' ', $stringy->trim());
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
return $stringy;
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$stringy->str = mb_ereg_replace('[[:space:]]+', ' ', $stringy);
|
||||
mb_regex_encoding($regexEncoding);
|
||||
|
||||
return $stringy->trim();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -336,9 +349,9 @@ class Stringy
|
||||
* 'right', 'both') is 'right'. Throws an InvalidArgumentException if
|
||||
* $padType isn't one of those 3 values.
|
||||
*
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @return Stringy Object with a padded $str
|
||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
@@ -969,6 +982,16 @@ class Stringy
|
||||
return $this->matchesPattern('^([[:alnum:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str contains only hexadecimal chars
|
||||
*/
|
||||
public function isHexadecimal()
|
||||
{
|
||||
return $this->matchesPattern('^([[:xdigit:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false otherwise.
|
||||
*
|
||||
@@ -979,6 +1002,21 @@ class Stringy
|
||||
return $this->matchesPattern('^([[:space:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is JSON, false otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str is JSON
|
||||
*/
|
||||
public function isJson()
|
||||
{
|
||||
if (!$this->endsWith('}') && !$this->endsWith(']')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !is_null(json_decode($this->str));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only lower case chars, false otherwise.
|
||||
*
|
||||
@@ -999,6 +1037,16 @@ class Stringy
|
||||
return $this->matchesPattern('^([[:upper:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is serialized, false otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str is serialized
|
||||
*/
|
||||
public function isSerialized()
|
||||
{
|
||||
return $this->str === 'b:0;' || @unserialize($this->str) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of occurrences of $substring in the given string.
|
||||
* An alias for mb_substr_count()
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class CommonTest extends PHPUnit_Framework_TestCase
|
||||
abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function stringsForUpperCaseFirst()
|
||||
{
|
||||
@@ -173,6 +173,8 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('test string', 'test string'),
|
||||
array('Ο συγγραφέας', ' Ο συγγραφέας '),
|
||||
array('123', ' 123 '),
|
||||
array('1 2 3', ' 1 2 3 ', 'UTF-8'), // ideographic spaces
|
||||
array('', ' ', 'UTF-8'), // thin space and space
|
||||
array('', ' '),
|
||||
array('', ''),
|
||||
);
|
||||
@@ -764,6 +766,28 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsJson()
|
||||
{
|
||||
$testData = array(
|
||||
array(false, ''),
|
||||
array(false, '123'),
|
||||
array(true, '{"foo": "bar"}'),
|
||||
array(false, '{"foo":"bar",}'),
|
||||
array(false, '{"foo"}'),
|
||||
array(true, '["foo"]'),
|
||||
array(false, '{"foo": "bar"]'),
|
||||
array(false, '123', 'UTF-8'),
|
||||
array(true, '{"fòô": "bàř"}', 'UTF-8'),
|
||||
array(false, '{"fòô":"bàř",}', 'UTF-8'),
|
||||
array(false, '{"fòô"}', 'UTF-8'),
|
||||
array(false, '["fòô": "bàř"]', 'UTF-8'),
|
||||
array(true, '["fòô"]', 'UTF-8'),
|
||||
array(false, '{"fòô": "bàř"]', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsLowerCase()
|
||||
{
|
||||
$testData = array(
|
||||
@@ -780,6 +804,21 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsSerialized()
|
||||
{
|
||||
$testData = array(
|
||||
array(false, ''),
|
||||
array(true, 'a:1:{s:3:"foo";s:3:"bar";}'),
|
||||
array(false, 'a:1:{s:3:"foo";s:3:"bar"}'),
|
||||
array(true, serialize(array('foo' => 'bar'))),
|
||||
array(true, 'a:1:{s:5:"fòô";s:5:"bàř";}', 'UTF-8'),
|
||||
array(false, 'a:1:{s:5:"fòô";s:5:"bàř"}', 'UTF-8'),
|
||||
array(true, serialize(array('fòô' => 'bár')), 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsUpperCase()
|
||||
{
|
||||
$testData = array(
|
||||
@@ -796,6 +835,27 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsHexadecimal()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, ''),
|
||||
array(true, 'abcdef'),
|
||||
array(true, 'ABCDEF'),
|
||||
array(true, '0123456789'),
|
||||
array(true, '0123456789AbCdEf'),
|
||||
array(false, '0123456789x'),
|
||||
array(false, 'ABCDEFx'),
|
||||
array(true, 'abcdef', 'UTF-8'),
|
||||
array(true, 'ABCDEF', 'UTF-8'),
|
||||
array(true, '0123456789', 'UTF-8'),
|
||||
array(true, '0123456789AbCdEf', 'UTF-8'),
|
||||
array(false, '0123456789x', 'UTF-8'),
|
||||
array(false, 'ABCDEFx', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForCount()
|
||||
{
|
||||
$testData = array(
|
||||
@@ -831,10 +891,4 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
// A test is required so as not to throw an error
|
||||
// This is a lot cleaner than using PHPUnit's mocks to spy
|
||||
public function test() {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
@@ -111,9 +111,9 @@ class StaticStringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::collapseWhitespace($str);
|
||||
$result = S::collapseWhitespace($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
@@ -292,10 +292,20 @@ class StaticStringyTestCase extends CommonTest
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$result = S::shuffle($str, $encoding);
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
$this->assertEquals(mb_strlen($str, $encoding),
|
||||
mb_strlen($result, $encoding));
|
||||
|
||||
// We'll make sure that the chars are present after shuffle
|
||||
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
|
||||
$char = mb_substr($str, $i, 1, $encoding);
|
||||
$countBefore = mb_substr_count($str, $char, $encoding);
|
||||
$countAfter = mb_substr_count($result, $char, $encoding);
|
||||
$this->assertEquals($countBefore, $countAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -461,6 +471,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsJson
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isJson($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
@@ -472,6 +492,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsSerialized
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isSerialized($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
*/
|
||||
@@ -482,6 +512,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isHexadecimal($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
|
@@ -7,6 +7,14 @@ use Stringy\Stringy as S;
|
||||
|
||||
class StringyTestCase extends CommonTest
|
||||
{
|
||||
public function testConstruct()
|
||||
{
|
||||
$stringy = new S('foo bar', 'UTF-8');
|
||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||
$this->assertEquals('foo bar', $stringy->str);
|
||||
$this->assertEquals('UTF-8', $stringy->encoding);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$stringy = S::create('foo bar', 'UTF-8');
|
||||
@@ -145,9 +153,9 @@ class StringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->collapseWhitespace();
|
||||
$this->assertInstanceOf('Stringy\Stringy', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
@@ -347,12 +355,22 @@ class StringyTestCase extends CommonTest
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$stringy = S::create($str, $encoding);
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$result = $stringy->shuffle();
|
||||
|
||||
$this->assertInstanceOf('Stringy\Stringy', $result);
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
$this->assertEquals($str, $stringy);
|
||||
$this->assertEquals(mb_strlen($str, $encoding),
|
||||
mb_strlen($result, $encoding));
|
||||
|
||||
// We'll make sure that the chars are present after shuffle
|
||||
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
|
||||
$char = mb_substr($str, $i, 1, $encoding);
|
||||
$countBefore = mb_substr_count($str, $char, $encoding);
|
||||
$countAfter = mb_substr_count($result, $char, $encoding);
|
||||
$this->assertEquals($countBefore, $countAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -538,6 +556,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsJson
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isJson();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
*/
|
||||
@@ -550,6 +580,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsSerialized
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isSerialized();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
*/
|
||||
@@ -562,6 +604,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isHexadecimal();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
|
Reference in New Issue
Block a user