Skip to content

Commit 36aabb2

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 #46936 (comment), thanks @fritzmg. Commits ------- 6dfe92e [HttpFoundation] Fix `\Stringable` support in `InputBag::get()`
2 parents 7129a54 + 6dfe92e commit 36aabb2

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)
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