From 1f9a3a115890b1436e6b228793e29a92bf7e5e5e Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Thu, 16 Sep 2021 09:57:32 +0200 Subject: [PATCH 1/3] feat: support enums in Choice constraints --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Validator/Constraints/ChoiceValidator.php | 9 ++++++++- .../Validator/Test/SampleBackedEnum.php | 20 +++++++++++++++++++ .../Tests/Constraints/ChoiceValidatorTest.php | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Validator/Test/SampleBackedEnum.php diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index f73923bc1f1f4..4fe89e7c775f1 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add support for `ConstraintViolationList::createFromMessage()` + * Add enum support (>= PHP 8.1) for the `Choice` constraint 5.3 --- diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index a1c47a6bb22ed..4978ee6680d48 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -35,7 +35,7 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, Choice::class); } - if (!\is_array($constraint->choices) && !$constraint->callback) { + if (!\is_array($constraint->choices) && !is_a($constraint->choices, \BackedEnum::class, true) && !$constraint->callback) { throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.'); } @@ -55,6 +55,13 @@ public function validate($value, Constraint $constraint) throw new ConstraintDefinitionException('The Choice constraint expects a valid callback.'); } $choices = $choices(); + } elseif (is_a($constraint->choices, \BackedEnum::class, true)) { + $choices = array_map( + function (\BackedEnum $enum) { + return $enum->value; + }, + $constraint->choices::cases() + ); } else { $choices = $constraint->choices; } diff --git a/src/Symfony/Component/Validator/Test/SampleBackedEnum.php b/src/Symfony/Component/Validator/Test/SampleBackedEnum.php new file mode 100644 index 0000000000000..60b8b3cb90b2c --- /dev/null +++ b/src/Symfony/Component/Validator/Test/SampleBackedEnum.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Validator\Test; + +enum SampleBackedEnum: string +{ + case FOO = 'foo'; + case BAR = 'bar'; +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 262e8654f043b..27572fd007f45 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\UnexpectedValueException; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +use Symfony\Component\Validator\Test\SampleBackedEnum; function choice_callback() { @@ -93,6 +94,10 @@ public function provideConstraintsWithChoicesArray(): iterable if (\PHP_VERSION_ID >= 80000) { yield 'named arguments' => [eval('return new \Symfony\Component\Validator\Constraints\Choice(choices: ["foo", "bar"]);')]; } + + if (\PHP_VERSION_ID >= 80100) { + yield 'enums' => [new Choice(SampleBackedEnum::class)]; + } } /** From 62940fcaefacc75d79ab55d4b89248b569d520d4 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Thu, 16 Sep 2021 12:56:50 +0200 Subject: [PATCH 2/3] fix: reviews --- .../Component/Validator/Constraints/ChoiceValidator.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index 4978ee6680d48..8cf3952731ae6 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, Choice::class); } - if (!\is_array($constraint->choices) && !is_a($constraint->choices, \BackedEnum::class, true) && !$constraint->callback) { + $valid = \is_array($constraint->choices) + || (\is_string($constraint->choices) && is_a($constraint->choices, \BackedEnum::class, true)) + || isset($constraint->callback); + + if (!$valid) { throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.'); } From 0bbe047df891ac325c8e6488cb1b624c522ec92d Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Fri, 17 Sep 2021 13:48:23 +0200 Subject: [PATCH 3/3] fix: reviews --- .../Component/Validator/Constraints/ChoiceValidator.php | 7 +------ .../Validator/Tests/Constraints/ChoiceValidatorTest.php | 2 +- .../Validator/{Test => Tests}/SampleBackedEnum.php | 4 +--- 3 files changed, 3 insertions(+), 10 deletions(-) rename src/Symfony/Component/Validator/{Test => Tests}/SampleBackedEnum.php (81%) diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index 8cf3952731ae6..169eec74aee0c 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -60,12 +60,7 @@ public function validate($value, Constraint $constraint) } $choices = $choices(); } elseif (is_a($constraint->choices, \BackedEnum::class, true)) { - $choices = array_map( - function (\BackedEnum $enum) { - return $enum->value; - }, - $constraint->choices::cases() - ); + $choices = \array_column($constraint->choices::cases(), 'value'); } else { $choices = $constraint->choices; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 27572fd007f45..7c160f81e3e06 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -16,7 +16,7 @@ use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\UnexpectedValueException; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; -use Symfony\Component\Validator\Test\SampleBackedEnum; +use Symfony\Component\Validator\Tests\SampleBackedEnum; function choice_callback() { diff --git a/src/Symfony/Component/Validator/Test/SampleBackedEnum.php b/src/Symfony/Component/Validator/Tests/SampleBackedEnum.php similarity index 81% rename from src/Symfony/Component/Validator/Test/SampleBackedEnum.php rename to src/Symfony/Component/Validator/Tests/SampleBackedEnum.php index 60b8b3cb90b2c..c40d1a44e00d9 100644 --- a/src/Symfony/Component/Validator/Test/SampleBackedEnum.php +++ b/src/Symfony/Component/Validator/Tests/SampleBackedEnum.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -declare(strict_types=1); - -namespace Symfony\Component\Validator\Test; +namespace Symfony\Component\Validator\Tests; enum SampleBackedEnum: string { 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