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

Commit 2b3089a

Browse files
committed
feat: added EachKey rule
1 parent ed70a3d commit 2b3089a

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

src/ChainedValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function country(
2323
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
2424
): ChainedValidatorInterface&Validator;
2525

26+
public function eachKey(
27+
Validator $validator,
28+
string $message = '{{ message }}'
29+
): ChainedValidatorInterface&Validator;
30+
2631
public function eachValue(
2732
Validator $validator,
2833
string $message = 'At key "{{ key }}": {{ message }}'

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

src/Rule/EachKey.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\EachKeyException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
9+
10+
class EachKey extends AbstractRule implements RuleInterface
11+
{
12+
public function __construct(
13+
private readonly Validator $validator,
14+
private readonly string $message = '{{ 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 => $element) {
27+
$this->validator->assert($key, $name);
28+
}
29+
}
30+
catch (ValidationException $exception) {
31+
throw new EachKeyException(
32+
message: $this->message,
33+
parameters: [
34+
'value' => $value,
35+
'name' => $name,
36+
'key' => $key,
37+
'element' => $element,
38+
// Replaces string "value" with string "key" to get a more intuitive error message
39+
'message' => \preg_replace(
40+
\sprintf('/"(%s)" value/', $name),
41+
'"$1" key',
42+
$exception->getMessage()
43+
)
44+
]
45+
);
46+
}
47+
}
48+
}

src/StaticValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public static function country(
2222
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
2323
): ChainedValidatorInterface&Validator;
2424

25+
public static function eachKey(
26+
Validator $validator,
27+
string $message = '{{ message }}'
28+
): ChainedValidatorInterface&Validator;
29+
2530
public static function eachValue(
2631
Validator $validator,
2732
string $message = 'At key "{{ key }}": {{ message }}'

0 commit comments

Comments
 (0)