Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit aae3642

Browse files
committed
chore: spread Choice options
1 parent 03acf9c commit aae3642

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=8.1",
16-
"symfony/options-resolver": "^6.3"
15+
"php": ">=8.1"
1716
},
1817
"require-dev": {
1918
"phpunit/phpunit": "^10.0",

src/Rule/Choice.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ChoiceException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7-
use Symfony\Component\OptionsResolver\OptionsResolver;
87

98
class Choice extends AbstractRule implements RuleInterface
109
{
11-
private array $options;
10+
private string $message;
11+
12+
private string $multipleMessage;
13+
14+
private string $minMessage;
15+
16+
private string $maxMessage;
1217

1318
public function __construct(
1419
private readonly array $constraints,
1520
private readonly bool $multiple = false,
1621
private readonly ?int $minConstraint = null,
1722
private readonly ?int $maxConstraint = null,
18-
array $options = []
23+
?string $message = null,
24+
?string $multipleMessage = null,
25+
?string $minMessage = null,
26+
?string $maxMessage = null
1927
)
2028
{
21-
$resolver = new OptionsResolver();
22-
23-
$resolver->setDefaults([
24-
'message' => 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
25-
'multipleMessage' => 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
26-
'minMessage' => 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
27-
'maxMessage' => 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
28-
]);
29-
30-
$resolver->setAllowedTypes('message', 'string');
31-
$resolver->setAllowedTypes('multipleMessage', 'string');
32-
$resolver->setAllowedTypes('minMessage', 'string');
33-
$resolver->setAllowedTypes('maxMessage', 'string');
34-
35-
$this->options = $resolver->resolve($options);
29+
$this->message = $message ?? 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
30+
$this->multipleMessage = $multipleMessage ?? 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
31+
$this->minMessage = $minMessage ?? 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.';
32+
$this->maxMessage = $maxMessage ?? 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.';
3633
}
3734

3835
public function assert(mixed $value, string $name): void
@@ -58,7 +55,7 @@ public function assert(mixed $value, string $name): void
5855
foreach ($value as $input) {
5956
if (!\in_array($input, $this->constraints, true)) {
6057
throw new ChoiceException(
61-
message: $this->options['multipleMessage'],
58+
message: $this->multipleMessage,
6259
parameters: [
6360
'value' => $value,
6461
'name' => $name,
@@ -72,7 +69,7 @@ public function assert(mixed $value, string $name): void
7269

7370
if ($this->minConstraint !== null && $numValues < $this->minConstraint) {
7471
throw new ChoiceException(
75-
message: $this->options['minMessage'],
72+
message: $this->minMessage,
7673
parameters: [
7774
'value' => $value,
7875
'numValues' => $numValues,
@@ -86,7 +83,7 @@ public function assert(mixed $value, string $name): void
8683

8784
if ($this->maxConstraint !== null && $numValues > $this->maxConstraint) {
8885
throw new ChoiceException(
89-
message: $this->options['maxMessage'],
86+
message: $this->maxMessage,
9087
parameters: [
9188
'value' => $value,
9289
'numValues' => $numValues,
@@ -100,7 +97,7 @@ public function assert(mixed $value, string $name): void
10097
}
10198
else if (!\in_array($value, $this->constraints, true)) {
10299
throw new ChoiceException(
103-
message: $this->options['message'],
100+
message: $this->message,
104101
parameters: [
105102
'value' => $value,
106103
'name' => $name,

tests/ChoiceTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,39 @@ public static function provideRuleMessageOptionData(): \Generator
6464
yield 'message' => [
6565
new Choice(
6666
constraints: $constraints,
67-
options: [
68-
'message' => 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
69-
]
70-
), 10, 'The "test" value "10" is not a valid choice.'
67+
message: 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
68+
),
69+
10,
70+
'The "test" value "10" is not a valid choice.'
7171
];
7272
yield 'multiple message' => [
7373
new Choice(
7474
constraints: $constraints,
7575
multiple: true,
76-
options: [
77-
'multipleMessage' => 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
78-
]
79-
), [10], 'The "test" value "[10]" is not a valid choice.'
76+
multipleMessage: 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
77+
),
78+
[10],
79+
'The "test" value "[10]" is not a valid choice.'
8080
];
8181
yield 'min message' => [
8282
new Choice(
8383
constraints: $constraints,
8484
multiple: true,
8585
minConstraint: 2,
86-
options: [
87-
'minMessage' => 'The "{{ name }}" value should have at least {{ minConstraint }} choices.'
88-
]
89-
), [1], 'The "test" value should have at least 2 choices.'
86+
minMessage: 'The "{{ name }}" value should have at least {{ minConstraint }} choices.'
87+
),
88+
[1],
89+
'The "test" value should have at least 2 choices.'
9090
];
9191
yield 'max message' => [
9292
new Choice(
9393
constraints: $constraints,
9494
multiple: true,
9595
maxConstraint: 2,
96-
options: [
97-
'maxMessage' => 'The "{{ name }}" value should have at most {{ maxConstraint }} choices.'
98-
]
99-
), [1, 2, 3], 'The "test" value should have at most 2 choices.'
96+
maxMessage: 'The "{{ name }}" value should have at most {{ maxConstraint }} choices.'
97+
),
98+
[1, 2, 3],
99+
'The "test" value should have at most 2 choices.'
100100
];
101101
}
102102

0 commit comments

Comments
 (0)