8000 bug #46957 [HttpFoundation] Fix `\Stringable` support in `InputBag::g… · symfony/http-foundation@69302fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 69302fb

Browse files
committed
bug #46957 [HttpFoundation] Fix \Stringable support in InputBag::get() (chalasr)
This PR was merged into the 6.0 branch. Discussion ---------- [HttpFoundation] Fix `\Stringable` support in `InputBag::get()` | Q | A | ------------- | --- | Branch? | 6.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Spotted in symfony/symfony#46936 (comment), thanks @fritzmg. Commits ------- 6dfe92e091 [HttpFoundation] Fix `\Stringable` support in `InputBag::get()`
2 parents 854c68b + cbbb036 commit 69302fb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

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

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