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

Commit 2a686e8

Browse files
committed
chore: changed All rule to EachValue with Validator as an argument
1 parent 5acd74b commit 2a686e8

File tree

3 files changed

+20
-58
lines changed

3 files changed

+20
-58
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
44

5-
class AllException extends ValidationException {}
5+
class EachValueException extends ValidationException {}
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachValueException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ValidatableTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
99

10-
class All extends AbstractRule implements RuleInterface
10+
class EachValue extends AbstractRule implements RuleInterface
1111
{
12-
use ValidatableTrait;
13-
14-
/**
15-
* @param RuleInterface[] $constraints
16-
*/
1712
public function __construct(
18-
private readonly array $constraints,
19-
private readonly string $message = 'At "{{ key }}": {{ message }}'
13+
private readonly Validator $validator,
14+
private readonly string $message = 'At key "{{ key }}": {{ message }}'
2015
) {}
2116

2217
public function assert(mixed $value, string $name): void
2318
{
24-
if (!$this->isValidatable($this->constraints)) {
25-
throw new UnexpectedValueException(
26-
'All constraints must be of type "RuleInterface".'
27-
);
28-
}
29-
3019
if (!\is_array($value)) {
3120
throw new UnexpectedValueException(
3221
\sprintf('Expected value of type "array", "%s" given.', get_debug_type($value))
@@ -35,13 +24,11 @@ public function assert(mixed $value, string $name): void
3524

3625
try {
3726
foreach ($value as $key => $input) {
38-
foreach ($this->constraints as $constraint) {
39-
$constraint->assert($input, $name);
40-
}
27+
$this->validator->assert($input, $name);
4128
}
4229
}
4330
catch (ValidationException $exception) {
44-
throw new AllException(
31+
throw new EachValueException(
4532
message: $this->message,
4633
parameters: [
4734
'value' => $value,
Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\All;
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\EachValue;
77
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
88
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
@@ -12,7 +12,7 @@
1212
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
1313
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1414

15-
class AllTest extends AbstractTest
15+
class EachValueTest extends AbstractTest
1616
{
1717
use TestRuleUnexpectedValueTrait;
1818
use TestRuleFailureConditionTrait;
@@ -21,36 +21,25 @@ class AllTest extends AbstractTest
2121

2222
public static function provideRuleUnexpectedValueData(): \Generator
2323
{
24-
yield 'invalid constraint' => [
25-
new All([new NotBlank(), 'invalid']),
26-
[1, 2, 3],
27-
'/All constraints must be of type "RuleInterface"./'
28-
];
2924
yield 'invalid value type' => [
30-
new All([new NotBlank()]),
25+
new EachValue(new Validator(new NotBlank())),
3126
'invalid',
3227
'/Expected value of type "array", "(.*)" given./'
3328
];
3429
yield 'unexpected value propagation' => [
35-
new All([new GreaterThan(10)]),
30+
new EachValue(new Validator(new GreaterThan(10))),
3631
['a'],
3732
'/Cannot compare a type "(.*)" with a type "(.*)"./'
3833
];
3934
}
4035

4136
public static function provideRuleFailureConditionData(): \Generator
4237
{
43-
$exception = AllException::class;
44-
$message = '/At "(.*)": The "(.*)" value should not be blank, "(.*)" given./';
38+
$exception = EachValueException::class;
39+
$message = '/At key "(.*)": The "(.*)" value should not be blank, "(.*)" given./';
4540

4641
yield 'constraint' => [
47-
new All([new NotBlank()]),
48-
[1, 2, ''],
49-
$exception,
50-
$message
51-
];
52-
yield 'validator' => [
53-
new All([(new Validator(new NotBlank()))]),
42+
new EachValue(new Validator(new NotBlank())),
5443
[1, 2, ''],
5544
$exception,
5645
$message
@@ -59,31 +48,17 @@ public static function provideRuleFailureConditionData(): \Generator
5948

6049
public static function provideRuleSuccessConditionData(): \Generator
6150
{
62-
yield 'constraints' => [
63-
new All([new NotBlank(), new GreaterThan(1)]),
64-
[2, 3, 4]
65-
];
66-
yield 'validators' => [
67-
new All([
68-
(new Validator(new NotBlank())),
69-
(new Validator(new GreaterThan(1)))
70-
]),
71-
[2, 3, 4]
72-
];
73-
yield 'constraints and validators' => [
74-
new All([
75-
new NotBlank(),
76-
(new Validator(new GreaterThan(1)))
77-
]),
51+
yield 'constraint' => [
52+
new EachValue(new Validator(new NotBlank(), new GreaterThan(1))),
7853
[2, 3, 4]
7954
];
8055
}
8156

8257
public static function provideRuleMessageOptionData(): \Generator
8358
{
8459
yield 'constraint' => [
85-
new All(
86-
constraints: [new NotBlank()],
60+
new EachValue(
61+
validator: new Validator(new NotBlank()),
8762
message: 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}".'
8863
),
8964
[1, 2, ''],

0 commit comments

Comments
 (0)