mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 16:24:00 +02:00
Fix and merge branch 'isJson'
This commit is contained in:
14
README.md
14
README.md
@@ -24,6 +24,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [isAlphanumeric](#isalphanumeric)
|
* [isAlphanumeric](#isalphanumeric)
|
||||||
* [isBlank](#isblank)
|
* [isBlank](#isblank)
|
||||||
* [isHexadecimal](#ishexadecimal)
|
* [isHexadecimal](#ishexadecimal)
|
||||||
|
* [isJson](#isjson)
|
||||||
* [isLowerCase](#islowercase)
|
* [isLowerCase](#islowercase)
|
||||||
* [isSerialized](#isserialized)
|
* [isSerialized](#isserialized)
|
||||||
* [isUpperCase](#isuppercase)
|
* [isUpperCase](#isuppercase)
|
||||||
@@ -352,6 +353,19 @@ S::create('A102F')->isHexadecimal();
|
|||||||
S::isHexadecimal('A102F'); // true
|
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
|
#### isLowerCase
|
||||||
|
|
||||||
$stringy->isLowerCase()
|
$stringy->isLowerCase()
|
||||||
|
@@ -624,6 +624,18 @@ class StaticStringy
|
|||||||
return Stringy::create($str, $encoding)->isBlank();
|
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.
|
* Returns true if the string contains only lower case chars, false otherwise.
|
||||||
*
|
*
|
||||||
|
@@ -1002,6 +1002,21 @@ class Stringy
|
|||||||
return $this->matchesPattern('^([[:space:]])*$');
|
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.
|
* Returns true if the string contains only lower case chars, false otherwise.
|
||||||
*
|
*
|
||||||
|
@@ -766,6 +766,28 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return $testData;
|
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()
|
public function stringsForIsLowerCase()
|
||||||
{
|
{
|
||||||
$testData = array(
|
$testData = array(
|
||||||
|
@@ -472,6 +472,16 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($expected, $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
|
* @dataProvider stringsForIsLowerCase
|
||||||
*/
|
*/
|
||||||
|
@@ -556,6 +556,18 @@ class StringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($str, $stringy);
|
$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
|
* @dataProvider stringsForIsLowerCase
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user