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

Commit 09f3dbd

Browse files
committed
chore: improved rule by allowing values that implement the Traversable interface
1 parent 2a686e8 commit 09f3dbd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Rule/EachValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function __construct(
1616

1717
public function assert(mixed $value, string $name): void
1818
{
19-
if (!\is_array($value)) {
19+
if (!\is_iterable($value)) {
2020
throw new UnexpectedValueException(
21-
\sprintf('Expected value of type "array", "%s" given.', get_debug_type($value))
21+
\sprintf('Expected value of type "array|\Traversable", "%s" given.', get_debug_type($value))
2222
);
2323
}
2424

tests/EachValueTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
2424
yield 'invalid value type' => [
2525
new EachValue(new Validator(new NotBlank())),
2626
'invalid',
27-
'/Expected value of type "array", "(.*)" given./'
27+
'/Expected value of type "(.*)", "(.*)" given./'
2828
];
2929
yield 'unexpected value propagation' => [
3030
new EachValue(new Validator(new GreaterThan(10))),
@@ -38,25 +38,35 @@ public static function provideRuleFailureConditionData(): \Generator
3838
$exception = EachValueException::class;
3939
$message = '/At key "(.*)": The "(.*)" value should not be blank, "(.*)" given./';
4040

41-
yield 'constraint' => [
41+
yield 'invalid array element' => [
4242
new EachValue(new Validator(new NotBlank())),
4343
[1, 2, ''],
4444
$exception,
4545
$message
4646
];
47+
yield 'invalid traversable element' => [
48+
new EachValue(new Validator(new NotBlank())),
49+
new \ArrayIterator([1, 2, '']),
50+
$exception,
51+
$message
52+
];
4753
}
4854

4955
public static function provideRuleSuccessConditionData(): \Generator
5056
{
51-
yield 'constraint' => [
57+
yield 'array element' => [
5258
new EachValue(new Validator(new NotBlank(), new GreaterThan(1))),
5359
[2, 3, 4]
5460
];
61+
yield 'traversable element' => [
62+
new EachValue(new Validator(new NotBlank(), new GreaterThan(1))),
63+
new \ArrayIterator([2, 3, 4])
64+
];
5565
}
5666

5767
public static function provideRuleMessageOptionData(): \Generator
5868
{
59-
yield 'constraint' => [
69+
yield 'message' => [
6070
new EachValue(
6171
validator: new Validator(new NotBlank()),
6272
message: 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}".'

0 commit comments

Comments
 (0)