From c2f036c159950081d39cab4788b6938c7dd825fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Palluel-Lafleur=20Fr=C3=A9d=C3=A9ric?= Date: Sun, 20 May 2018 15:59:52 +0200 Subject: [PATCH] =?UTF-8?q?fix(SymfonyConstraintAnnotationReader):=20fixed?= =?UTF-8?q?=20enum=20guessing=20in=20Assert=E2=80=A6=20(#1325)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(SymfonyConstraintAnnotationReader): fixed enum guessing in Assert\Choicewhen callback parameter is used * fix(SymfonyConstraints): turn double quotes in single quotes --- .../SymfonyConstraintAnnotationReader.php | 2 +- .../Functional/Entity/SymfonyConstraints.php | 23 +++++++++++++++++++ Tests/Functional/FunctionalTest.php | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index 722844e..c4df7cd 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -70,7 +70,7 @@ class SymfonyConstraintAnnotationReader } if ($annotation instanceof Assert\Choice) { - $property->setEnum($annotation->choices); + $property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices); } if ($annotation instanceof Assert\Expression) { diff --git a/Tests/Functional/Entity/SymfonyConstraints.php b/Tests/Functional/Entity/SymfonyConstraints.php index 13d2e08..6bea33e 100644 --- a/Tests/Functional/Entity/SymfonyConstraints.php +++ b/Tests/Functional/Entity/SymfonyConstraints.php @@ -64,6 +64,13 @@ class SymfonyConstraints */ private $propertyChoice; + /** + * @var int + * + * @Assert\Choice(callback={SymfonyConstraints::class,"fetchAllowedChoices"}) + */ + private $propertyChoiceWithCallback; + /** * @var int * @@ -130,6 +137,14 @@ class SymfonyConstraints $this->propertyChoice = $propertyChoice; } + /** + * @param int $propertyChoiceWithCallback + */ + public function setPropertyChoiceWithCallback(int $propertyChoiceWithCallback): void + { + $this->propertyChoiceWithCallback = $propertyChoiceWithCallback; + } + /** * @param int $propertyExpression */ @@ -137,4 +152,12 @@ class SymfonyConstraints { $this->propertyExpression = $propertyExpression; } + + /** + * @return array + */ + public static function fetchAllowedChoices() + { + return ['choice1', 'choice2']; + } } diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index e53274b..64dfbaf 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -341,6 +341,10 @@ class FunctionalTest extends WebTestCase 'type' => 'integer', 'enum' => ['choice1', 'choice2'], ], + 'propertyChoiceWithCallback' => [ + 'type' => 'integer', + 'enum' => ['choice1', 'choice2'], + ], 'propertyExpression' => [ 'type' => 'integer', 'pattern' => 'If this is a tech post, the category should be either php or symfony!',