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

Commit d5f272f

Browse files
committed
chore: added option to GreaterThan rule
1 parent f652f21 commit d5f272f

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/Rule/GreaterThan.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
78

89
class GreaterThan extends AbstractRule implements RuleInterface
910
{
1011
use AssertComparableTrait;
1112

12-
private string $message;
13+
private array $options;
1314

14-
public function __construct(
15-
private readonly mixed $constraint,
16-
string $message = null
17-
)
15+
public function __construct(private readonly mixed $constraint, array $options = [])
1816
{
19-
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
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);
2024
}
2125

2226
/**
@@ -29,7 +33,7 @@ public function assert(mixed $value, string $name): void
2933

3034
if (!($value > $this->constraint)) {
3135
throw new GreaterThanException(
32-
message: $this->message,
36+
message: $this->options['message'],
3337
parameters: [
3438
'name' => $name,
3539
'constraint' => $this->constraint,

tests/GreaterThanTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public static function provideRuleSuccessConditionData(): \Generator
5252
public static function provideRuleCustomMessageData(): \Generator
5353
{
5454
yield 'message' => [
55-
new GreaterThan(
56-
constraint: 10,
57-
message: 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
58-
), 1, 'The "test" value "1" is not greater than "10".'
55+
new GreaterThan(10, [
56+
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
57+
]),
58+
1,
59+
'The "test" value "1" is not greater than "10".'
5960
];
6061
}
6162
}

0 commit comments

Comments
 (0)