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

Commit cd8f1b9

Browse files
committed
feat: added All rule
1 parent 9d0acb8 commit cd8f1b9

File tree

6 files changed

+99
-5
lines changed

6 files changed

+99
-5
lines changed

src/ChainedValidatorInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public function validate(mixed $value): bool;
1515

1616
// --- Rules ---
1717

18-
public function notBlank(array $options = []): ChainedValidatorInterface;
18+
public function all(array $constraints, array $options = null): ChainedValidatorInterface;
19+
20+
public function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
1921

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

@@ -25,7 +27,7 @@ public function lessThan(mixed $constraint, array $options = []): ChainedValidat
2527

2628
public function lessThanOrEqual(mixed $constraint, array $options = []): ChainedValidatorInterface;
2729

28-
public function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
30+
public function notBlank(array $options = []): ChainedValidatorInterface;
2931

3032
public function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
3133
}

src/Exception/AllException.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 AllException extends ValidationException {}

src/Rule/All.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsValidatableTrait;
9+
use Symfony\Component\OptionsResolver\OptionsResolver;
10+
11+
class All extends AbstractRule implements RuleInterface
12+
{
13+
use AssertIsValidatableTrait;
14+
15+
private array $options;
16+
17+
/**
18+
* @param RuleInterface[] $constraints
19+
*/
20+
public function __construct(
21+
private readonly array $constraints,
22+
array $options = []
23+
)
24+
{
25+
$resolver = new OptionsResolver();
26+
27+
$resolver->setDefaults(['message' => 'At "{{ key }}": {{ message }}']);
28+
29+
$resolver->setAllowedTypes('message', 'string');
30+
31+
$this->options = $resolver->resolve($options);
32+
}
33+
34+
public function assert(mixed $value, string $name): void
35+
{
36+
$this->assertIsValidatable($this->constraints);
37+
38+
if (!\is_array($value)) {
39+
throw new UnexpectedValueException(
40+
\sprintf('Expected value of type "array", "%s" given', get_debug_type($value))
41+
);
42+
}
43+
44+
try {
45+
foreach ($value as $key => $input) {
46+
foreach ($this->constraints as $constraint) {
47+
$constraint->assert($input, $name);
48+
}
49+
}
50+
}
51+
catch (ValidationException $exception) {
52+
throw new AllException(
53+
message: $this->options['message'],
54+
parameters: [
55+
'value' => $value,
56+
'name' => $name,
57+
'key' => $key,
58+
'message' => $exception->getMessage()
59+
]
60+
);
61+
}
62+
}
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule\Util;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
7+
8+
trait AssertIsValidatableTrait
9+
{
10+
private function assertIsValidatable(array $constraints): bool
11+
{
12+
foreach ($constraints as $constraint) {
13+
if (!$constraint instanceof RuleInterface) {
14+
throw new UnexpectedValueException(
15+
\sprintf('Expected constraint of type "RuleInterface", "%s" given.', get_debug_type($constraint))
16+
);
17+
}
18+
}
19+
20+
return true;
21+
}
22+
}

src/StaticValidatorInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
interface StaticValidatorInterface
66
{
7-
public static function notBlank(array $options = []): ChainedValidatorInterface;
7+
public static function all(array $constraints, array $options = null): ChainedValidatorInterface;
8+
9+
public static function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
810

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

@@ -14,7 +16,7 @@ public static function lessThan(mixed $constraint, array $options = []): Chained
1416

1517
public static function lessThanOrEqual(mixed $constraint, array $options = []): ChainedValidatorInterface;
1618

17-
public static function choice(array $constraints, bool $multiple = false, ?int $minConstraint = null, ?int $maxConstraint = null, array $options = []): ChainedValidatorInterface;
19+
public static function notBlank(array $options = []): ChainedValidatorInterface;
1820

1921
public static function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
2022
}

src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @mixin StaticValidatorInterface
1212
*/
13-
class Validator
13+
class Validator implements RuleInterface
1414
{
1515
private array $rules;
1616

0 commit comments

Comments
 (0)