Skip to content

Commit 8e56a65

Browse files
authored
Fix Choice annotation/attribute constructor args changing with Symfony versions (#53)
1 parent e98614b commit 8e56a65

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ jobs:
1818
symfony-version: 4.4.*
1919
- php-version: 8.1
2020
symfony-version: 4.4.*
21+
- php-version: 7.2
22+
symfony-version: 5.2.*
23+
- php-version: 7.2
24+
symfony-version: 5.3.*
2125
- php-version: 7.2
2226
symfony-version: 5.4.*
2327
- php-version: 8.1

src/Validator/Constraints/Enum.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,45 @@ public function __construct(
4242
if (\is_string($enum)) {
4343
$this->enum = $enum;
4444
}
45-
4645
// Symfony 5.x Constraints has many constructor arguments for PHP 8.0 Attributes support
47-
parent::__construct(
48-
null,
49-
$callback,
50-
$multiple,
51-
$strict,
52-
$min,
53-
$max,
54-
$message,
55-
$multipleMessage,
56-
$minMessage,
57-
$maxMessage,
58-
$groups,
59-
$payload,
60-
$options
61-
);
46+
47+
$firstConstructorArg = (new \ReflectionClass(Choice::class))
48+
->getConstructor()->getParameters()[0]->getName();
49+
if ($firstConstructorArg === 'choices') {
50+
// Prior to Symfony 5.3, first argument of Choice was $choices
51+
parent::__construct(
52+
null,
53+
$callback,
54+
$multiple,
55+
$strict,
56+
$min,
57+
$max,
58+
$message,
59+
$multipleMessage,
60+
$minMessage,
61+
$maxMessage,
62+
$groups,
63+
$payload,
64+
$options
65+
);
66+
} else {
67+
// Since Symfony 5.3, first argument of Choice is $options
68+
parent::__construct(
69+
$options,
70+
null,
71+
$callback,
72+
$multiple,
73+
$strict,
74+
$min,
75+
$max,
76+
$message,
77+
$multipleMessage,
78+
$minMessage,
79+
$maxMessage,
80+
$groups,
81+
$payload
82+
);
83+
}
6284
}
6385
}
6486

0 commit comments

Comments
 (0)