From e84c0774c3aaf9f0e6032015c5aa15eefb83a7e7 Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Sat, 23 Jan 2021 12:34:55 -0500 Subject: [PATCH 1/3] Support JSON encoding Stringable --- src/Illuminate/Support/Stringable.php | 13 ++++++++++++- tests/Support/SupportStringableTest.php | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index c24d19e6a923..28e2e8451f0c 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; @@ -722,6 +723,16 @@ public function dd() exit(1); } + /** + * Convert the object to a string when JSON encoded. + * + * @return string + */ + public function jsonSerialize() + { + return $this->value; + } + /** * Proxy dynamic properties onto methods. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 1986d050de9c..c093e3f013e0 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -545,4 +545,9 @@ public function testChunk() $this->assertInstanceOf(Collection::class, $chunks); $this->assertSame(['foo', 'bar', 'baz'], $chunks->all()); } + + public function testJsonSerialize() + { + $this->assertSame('"foo"', json_encode($this->stringable('foo'))); + } } From 76bb6528a1f2ac1a0f0669721c57908467eb690c Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Mon, 25 Jan 2021 08:21:16 -0500 Subject: [PATCH 2/3] Call __toString() from jsonSerialize() in Stringable class --- 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 fe72e94fce2d..65320e1bcf3f 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -742,7 +742,7 @@ public function dd() */ public function jsonSerialize() { - return $this->value; + return $this->__toString(); } /** From bfe9c569c75091919f66e3c24759bf0c065d4b8d Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Mon, 25 Jan 2021 08:21:42 -0500 Subject: [PATCH 3/3] Update router to properly handle Stringable instances --- src/Illuminate/Routing/Router.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index f1b61dc74dcf..f1912bf0374e 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -18,6 +18,7 @@ use Illuminate\Routing\Events\RouteMatched; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use Illuminate\Support\Stringable; use Illuminate\Support\Traits\Macroable; use JsonSerializable; use Psr\Http\Message\ResponseInterface as PsrResponseInterface; @@ -769,6 +770,8 @@ public static function toResponse($request, $response) $response = (new HttpFoundationFactory)->createResponse($response); } elseif ($response instanceof Model && $response->wasRecentlyCreated) { $response = new JsonResponse($response, 201); + } elseif ($response instanceof Stringable) { + $response = new Response($response->__toString(), 200, ['Content-Type' => 'text/html']); } elseif (! $response instanceof SymfonyResponse && ($response instanceof Arrayable || $response instanceof Jsonable ||