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

Commit 03acf9c

Browse files
committed
chore: spread All options
1 parent f0d3674 commit 03acf9c

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

src/Rule/All.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ValidatableTrait;
9-
use Symfony\Component\OptionsResolver\OptionsResolver;
109

1110
class All extends AbstractRule implements RuleInterface
1211
{
1312
use ValidatableTrait;
1413

15-
private array $options;
14+
private string $message;
1615

1716
/**
1817
* @param RuleInterface[] $constraints
1918
*/
2019
public function __construct(
2120
private readonly array $constraints,
22-
array $options = []
21+
?string $message = null
2322
)
2423
{
25-
$resolver = new OptionsResolver();
26-
27-
$resolver->setDefaults(['message' => 'At "{{ key }}": {{ message }}']);
28-
29-
$resolver->setAllowedTypes('message', 'string');
30-
31-
$this->options = $resolver->resolve($options);
24+
$this->message = $message ?? 'At "{{ key }}": {{ message }}';
3225
}
3326

3427
public function assert(mixed $value, string $name): void
@@ -45,23 +38,23 @@ public function assert(mixed $value, string $name): void
4538
);
4639
}
4740

48-
foreach ($value as $key => $input) {
49-
foreach ($this->constraints as $constraint) {
50-
try {
41+
try {
42+
foreach ($value as $key => $input) {
43+
foreach ($this->constraints as $constraint) {
5144
$constraint->assert($input, $name);
5245
}
53-
catch (ValidationException $exception) {
54-
throw new AllException(
55-
message: $this->options['message'],
56-
parameters: [
57-
'value' => $value,
58-
'name' => $name,
59-
'key' => $key,
60-
'message' => $exception->getMessage()
61-
]
62-
);
63-
}
6446
}
6547
}
48+
catch (ValidationException $exception) {
49+
throw new AllException(
50+
message: $this->message,
51+
parameters: [
52+
'value' => $value,
53+
'name' => $name,
54+
'key' => $key,
55+
'message' => $exception->getMessage()
56+
]
57+
);
58+
}
6659
}
6760
}

tests/AllTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public static function provideRuleMessageOptionData(): \Generator
8484
yield 'constraint' => [
8585
new All(
8686
constraints: [new NotBlank()],
87-
options: [
88-
'message' => 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}".'
89-
]
90-
), [1, 2, ''], 'The "test" value "[1, 2, ]" failed at key "2".'
87+
message: 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}".'
88+
),
89+
[1, 2, ''],
90+
'The "test" value "[1, 2, ]" failed at key "2".'
9191
];
9292
}
9393
}

0 commit comments

Comments
 (0)