mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 07:44:12 +02:00
function isSerialized(), with tests
This commit is contained in:
14
README.md
14
README.md
@@ -25,6 +25,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [isBlank](#isblank)
|
* [isBlank](#isblank)
|
||||||
* [isHexadecimal](#ishexadecimal)
|
* [isHexadecimal](#ishexadecimal)
|
||||||
* [isLowerCase](#islowercase)
|
* [isLowerCase](#islowercase)
|
||||||
|
* [isSerialized](#isserialized)
|
||||||
* [isUpperCase](#isuppercase)
|
* [isUpperCase](#isuppercase)
|
||||||
* [last](#last)
|
* [last](#last)
|
||||||
* [length](#length)
|
* [length](#length)
|
||||||
@@ -364,6 +365,19 @@ S::create('fòô bàř', 'UTF-8')->isLowerCase();
|
|||||||
S::isLowerCase('fòô bàř', 'UTF-8'); // true
|
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
|
#### isUpperCase
|
||||||
|
|
||||||
$stringy->isUpperCase()
|
$stringy->isUpperCase()
|
||||||
|
@@ -636,6 +636,18 @@ class StaticStringy
|
|||||||
return Stringy::create($str, $encoding)->isLowerCase();
|
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.
|
* Returns true if the string contains only upper case chars, false otherwise.
|
||||||
*
|
*
|
||||||
|
@@ -1014,6 +1014,16 @@ class Stringy
|
|||||||
return $this->matchesPattern('^([[:upper:]])*$');
|
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.
|
* Returns the number of occurrences of $substring in the given string.
|
||||||
* An alias for mb_substr_count()
|
* An alias for mb_substr_count()
|
||||||
|
@@ -782,6 +782,21 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return $testData;
|
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(false)),
|
||||||
|
array(true, 'a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'),
|
||||||
|
array(false, 'a:1:{s:3:"foo";s:3:"bar"}', 'UTF-8'),
|
||||||
|
array(true, serialize(false), 'UTF-8'),
|
||||||
|
);
|
||||||
|
|
||||||
|
return $testData;
|
||||||
|
}
|
||||||
|
|
||||||
public function stringsForIsUpperCase()
|
public function stringsForIsUpperCase()
|
||||||
{
|
{
|
||||||
$testData = array(
|
$testData = array(
|
||||||
|
@@ -472,6 +472,16 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($expected, $result);
|
$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
|
* @dataProvider stringsForIsUpperCase
|
||||||
*/
|
*/
|
||||||
|
@@ -550,6 +550,18 @@ class StringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($str, $stringy);
|
$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
|
* @dataProvider stringsForIsUpperCase
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user