Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e5a6566

Browse filesBrowse files
committed
feat: support enums in Choice constraints
1 parent 1b7526e commit e5a6566
Copy full SHA for e5a6566

File tree

Expand file treeCollapse file tree

4 files changed

+25
-1
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+25
-1
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for `ConstraintViolationList::createFromMessage()`
8+
* Add enum support (>= PHP 8.1) for the `Choice` constraint
89

910
5.3
1011
---

‎src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function validate($value, Constraint $constraint)
3535
throw new UnexpectedTypeException($constraint, Choice::class);
3636
}
3737

38-
if (!\is_array($constraint->choices) && !$constraint->callback) {
38+
if (!\is_array($constraint->choices) && !\is_a($constraint->choices, \BackedEnum::class, true) && !$constraint->callback) {
3939
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.');
4040
}
4141

@@ -55,6 +55,13 @@ public function validate($value, Constraint $constraint)
5555
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback.');
5656
}
5757
$choices = $choices();
58+
} elseif (\is_a($constraint->choices, \BackedEnum::class, true)) {
59+
$choices = \array_map(
60+
function (\BackedEnum $enum) {
61+
return $enum->value;
62+
},
63+
$constraint->choices::cases()
64+
);
5865
} else {
5966
$choices = $constraint->choices;
6067
}
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Validator\Test;
6+
7+
enum SampleBackedEnum: string
8+
{
9+
case FOO = 'foo';
10+
case BAR = 'bar';
11+
}

‎src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1717
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1818
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19+
use Symfony\Component\Validator\Test\SampleBackedEnum;
1920

2021
function choice_callback()
2122
{
@@ -93,6 +94,10 @@ public function provideConstraintsWithChoicesArray(): iterable
9394
if (\PHP_VERSION_ID >= 80000) {
9495
yield 'named arguments' => [eval('return new \Symfony\Component\Validator\Constraints\Choice(choices: ["foo", "bar"]);')];
9596
}
97+
98+
if (\PHP_VERSION_ID >= 80100) {
99+
yield 'enums' => [new Choice(SampleBackedEnum::class)];
100+
}
96101
}
97102

98103
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.