Skip to content

Commit 4186137

Browse files
committed
Allow respond body part with stringable
1 parent 4dc9d62 commit 4186137

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Route.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ protected function makeResponseBodyPart(mixed $result) : string
352352
$this->router->getResponse()->setJson($result);
353353
return '';
354354
}
355+
if (\is_object($result) && \method_exists($result, '__toString')) {
356+
return (string) $result;
357+
}
355358
$type = \get_debug_type($result);
356359
throw new RoutingException(
357360
"Invalid action return type '{$type}'" . $this->onNamedRoutePart()

tests/RouteTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ static function () {
286286
self::assertSame('1.5', $this->response->getBody());
287287
}
288288

289+
public function testResponseBodyPartWithStringable() : void
290+
{
291+
$route = new Route(
292+
$this->router,
293+
'http://domain.tld',
294+
'/',
295+
static function () {
296+
return new class() {
297+
public function __toString() : string
298+
{
299+
return '__toString';
300+
}
301+
};
302+
}
303+
);
304+
self::assertInstanceOf(Response::class, $route->run());
305+
self::assertSame('__toString', $this->response->getBody());
306+
}
307+
289308
public function testResponseBodyPartWithJsonSerializable() : void
290309
{
291310
$data = ['id' => 1, 'name' => 'Natan'];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy