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

Commit 8811013

Browse files
authored
Merge pull request #8 from programmatordev/YAPV-9-create-range-rule
Create Range rule (unfinished)
2 parents 5c2a8eb + 7f5ff90 commit 8811013

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
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/Choice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public function __construct(
2323
$resolver->setDefaults([
2424
'message' => 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
2525
'multipleMessage' => 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
26-
'maxMessage' => 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.',
27-
'minMessage' => 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.'
26+
'minMessage' => 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
27+
'maxMessage' => 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
2828
]);
2929

3030
$resolver->setAllowedTypes('message', 'string');
3131
$resolver->setAllowedTypes('multipleMessage', 'string');
32-
$resolver->setAllowedTypes('maxMessage', 'string');
3332
$resolver->setAllowedTypes('minMessage', 'string');
33+
$resolver->setAllowedTypes('maxMessage', 'string');
3434

3535
$this->options = $resolver->resolve($options);
3636
}

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)