From 8e56a651f589c1ad2e0703b82c5dfb0d5987d6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Mon, 21 Feb 2022 09:55:08 +0100 Subject: [PATCH] Fix Choice annotation/attribute constructor args changing with Symfony versions (#53) --- .github/workflows/tests.yml | 4 +++ src/Validator/Constraints/Enum.php | 54 +++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 81bd036..3e360ab 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,6 +18,10 @@ jobs: symfony-version: 4.4.* - php-version: 8.1 symfony-version: 4.4.* + - php-version: 7.2 + symfony-version: 5.2.* + - php-version: 7.2 + symfony-version: 5.3.* - php-version: 7.2 symfony-version: 5.4.* - php-version: 8.1 diff --git a/src/Validator/Constraints/Enum.php b/src/Validator/Constraints/Enum.php index b597ee0..61444dc 100644 --- a/src/Validator/Constraints/Enum.php +++ b/src/Validator/Constraints/Enum.php @@ -42,23 +42,45 @@ public function __construct( if (\is_string($enum)) { $this->enum = $enum; } - // Symfony 5.x Constraints has many constructor arguments for PHP 8.0 Attributes support - parent::__construct( - null, - $callback, - $multiple, - $strict, - $min, - $max, - $message, - $multipleMessage, - $minMessage, - $maxMessage, - $groups, - $payload, - $options - ); + + $firstConstructorArg = (new \ReflectionClass(Choice::class)) + ->getConstructor()->getParameters()[0]->getName(); + if ($firstConstructorArg === 'choices') { + // Prior to Symfony 5.3, first argument of Choice was $choices + parent::__construct( + null, + $callback, + $multiple, + $strict, + $min, + $max, + $message, + $multipleMessage, + $minMessage, + $maxMessage, + $groups, + $payload, + $options + ); + } else { + // Since Symfony 5.3, first argument of Choice is $options + parent::__construct( + $options, + null, + $callback, + $multiple, + $strict, + $min, + $max, + $message, + $multipleMessage, + $minMessage, + $maxMessage, + $groups, + $payload + ); + } } }