From 05a07d42eea829571d5c0f57f4ebd4028d9d5ab5 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sun, 20 Dec 2020 18:40:45 +0800 Subject: [PATCH 1/3] [9.x] Cast Stringable to string when used with json_encode(). Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Support/Stringable.php | 13 ++++++++++++- tests/Support/SupportStringableTest.php | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index c24d19e6a923..79b1a2eccc97 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -4,9 +4,10 @@ use Closure; use Illuminate\Support\Traits\Macroable; +use JsonSerializable; use Symfony\Component\VarDumper\VarDumper; -class Stringable +class Stringable implements JsonSerializable { use Macroable; @@ -733,6 +734,16 @@ public function __get($key) return $this->{$key}(); } + /** + * Convert the object into something JSON serializable. + * + * @return array + */ + public function jsonSerialize() + { + return $this->__toString(); + } + /** * Get the raw string value. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 1986d050de9c..930d99ac4aef 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -545,4 +545,11 @@ public function testChunk() $this->assertInstanceOf(Collection::class, $chunks); $this->assertSame(['foo', 'bar', 'baz'], $chunks->all()); } + + public function testJsonSerializable() + { + $this->assertSame('"laravel-php-framework"', json_encode($this->stringable('LaravelPhpFramework')->kebab())); + $this->assertSame('["laravel-php-framework"]', json_encode([$this->stringable('LaravelPhpFramework')->kebab()])); + $this->assertSame('{"title":"laravel-php-framework"}', json_encode(['title' => $this->stringable('LaravelPhpFramework')->kebab()])); + } } From 927337c188050c50799159fd2bcbc9198729d5f5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 20 Dec 2020 16:11:12 -0600 Subject: [PATCH 2/3] Update src/Illuminate/Support/Stringable.php Co-authored-by: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> --- src/Illuminate/Support/Stringable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 79b1a2eccc97..654b61ace369 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -737,7 +737,7 @@ public function __get($key) /** * Convert the object into something JSON serializable. * - * @return array + * @return string */ public function jsonSerialize() { From 66bd5f231293b59c249d98505223752e666af9f6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 20 Dec 2020 16:12:47 -0600 Subject: [PATCH 3/3] Update Stringable.php --- src/Illuminate/Support/Stringable.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 654b61ace369..017917baa097 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -724,24 +724,24 @@ public function dd() } /** - * Proxy dynamic properties onto methods. + * Convert the object into something JSON serializable. * - * @param string $key - * @return mixed + * @return string */ - public function __get($key) + public function jsonSerialize() { - return $this->{$key}(); + return $this->__toString(); } /** - * Convert the object into something JSON serializable. + * Proxy dynamic properties onto methods. * - * @return string + * @param string $key + * @return mixed */ - public function jsonSerialize() + public function __get($key) { - return $this->__toString(); + return $this->{$key}(); } /**