Skip to content

Commit 1bb884d

Browse files
committed
feat: support enums in Choice constraints
1 parent 1b7526e commit 1bb884d

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines 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

Lines 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
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Validator\Test;
15+
16+
enum SampleBackedEnum: string
17+
{
18+
case FOO = 'foo';
19+
case BAR = 'bar';
20+
}

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

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