From fb32da1428207280e7b4abc68f96a8250c10208c Mon Sep 17 00:00:00 2001 From: ecrmnn Date: Wed, 25 May 2022 09:53:08 +0200 Subject: [PATCH 1/4] Adds Str::isJson() --- src/Illuminate/Support/Str.php | 22 ++++++++++++++++++++++ src/Illuminate/Support/Stringable.php | 10 ++++++++++ tests/Support/SupportStrTest.php | 18 ++++++++++++++++++ tests/Support/SupportStringableTest.php | 18 ++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 28b29f1e9628..25908d513bb6 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -3,6 +3,7 @@ namespace Illuminate\Support; use Illuminate\Support\Traits\Macroable; +use JsonException; use League\CommonMark\GithubFlavoredMarkdownConverter; use Ramsey\Uuid\Codec\TimestampFirstCombCodec; use Ramsey\Uuid\Generator\CombGenerator; @@ -385,6 +386,27 @@ public static function isUuid($value) return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0; } + /** + * Determine if a given string is valid JSON. + * + * @param string $value + * @return bool + */ + public static function isJson($value) + { + if (! is_string($value)) { + return false; + } + + try { + json_decode($value, true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException) { + return false; + } + + return true; + } + /** * Convert a string to kebab case. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index ac1540b4e86f..5348be54c78b 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -307,6 +307,16 @@ public function isUuid() return Str::isUuid($this->value); } + /** + * Determine if a given string is valid JSON. + * + * @return bool + */ + public function isJson() + { + return Str::isJson($this->value); + } + /** * Determine if the given string is empty. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 0b897ec055b6..eae55a748559 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -406,6 +406,24 @@ public function testIsUuidWithInvalidUuid($uuid) $this->assertFalse(Str::isUuid($uuid)); } + public function testIsJson() + { + $this->assertTrue(Str::isJson('1')); + $this->assertTrue(Str::isJson('[1,2,3]')); + $this->assertTrue(Str::isJson('[1, 2, 3]')); + $this->assertTrue(Str::isJson('{"first": "John", "last": "Doe"}')); + $this->assertTrue(Str::isJson('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')); + + $this->assertFalse(Str::isJson('1,')); + $this->assertFalse(Str::isJson('[1,2,3')); + $this->assertFalse(Str::isJson('[1, 2 3]')); + $this->assertFalse(Str::isJson('{first: "John"}')); + $this->assertFalse(Str::isJson('[{first: "John"}, {first: "Jane"}]')); + $this->assertFalse(Str::isJson('')); + $this->assertFalse(Str::isJson(null)); + $this->assertFalse(Str::isJson([])); + } + public function testKebab() { $this->assertSame('laravel-php-framework', Str::kebab('LaravelPhpFramework')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 95da0705ee28..9efed389e5e7 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -38,6 +38,24 @@ public function testIsUuid() $this->assertFalse($this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->isUuid()); } + public function testIsJson() + { + $this->assertTrue($this->stringable('1')->isJson()); + $this->assertTrue($this->stringable('[1,2,3]')->isJson()); + $this->assertTrue($this->stringable('[1, 2, 3]')->isJson()); + $this->assertTrue($this->stringable('{"first": "John", "last": "Doe"}')->isJson()); + $this->assertTrue($this->stringable('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')->isJson()); + + $this->assertFalse($this->stringable('1,')->isJson()); + $this->assertFalse($this->stringable('[1,2,3')->isJson()); + $this->assertFalse($this->stringable('[1, 2 3]')->isJson()); + $this->assertFalse($this->stringable('{first: "John"}')->isJson()); + $this->assertFalse($this->stringable('[{first: "John"}, {first: "Jane"}]')->isJson()); + $this->assertFalse($this->stringable('')->isJson()); + $this->assertFalse($this->stringable(null)->isJson()); + $this->assertFalse($this->stringable([])->isJson()); + } + public function testIsEmpty() { $this->assertTrue($this->stringable('')->isEmpty()); From b5a1f23f4cdbc0387899ddd2c1dbc59b46020c63 Mon Sep 17 00:00:00 2001 From: ecrmnn Date: Wed, 25 May 2022 09:54:53 +0200 Subject: [PATCH 2/4] Removes unnecessary test --- tests/Support/SupportStringableTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 9efed389e5e7..d1ed24bec5be 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -53,7 +53,6 @@ public function testIsJson() $this->assertFalse($this->stringable('[{first: "John"}, {first: "Jane"}]')->isJson()); $this->assertFalse($this->stringable('')->isJson()); $this->assertFalse($this->stringable(null)->isJson()); - $this->assertFalse($this->stringable([])->isJson()); } public function testIsEmpty() From 2f1f44ea998e0c2b7e0d0ddea483f46184fa5738 Mon Sep 17 00:00:00 2001 From: ecrmnn Date: Fri, 27 May 2022 16:04:06 +0200 Subject: [PATCH 3/4] Fixes code style --- src/Illuminate/Support/Str.php | 2 +- tests/Support/SupportStrTest.php | 2 +- tests/Support/SupportStringableTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 25908d513bb6..6856df2f5a8c 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -397,7 +397,7 @@ public static function isJson($value) if (! is_string($value)) { return false; } - + try { json_decode($value, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException) { diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index eae55a748559..541c4c669d31 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -413,7 +413,7 @@ public function testIsJson() $this->assertTrue(Str::isJson('[1, 2, 3]')); $this->assertTrue(Str::isJson('{"first": "John", "last": "Doe"}')); $this->assertTrue(Str::isJson('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')); - + $this->assertFalse(Str::isJson('1,')); $this->assertFalse(Str::isJson('[1,2,3')); $this->assertFalse(Str::isJson('[1, 2 3]')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index d1ed24bec5be..325197b6d84e 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -45,7 +45,7 @@ public function testIsJson() $this->assertTrue($this->stringable('[1, 2, 3]')->isJson()); $this->assertTrue($this->stringable('{"first": "John", "last": "Doe"}')->isJson()); $this->assertTrue($this->stringable('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')->isJson()); - + $this->assertFalse($this->stringable('1,')->isJson()); $this->assertFalse($this->stringable('[1,2,3')->isJson()); $this->assertFalse($this->stringable('[1, 2 3]')->isJson()); From 84ecfb1ea0f3faf7f2505357d5abc76f889a3bf2 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 30 May 2022 06:33:03 -0700 Subject: [PATCH 4/4] formatting --- src/Illuminate/Support/Str.php | 24 ++++++++++++------------ src/Illuminate/Support/Stringable.php | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 6856df2f5a8c..2b13e1805f8a 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -372,39 +372,39 @@ public static function isAscii($value) } /** - * Determine if a given string is a valid UUID. + * Determine if a given string is valid JSON. * * @param string $value * @return bool */ - public static function isUuid($value) + public static function isJson($value) { if (! is_string($value)) { return false; } - return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0; + try { + json_decode($value, true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException) { + return false; + } + + return true; } /** - * Determine if a given string is valid JSON. + * Determine if a given string is a valid UUID. * * @param string $value * @return bool */ - public static function isJson($value) + public static function isUuid($value) { if (! is_string($value)) { return false; } - try { - json_decode($value, true, 512, JSON_THROW_ON_ERROR); - } catch (JsonException) { - return false; - } - - return true; + return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0; } /** diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 5348be54c78b..1ba00aa35bfb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -298,23 +298,23 @@ public function isAscii() } /** - * Determine if a given string is a valid UUID. + * Determine if a given string is valid JSON. * * @return bool */ - public function isUuid() + public function isJson() { - return Str::isUuid($this->value); + return Str::isJson($this->value); } /** - * Determine if a given string is valid JSON. + * Determine if a given string is a valid UUID. * * @return bool */ - public function isJson() + public function isUuid() { - return Str::isJson($this->value); + return Str::isUuid($this->value); } /**