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

Commit 96c2f3b

Browse files
authored
Merge pull request #39 from programmatordev/1.x
1.x
2 parents 3ae68bf + 0e29447 commit 96c2f3b

15 files changed

+361
-198
lines changed

docs/03-rules.md

Lines changed: 4 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,7 @@
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-each-value.md)
34+
- [EachKey](03x-rules-each-key.md)

docs/03x-rules-all.md

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

docs/03x-rules-each-key.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## EachKey
2+
3+
Validates every key of an `array` or object implementing `\Traversable` with a given set of rules.
4+
5+
```php
6+
EachKey(
7+
Validator $validator,
8+
string $message = 'Invalid key: {{ message }}'
9+
);
10+
```
11+
12+
## Basic Usage
13+
14+
```php
15+
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 'green' => '#0f0']); // true
16+
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 1 => '#0f0']); // 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 key of an `array` or object implementing `\Traversable`.
29+
30+
### `message`
31+
32+
type: `string` default: `Invalid key: {{ message }}`
33+
34+
Message that will be shown if at least one input value key is invalid according to the given `validator`.
35+
36+
```php
37+
Validator::eachKey(Validator::notBlank())->assert(['red' => '#f00', 1 => '#0f0'], 'Test');
38+
// Throws: Invalid key: The "Test" key should be of type "string", "1" 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+
| `{{ element }}` | The value of the invalid iterable element |
49+
| `{{ message }}` | The rule message of the invalid iterable element |

docs/03x-rules-each-value.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
| `{{ element }}` | The value of the invalid iterable element |
49+
| `{{ message }}` | The rule message of the invalid iterable element |

src/ChainedValidatorInterface.php

Lines changed: 23 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,66 @@ 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 eachKey(
27+
Validator $validator,
28+
string $message = 'Invalid key: {{ message }}'
29+
): ChainedValidatorInterface&Validator;
30+
31+
public function eachValue(
32+
Validator $validator,
33+
string $message = 'At key "{{ key }}": {{ message }}'
34+
): ChainedValidatorInterface&Validator;
3935

4036
public function greaterThan(
4137
mixed $constraint,
4238
string $message = 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.'
43-
): ChainedValidatorInterface;
39+
): ChainedValidatorInterface&Validator;
4440

4541
public function greaterThanOrEqual(
4642
mixed $constraint,
4743
string $message = 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.'
48-
): ChainedValidatorInterface;
44+
): ChainedValidatorInterface&Validator;
4945

5046
public function lessThan(
5147
mixed $constraint,
5248
string $message = 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.'
53-
): ChainedValidatorInterface;
49+
): ChainedValidatorInterface&Validator;
5450

5551
public function lessThanOrEqual(
5652
mixed $constraint,
5753
string $message = 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.'
58-
): ChainedValidatorInterface;
54+
): ChainedValidatorInterface&Validator;
5955

6056
public function notBlank(
6157
?callable $normalizer = null,
6258
string $message = 'The "{{ name }}" value should not be blank, "{{ value }}" given.'
63-
): ChainedValidatorInterface;
59+
): ChainedValidatorInterface&Validator;
6460

6561
public function range(
6662
mixed $minConstraint,
6763
mixed $maxConstraint,
6864
string $message = 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.'
69-
): ChainedValidatorInterface;
65+
): ChainedValidatorInterface&Validator;
7066

71-
public function rule(RuleInterface $constraint): ChainedValidatorInterface;
67+
public function rule(
68+
RuleInterface $constraint
69+
): ChainedValidatorInterface&Validator;
7270

7371
public function timezone(
7472
int $timezoneGroup = \DateTimeZone::ALL,
7573
?string $countryCode = null,
7674
string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.'
77-
): ChainedValidatorInterface;
75+
): ChainedValidatorInterface&Validator;
7876

7977
public function type(
8078
string|array $constraint,
8179
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'
82-
): ChainedValidatorInterface;
80+
): ChainedValidatorInterface&Validator;
8381
}

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

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

src/Rule/All.php

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

0 commit comments

Comments
 (0)