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

Commit fc74a2c

Browse files
committed
feat: added GreaterThanEqualTo rule
1 parent 8811013 commit fc74a2c

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

src/ChainedValidatorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function notBlank(array $options = []): ChainedValidatorInterface;
1919

2020
public function greaterThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
2121

22+
public function greaterThanOrEqual(mixed $constraint, array $options = []): ChainedValidatorInterface;
23+
2224
public function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
2325

2426
public function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class GreaterThanOrEqualException extends ValidationException {}

src/Rule/GreaterThanOrEqual.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class GreaterThanOrEqual extends AbstractRule implements RuleInterface
11+
{
12+
use AssertIsComparableTrait;
13+
14+
private array $options;
15+
16+
public function __construct(private readonly mixed $constraint, array $options = [])
17+
{
18+
$resolver = new OptionsResolver();
19+
20+
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.']);
21+
22+
$resolver->setAllowedTypes('message', 'string');
23+
24+
$this->options = $resolver->resolve($options);
25+
}
26+
27+
/**
28+
* @throws GreaterThanOrEqualException
29+
*/
30+
public function assert(mixed $value, string $name): void
31+
{
32+
$this->assertIsComparable($this->constraint, $value);
33+
34+
if (!($value >= $this->constraint)) {
35+
throw new GreaterThanOrEqualException(
36+
message: $this->options['message'],
37+
parameters: [
38+
'value' => $value,
39+
'name' => $name,
40+
'constraint' => $this->constraint
41+
]
42+
);
43+
}
44+
}
45+
}

src/StaticValidatorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public static function notBlank(array $options = []): ChainedValidatorInterface;
88

99
public static function greaterThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
1010

11+
public static function greaterThanOrEqual(mixed $constraint, array $options = []): ChainedValidatorInterface;
12+
1113
public static function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
1214

1315
public static function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;

tests/GreaterThanOrEqualTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThanOrEqual;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
11+
12+
class GreaterThanOrEqualTest extends AbstractTest
13+
{
14+
use TestRuleUnexpectedValueTrait;
15+
use TestRuleFailureConditionTrait;
16+
use TestRuleSuccessConditionTrait;
17+
use TestRuleMessageOptionTrait;
18+
19+
public static function provideRuleUnexpectedValueData(): \Generator
20+
{
21+
$message = '/Cannot compare a type "(.*)" with a type "(.*)"/';
22+
23+
yield 'datetime constraint with int value' => [new GreaterThanOrEqual(new \DateTime()), 10, $message];
24+
yield 'datetime constraint with float value' => [new GreaterThanOrEqual(new \DateTime()), 1.0, $message];
25+
yield 'datetime constraint with string value' => [new GreaterThanOrEqual(new \DateTime()), 'a', $message];
26+
yield 'int constraint with string value' => [new GreaterThanOrEqual(10), 'a', $message];
27+
yield 'float constraint with string value' => [new GreaterThanOrEqual(1.0), 'a', $message];
28+
yield 'array constraint' => [new GreaterThanOrEqual([10]), 10, $message];
29+
yield 'null constraint' => [new GreaterThanOrEqual(null), 10, $message];
30+
}
31+
32+
public static function provideRuleFailureConditionData(): \Generator
33+
{
34+
$exception = GreaterThanOrEqualException::class;
35+
$message = '/The "(.*)" value should be greater than or equal to "(.*)", "(.*)" given./';
36+
37+
yield 'datetime' => [new GreaterThanOrEqual(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
38+
yield 'int' => [new GreaterThanOrEqual(10), 1, $exception, $message];
39+
yield 'float' => [new GreaterThanOrEqual(10.0), 1.0, $exception, $message];
40+
yield 'int with float' => [new GreaterThanOrEqual(10), 1.0, $exception, $message];
41+
yield 'string' => [new GreaterThanOrEqual('z'), 'a', $exception, $message];
42+
}
43+
44+
public static function provideRuleSuccessConditionData(): \Generator
45+
{
46+
yield 'datetime' => [new GreaterThanOrEqual(new \DateTime('today')), new \DateTime('tomorrow')];
47+
yield 'same datetime' => [new GreaterThanOrEqual(new \DateTime('today')), new \DateTime('today')];
48+
yield 'int' => [new GreaterThanOrEqual(10), 20];
49+
yield 'same int' => [new GreaterThanOrEqual(10), 10];
50+
yield 'float' => [new GreaterThanOrEqual(10.0), 20.0];
51+
yield 'same float' => [new GreaterThanOrEqual(10.0), 10.0];
52+
yield 'int with float' => [new GreaterThanOrEqual(10), 20.0];
53+
yield 'same int with float' => [new GreaterThanOrEqual(10), 10.0];
54+
yield 'string' => [new GreaterThanOrEqual('a'), 'z'];
55+
yield 'same string' => [new GreaterThanOrEqual('a'), 'a'];
56+
}
57+
58+
public static function provideRuleMessageOptionData(): \Generator
59+
{
60+
yield 'message' => [
61+
new GreaterThanOrEqual(
62+
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".'
67+
];
68+
}
69+
}

0 commit comments

Comments
 (0)