Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bb289f

Browse files
committed
[HttpFoundation] ParameterBag::getEnum()
1 parent f43cd26 commit 3bb289f

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ CHANGELOG
44
6.3
55
---
66

7-
* Create migration for session table when pdo handler is used
7+
* Add `ParameterBag::getEnum()`
8+
* Create migration for session table when pdo handler is used
89

910
6.2
1011
---

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public function set(string $key, mixed $value)
7373
$this->parameters[$key] = $value;
7474
}
7575

76+
public function getEnum(string $key, string $class, \BackedEnum $default = null): ?\BackedEnum
77+
{
78+
try {
79+
return parent::getEnum($key, $class, $default);
80+
} catch (\UnexpectedValueException $e) {
81+
throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
82+
}
83+
}
84+
7685
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
7786
{
7887
$value = $this->has($key) ? $this->all()[$key] : $default;

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,31 @@ public function getBoolean(string $key, bool $default = false): bool
141141
return $this->filter($key, $default, \FILTER_VALIDATE_BOOL);
142142
}
143143

144+
/**
145+
* Returns the parameter value converted to an enum.
146+
*
147+
* @template T of \BackedEnum
148+
*
149+
* @param class-string<T> $class
150+
* @param ?T $default
151+
*
152+
* @return ?T
153+
*/
154+
public function getEnum(string $key, string $class, \BackedEnum $default = null): ?\BackedEnum
155+
{
156+
$value = $this->get($key);
157+
158+
if (null === $value) {
159+
return $default;
160+
}
161+
162+
try {
163+
return $class::from($value);
164+
} catch (\ValueError|\TypeError $e) {
165+
throw new \UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
166+
}
167+
}
168+
144169
/**
145170
* Filter key.
146171
*

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,21 @@ public function testFilterArrayWithoutArrayFlag()
106106
$bag = new InputBag(['foo' => ['bar', 'baz']]);
107107
$bag->filter('foo', \FILTER_VALIDATE_INT);
108108
}
109+
110+
public function testGetEnum()
111+
{
112+
$bag = new InputBag(['valid-value' => 1]);
113+
114+
$this->assertSame(Foo::Bar, $bag->getEnum('valid-value', Foo::class));
115+
}
116+
117+
public function testGetEnumThrowsExceptionWithInvalidValue()
118+
{
119+
$bag = new InputBag(['invalid-value' => 2]);
120+
121+
$this->expectException(BadRequestException::class);
122+
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum "Symfony\Component\HttpFoundation\Tests\Foo".');
123+
124+
$this->assertNull($bag->getEnum('invalid-value', Foo::class));
125+
}
109126
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,39 @@ public function testGetBoolean()
226226
$this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
227227
$this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
228228
}
229+
230+
public function testGetEnum()
231+
{
232+
$bag = new ParameterBag(['valid-value' => 1]);
233+
234+
$this->assertSame(Foo::Bar, $bag->getEnum('valid-value', Foo::class));
235+
236+
$this->assertNull($bag->getEnum('invalid-key', Foo::class));
237+
$this->assertSame(Foo::Bar, $bag->getEnum('invalid-key', Foo::class, Foo::Bar));
238+
}
239+
240+
public function testGetEnumThrowsExceptionWithNotBackingValue()
241+
{
242+
$bag = new ParameterBag(['invalid-value' => 2]);
243+
244+
$this->expectException(\UnexpectedValueException::class);
245+
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum "Symfony\Component\HttpFoundation\Tests\Foo".');
246+
247+
$this->assertNull($bag->getEnum('invalid-value', Foo::class));
248+
}
249+
250+
public function testGetEnumThrowsExceptionWithInvalidValueType()
251+
{
252+
$bag = new ParameterBag(['invalid-value' => ['foo']]);
253+
254+
$this->expectException(\UnexpectedValueException::class);
255+
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: Symfony\Component\HttpFoundation\Tests\Foo::from(): Argument #1 ($value) must be of type int, array given.');
256+
257+
$this->assertNull($bag->getEnum('invalid-value', Foo::class));
258+
}
259+
}
260+
261+
enum Foo: int
262+
{
263+
case Bar = 1;
229264
}

0 commit comments

Comments
 (0)