From 9579aec77935371108543effb149392559915552 Mon Sep 17 00:00:00 2001 From: "Nikola Svitlica a.k.a. TheCelavi" Date: Fri, 20 Jan 2017 11:16:08 +0100 Subject: [PATCH 1/3] [Validator] New feature: Choice validator is able to use class constants as source of choices from now on. --- .../Validator/Constraints/Choice.php | 1 + .../Validator/Constraints/ChoiceValidator.php | 3 +++ .../Tests/Constraints/ChoiceValidatorTest.php | 27 ++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Choice.php b/src/Symfony/Component/Validator/Constraints/Choice.php index 4b93c70e4a5f4..64bec6d393d95 100644 --- a/src/Symfony/Component/Validator/Constraints/Choice.php +++ b/src/Symfony/Component/Validator/Constraints/Choice.php @@ -33,6 +33,7 @@ class Choice extends Constraint public $choices; public $callback; + public $enum; public $multiple = false; public $strict = false; public $min; diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index 6c81b3b4e0b22..7a6e1d60031e1 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -54,6 +54,9 @@ public function validate($value, Constraint $constraint) throw new ConstraintDefinitionException('The Choice constraint expects a valid callback'); } $choices = call_user_func($choices); + } elseif ($constraint->enum) { + $enum = new \ReflectionClass($constraint->enum); + $choices = $enum->getConstants(); } 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 01483161c9fc4..62fb981002bf1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -22,6 +22,9 @@ function choice_callback() class ChoiceValidatorTest extends ConstraintValidatorTestCase { + const FOO = 'foo'; + const BAR = 'bar'; + protected function createValidator() { return new ChoiceValidator(); @@ -69,7 +72,7 @@ public function testNullIsValid() /** * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException */ - public function testChoicesOrCallbackExpected() + public function testChoicesOrCallbackOrEnumExpected() { $this->validator->validate('foobar', new Choice(array('strict' => true))); } @@ -149,6 +152,28 @@ public function testValidChoiceCallbackContextObjectMethod() $this->assertNoViolation(); } + public function testValidChoiceSplEnum() + { + $constraint = new Choice(array('enum' => new class extends \SplEnum { + const __default = 'foo'; + const FOO = 'foo'; + const BAR = 'bar'; + })); + + $this->validator->validate('foo', $constraint); + + $this->assertNoViolation(); + } + + public function testValidChoiceClassConstantsEnum() + { + $constraint = new Choice(array('enum' => __CLASS__)); + + $this->validator->validate('foo', $constraint); + + $this->assertNoViolation(); + } + public function testMultipleChoices() { $constraint = new Choice(array( From 3411276736ca071b3a809a8b71e7d579a6e8ab4e Mon Sep 17 00:00:00 2001 From: "Nikola Svitlica a.k.a. TheCelavi" Date: Fri, 20 Jan 2017 12:33:31 +0100 Subject: [PATCH 2/3] Fixing missing check in constraing and adding skip-test in tests where SplEnum is not installed. --- .../Component/Validator/Constraints/ChoiceValidator.php | 2 +- .../Validator/Tests/Constraints/ChoiceValidatorTest.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index 7a6e1d60031e1..de4a704a8506c 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice'); } - if (!is_array($constraint->choices) && !$constraint->callback) { + if (!is_array($constraint->choices) && !$constraint->callback && !$constraint->enum) { throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice'); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 62fb981002bf1..0cd6cc47809eb 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -154,6 +154,10 @@ public function testValidChoiceCallbackContextObjectMethod() public function testValidChoiceSplEnum() { + if (!class_exists('\SplEnum')) { + $this->markTestSkipped('SplEnum is not installed on this system.'); + } + $constraint = new Choice(array('enum' => new class extends \SplEnum { const __default = 'foo'; const FOO = 'foo'; From ff233c9f80049b90be8be2cf3905b43ccd4b7487 Mon Sep 17 00:00:00 2001 From: "Nikola Svitlica a.k.a. TheCelavi" Date: Sun, 8 Oct 2017 20:23:07 +0200 Subject: [PATCH 3/3] Removed redundant test, synhornized with master branch. --- .../Tests/Constraints/ChoiceValidatorTest.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index cabae49bdb792..774047420c66a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -149,23 +149,6 @@ public function testValidChoiceCallbackContextObjectMethod() $this->assertNoViolation(); } - public function testValidChoiceSplEnum() - { - if (!class_exists('\SplEnum')) { - $this->markTestSkipped('SplEnum is not installed on this system.'); - } - - $constraint = new Choice(array('enum' => new class extends \SplEnum { - const __default = 'foo'; - const FOO = 'foo'; - const BAR = 'bar'; - })); - - $this->validator->validate('foo', $constraint); - - $this->assertNoViolation(); - } - public function testValidChoiceClassConstantsEnum() { $constraint = new Choice(array('enum' => __CLASS__));