8000 [HttpFoundation] Fix `\Stringable` support in `InputBag::get()` · symfony/symfony@6dfe92e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6dfe92e

Browse files
committed
[HttpFoundation] Fix \Stringable support in InputBag::get()
1 parent c158a6a commit 6dfe92e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function get(string $key, mixed $default = null): string|int|float|bool|n
3333

3434
$value = parent::get($key, $this);
3535

36-
if (null !== $value && $this !== $value && !\is_scalar($value)) {
36+
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
3737
throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
3838
}
3939

src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ class InputBagTest extends TestCase
1919
{
2020
public function testGet()
2121
{
22-
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false]);
22+
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false, 'stringable' => new class() implements \Stringable {
23+
public function __toString(): string
24+
{
25+
return 'strval';
26+
}
27+
}]);
2328

2429
$this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
2530
$this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
2631
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
2732
$this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
2833
$this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
34+
$this->assertSame('strval', $bag->get('stringable'), '->get() gets the string value of a \Stringable parameter');
2935
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
3036
}
3137

0 commit comments

Comments
 (0)
0