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

Commit 7f5ff90

Browse files
committed
chore: add Range unfinished
1 parent d750f73 commit 7f5ff90

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

src/ChainedValidatorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public function greaterThan(mixed $constraint, array $options = []): ChainedVali
2222
public function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
2323

2424
public function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
25+
26+
public function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
2527
}

src/Exception/RangeException.php

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 RangeException extends ValidationException {}

src/Rule/GreaterThan.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function __construct(private readonly mixed $constraint, array $options =
2828
*/
2929
public function assert(mixed $value, string $name): void
3030
{
31-
// Assert if constraint and value can be compared
3231
$this->assertIsComparable($this->constraint, $value);
3332

3433
if (!($value > $this->constraint)) {

src/Rule/LessThan.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function __construct(private readonly mixed $constraint, array $options =
2828
*/
2929
public function assert(mixed $value, string $name): void
3030
{
31-
// Assert if constraint and value can be compared
3231
$this->assertIsComparable($this->constraint, $value);
3332

3433
if (!($value < $this->constraint)) {

src/Rule/Range.php

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

src/StaticValidatorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public static function greaterThan(mixed $constraint, array $options = []): Chai
1111
public static function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
1212

1313
public static function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
14+
15+
public static function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
1416
}

0 commit comments

Comments
 (0)