Skip to content

Commit 35f3614

Browse files
Merge branch '5.4' into 6.0
* 5.4: (21 commits) [Finder] Fix finding VCS re-included files in excluded directory [Yaml] Improve the deprecation warnings for octal numbers to suggest migrating Fix Choice constraint with associative choices array [Form] UrlType should not add protocol to emails [Dotenv] Fix bootEnv() override with .env.local.php when the env key already exists Silence isatty warnings during tty detection [Serializer] Fix AbstractObjectNormalizer not considering pseudo type false [Notifier] Fix encoding of messages with FreeMobileTransport [Cache] workaround PHP crash [Console] Fix PHP 8.1 deprecation in ChoiceQuestion [HttpKernel] Fix compatibility with php bridge and already started php sessions [Notifier] smsapi-notifier - correct encoding Replaced full CoC text with link to documentation Making the parser stateless [Console] fix restoring stty mode on CTRL+C fix merge (bis) fix merge [Process] Avoid calling fclose on an already closed resource [GHA] test tty group [DI] Fix tests on PHP 7.1 ...
2 parents 3ab6c72 + b420894 commit 35f3614

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

Constraints/Choice.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function getDefaultOption(): ?string
5252
}
5353

5454
public function __construct(
55-
string|array $choices = null,
55+
string|array $options = [],
56+
array $choices = null,
5657
callable|string $callback = null,
5758
bool $multiple = null,
5859
bool $strict = null,
@@ -63,12 +64,13 @@ public function __construct(
6364
string $minMessage = null,
6465
string $maxMessage = null,
6566
array $groups = null,
66-
mixed $payload = null,
67-
array $options = []
67+
mixed $payload = null
6868
) {
69-
if (\is_array($choices) && \is_string(key($choices))) {
70-
$options = array_merge($choices, $options);
71-
} elseif (null !== $choices) {
69+
if (\is_array($options) && $options && array_is_list($options)) {
70+
$choices ??= $options;
71+
$options = [];
72+
}
73+
if (null !== $choices) {
7274
$options['value'] = $choices;
7375
}
7476

Tests/Constraints/ChoiceTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Choice;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
1518
use Symfony\Component\Validator\Tests\Fixtures\ConstraintChoiceWithPreset;
1619

1720
class ChoiceTest extends TestCase
@@ -22,4 +25,47 @@ public function testSetDefaultPropertyChoice()
2225

2326
self::assertEquals(['A', 'B', 'C'], $constraint->choices);
2427
}
28+
29+
public function testAttributes()
30+
{
31+
$metadata = new ClassMetadata(ChoiceDummy::class);
32+
$loader = new AnnotationLoader();
33+
self::assertTrue($loader->loadClassMetadata($metadata));
34+
35+
/** @var Choice $aConstraint */
36+
[$aConstraint] = $metadata->properties['a']->getConstraints();
37+
self::assertSame([1, 2], $aConstraint->choices);
38+
self::assertSame(['Default', 'ChoiceDummy'], $aConstraint->groups);
39+
40+
/** @var Choice $bConstraint */
41+
[$bConstraint] = $metadata->properties['b']->getConstraints();
42+
self::assertSame(['foo', 'bar'], $bConstraint->choices);
43+
self::assertSame('myMessage', $bConstraint->message);
44+
self::assertSame(['Default', 'ChoiceDummy'], $bConstraint->groups);
45+
46+
/** @var Choice $cConstraint */
47+
[$cConstraint] = $metadata->properties['c']->getConstraints();
48+
self::assertSame([1, 2], $aConstraint->choices);
49+
self::assertSame(['my_group'], $cConstraint->groups);
50+
self::assertSame('some attached data', $cConstraint->payload);
51+
52+
/** @var Choice $stringIndexedConstraint */
53+
[$stringIndexedConstraint] = $metadata->properties['stringIndexed']->getConstraints();
54+
self::assertSame(['one' => 1, 'two' => 2], $stringIndexedConstraint->choices);
55+
}
56+
}
57+
58+
class ChoiceDummy
59+
{
60+
#[Choice(choices: [1, 2])]
61+
private $a;
62+
63+
#[Choice(choices: ['foo', 'bar'], message: 'myMessage')]
64+
private $b;
65+
66+
#[Choice([1, 2], groups: ['my_group'], payload: 'some attached data')]
67+
private $c;
68+
69+
#[Choice(choices: ['one' => 1, 'two' => 2])]
70+
private $stringIndexed;
2571
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": ">=8.0.2",
2020
"symfony/polyfill-ctype": "~1.8",
2121
"symfony/polyfill-mbstring": "~1.0",
22+
"symfony/polyfill-php81": "^1.22",
2223
"symfony/translation-contracts": "^1.1|^2|^3"
2324
},
2425
"require-dev": {

0 commit comments

Comments
 (0)