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

Commit fe76321

Browse files
authored
Merge pull request #37 from programmatordev/YAPV-51-change-all-rule-name-to-eachvalue
Change All rule name to EachValue
2 parents 5acd74b + 7a3971a commit fe76321

File tree

11 files changed

+167
-198
lines changed

11 files changed

+167
-198
lines changed

docs/03-rules.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- [Comparison Rules](#comparison-rules)
55
- [Date Rules](#date-rules)
66
- [Choice Rules](#choice-rules)
7-
- [Other Rules](#other-rules)
7+
- [Iterable Rules](#iterable-rules)
88

99
## Basic Rules
1010

@@ -28,6 +28,6 @@
2828
- [Choice](03x-rules-choice.md)
2929
- [Country](03x-rules-country.md)
3030

31-
## Other Rules
31+
## Iterable Rules
3232

33-
- [All](03x-rules-all.md)
33+
- [EachValue](03x-rules-eachvalue.md)

docs/03x-rules-all.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

docs/03x-rules-eachvalue.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## EachValue
2+
3+
Validates every element of an `array` or object implementing `\Traversable` with a given set of rules.
4+
5+
```php
6+
EachValue(
7+
Validator $validator,
8+
string $message = 'At key "{{ key }}": {{ message }}'
9+
);
10+
```
11+
12+
## Basic Usage
13+
14+
```php
15+
Validator::eachValue(Validator::notBlank()->greaterThan(1)->lessThan(10))->validate([4, 5, 6]); // true
16+
Validator::eachValue(Validator::notBlank()->greaterThan(1)->lessThan(10))->validate([4, 5, 20]); // false
17+
```
18+
19+
> **Note**
20+
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Traversable`.
21+
22+
## Options
23+
24+
### `validator`
25+
26+
type: `Validator` `required`
27+
28+
Validator that will validate each element of an `array` or object implementing `\Traversable`.
29+
30+
### `message`
31+
32+
type: `string` default: `At key "{{ key }}": {{ message }}`
33+
34+
Message that will be shown if at least one input value element is invalid according to the given `validator`.
35+
36+
```php
37+
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'Test');
38+
// Throws: At key "2": The "Test" value should not be blank, "" given.
39+
```
40+
41+
The following parameters are available:
42+
43+
| Parameter | Description |
44+
|-----------------|--------------------------------------------------|
45+
| `{{ value }}` | The current invalid value |
46+
| `{{ name }}` | Name of the invalid value |
47+
| `{{ key }}` | The key of the invalid iterable element |
48+
| `{{ message }}` | The rule message of the invalid iterable element |

src/ChainedValidatorInterface.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@
77

88
interface ChainedValidatorInterface
99
{
10-
/**
11-
* @throws ValidationException
12-
*/
13-
public function assert(mixed $value, string $name): void;
14-
15-
public function validate(mixed $value): bool;
16-
17-
// --- Rules ---
18-
19-
public function all(
20-
array $constraints,
21-
string $message = 'At "{{ key }}": {{ message }}'
22-
): ChainedValidatorInterface;
23-
2410
public function choice(
2511
array $constraints,
2612
bool $multiple = false,
@@ -30,54 +16,61 @@ public function choice(
3016
string $multipleMessage = 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
3117
string $minMessage = 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
3218
string $maxMessage = 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
33-
): ChainedValidatorInterface;
19+
): ChainedValidatorInterface&Validator;
3420

3521
public function country(
3622
string $code = 'alpha-2',
3723
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
38-
): ChainedValidatorInterface;
24+
): ChainedValidatorInterface&Validator;
25+
26+
public function eachValue(
27+
Validator $validator,
28+
string $message = 'At key "{{ key }}": {{ message }}'
29+
): ChainedValidatorInterface&Validator;
3930

4031
public function greaterThan(
4132
mixed $constraint,
4233
string $message = 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.'
43-
): ChainedValidatorInterface;
34+
): ChainedValidatorInterface&Validator;
4435

4536
public function greaterThanOrEqual(
4637
mixed $constraint,
4738
string $message = 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.'
48-
): ChainedValidatorInterface;
39+
): ChainedValidatorInterface&Validator;
4940

5041
public function lessThan(
5142
mixed $constraint,
5243
string $message = 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.'
53-
): ChainedValidatorInterface;
44+
): ChainedValidatorInterface&Validator;
5445

5546
public function lessThanOrEqual(
5647
mixed $constraint,
5748
string $message = 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.'
58-
): ChainedValidatorInterface;
49+
): ChainedValidatorInterface&Validator;
5950

6051
public function notBlank(
6152
?callable $normalizer = null,
6253
string $message = 'The "{{ name }}" value should not be blank, "{{ value }}" given.'
63-
): ChainedValidatorInterface;
54+
): ChainedValidatorInterface&Validator;
6455

6556
public function range(
6657
mixed $minConstraint,
6758
mixed $maxConstraint,
6859
string $message = 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.'
69-
): ChainedValidatorInterface;
60+
): ChainedValidatorInterface&Validator;
7061

71-
public function rule(RuleInterface $constraint): ChainedValidatorInterface;
62+
public function rule(
63+
RuleInterface $constraint
64+
): ChainedValidatorInterface&Validator;
7265

7366
public function timezone(
7467
int $timezoneGroup = \DateTimeZone::ALL,
7568
?string $countryCode = null,
7669
string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.'
77-
): ChainedValidatorInterface;
70+
): ChainedValidatorInterface&Validator;
7871

7972
public function type(
8073
string|array $constraint,
8174
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'
82-
): ChainedValidatorInterface;
75+
): ChainedValidatorInterface&Validator;
8376
}

src/Exception/AllException.php renamed to src/Exception/EachValueException.php

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 {}

src/Rule/All.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Rule/EachValue.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
9+
10+
class EachValue extends AbstractRule implements RuleInterface
11+
{
12+
public function __construct(
13+
private readonly Validator $validator,
14+
private readonly string $message = 'At key "{{ key }}": {{ message }}'
15+
) {}
16+
17+
public function assert(mixed $value, string $name): void
18+
{
19+
if (!\is_iterable($value)) {
20+
throw new UnexpectedValueException(
21+
\sprintf('Expected value of type "array|\Traversable", "%s" given.', get_debug_type($value))
22+
);
23+
}
24+
25+
try {
26+
foreach ($value as $key => $input) {
27+
$this->validator->assert($input, $name);
28+
}
29+
}
30+
catch (ValidationException $exception) {
31+
throw new EachValueException(
32+
message: $this->message,
33+
parameters: [
34+
'value' => $value,
35+
'name' => $name,
36+
'key' => $key,
37+
'message' => $exception->getMessage()
38+
]
39+
);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)