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

Commit f0d3674

Browse files
committed
chore: spread comparison rules options
1 parent 11c3add commit f0d3674

11 files changed

+37
-72
lines changed

src/Rule/AbstractComparisonRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function assert(mixed $value, string $name): void
2020

2121
if (!$this->compareValues($value, $this->constraint)) {
2222
throw new ($this->getException())(
23-
message: $this->options['message'],
23+
message: $this->message,
2424
parameters: [
2525
'value' => $value,
2626
'name' => $name,

src/Rule/GreaterThan.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6-
use Symfony\Component\OptionsResolver\OptionsResolver;
76

87
class GreaterThan extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/GreaterThanOrEqual.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
6-
use Symfony\Component\OptionsResolver\OptionsResolver;
76

87
class GreaterThanOrEqual extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/LessThan.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6-
use Symfony\Component\OptionsResolver\OptionsResolver;
76

87
class LessThan extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/LessThanOrEqual.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanOrEqualException;
6-
use Symfony\Component\OptionsResolver\OptionsResolver;
76

87
class LessThanOrEqual extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/Range.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,20 @@
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
10-
use Symfony\Component\OptionsResolver\OptionsResolver;
1110

1211
class Range extends AbstractRule implements RuleInterface
1312
{
1413
use ComparableTrait;
1514

16-
private array $options;
15+
private string $message;
1716

1817
public function __construct(
1918
private readonly mixed $minConstraint,
2019
private readonly mixed $maxConstraint,
21-
array $options = []
20+
?string $message = null
2221
)
2322
{
24-
$resolver = new OptionsResolver();
25-
26-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.']);
27-
28-
$resolver->setAllowedTypes('message', 'string');
29-
30-
$this->options = $resolver->resolve($options);
23+
$this->message = $message ?? 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.';
3124
}
3225

3326
public function assert(mixed $value, string $name): void
@@ -54,7 +47,7 @@ public function assert(mixed $value, string $name): void
5447
->validate($value)
5548
) {
5649
throw new RangeException(
57-
message: $this->options['message'],
50+
message: $this->message,
5851
parameters: [
5952
'value' => $value,
6053
'name' => $name,

tests/GreaterThanOrEqualTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
6060
yield 'message' => [
6161
new GreaterThanOrEqual(
6262
constraint: 10,
63-
options: [
64-
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than or equal to "{{ constraint }}".'
65-
]
66-
), 1, 'The "test" value "1" is not greater than or equal to "10".'
63+
message: 'The "{{ name }}" value "{{ value }}" is not greater than or equal to "{{ constraint }}".'
64+
),
65+
1,
66+
'The "test" value "1" is not greater than or equal to "10".'
6767
];
6868
}
6969
}

tests/GreaterThanTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
6060
yield 'message' => [
6161
new GreaterThan(
6262
constraint: 10,
63-
options: [
64-
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
65-
]
66-
), 1, 'The "test" value "1" is not greater than "10".'
63+
message: 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
64+
),
65+
1,
66+
'The "test" value "1" is not greater than "10".'
6767
];
6868
}
6969
}

tests/LessThanOrEqualTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
6060
yield 'message' => [
6161
new LessThanOrEqual(
6262
constraint: 10,
63-
options: [
64-
'message' => 'The "{{ name }}" value "{{ value }}" is not less than or equal to "{{ constraint }}".'
65-
]
66-
), 20, 'The "test" value "20" is not less than or equal to "10".'
63+
message: 'The "{{ name }}" value "{{ value }}" is not less than or equal to "{{ constraint }}".'
64+
),
65+
20,
66+
'The "test" value "20" is not less than or equal to "10".'
6767
];
6868
}
6969
}

tests/LessThanTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
6060
yield 'message' => [
6161
new LessThan(
6262
constraint: 10,
63-
options: [
64-
'message' => 'The "{{ name }}" value "{{ value }}" is not less than "{{ constraint }}".'
65-
]
66-
), 20, 'The "test" value "20" is not less than "10".'
63+
message: 'The "{{ name }}" value "{{ value }}" is not less than "{{ constraint }}".'
64+
),
65+
20,
66+
'The "test" value "20" is not less than "10".'
6767
];
6868
}
6969
}

0 commit comments

Comments
 (0)