diff --git a/README.md b/README.md index 8f9b9b1..9e76f1e 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [isAlpha](#isalpha) * [isAlphanumeric](#isalphanumeric) * [isBlank](#isblank) + * [isJson](#isjson) * [isHexadecimal](#ishexadecimal) * [isLowerCase](#islowercase) * [isUpperCase](#isuppercase) @@ -338,6 +339,19 @@ S::create("\n\t \v\f")->isBlank(); S::isBlank("\n\t \v\f"); // 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 +``` + #### isHexadecimal $stringy->isHexadecimal() diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index ab75aef..50ebe41 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -624,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. * diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 256d07a..cabee2d 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -994,6 +994,19 @@ 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() + { + json_decode($this->str); + + return json_last_error() == JSON_ERROR_NONE && $this->matchesPattern("^\W*[\[{].+$"); + } + + /** * Returns true if the string contains only lower case chars, false otherwise. * diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index f300a08..9fa2d12 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -766,6 +766,33 @@ abstract 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(false, '["foo": "bar"]'), + array(true, '["foo"]'), + array(false, '{"foo"}'), + array(false, '{"foo": "bar"]'), + array(true, ' { "foo" : "bar" } '), + array(false, '123', 'UTF-8'), + array(true, '{"foo": "bar"}', 'UTF-8'), + array(false, '{"foo":"bar",}', 'UTF-8'), + array(false, '{"foo"}', 'UTF-8'), + array(false, '["foo": "bar"]', 'UTF-8'), + array(true, '["foo"]', 'UTF-8'), + array(false, '{"foo"}', 'UTF-8'), + array(false, '{"foo": "bar"]', 'UTF-8'), + array(true, ' { "foo" : "bar" } ', 'UTF-8'), + ); + + return $testData; + } + public function stringsForIsLowerCase() { $testData = array( diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 7ceae44..8c15210 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -461,6 +461,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 diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index d6ab75b..bdab220 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -538,6 +538,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 */