Conditions | 5 |
Paths | 5 |
Total Lines | 21 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 10 |
CRAP Score | 5.1158 |
Changes | 0 |
1 | <?php |
||
15 | 10 | public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
|
16 | { |
||
17 | 10 | if (!$rule instanceof Subset) { |
|
18 | 1 | throw new UnexpectedRuleException(Subset::class, $rule); |
|
19 | } |
||
20 | |||
21 | 9 | $result = new Result(); |
|
22 | |||
23 | 9 | if (!is_iterable($value)) { |
|
24 | $result->addError($rule->iterableMessage); |
||
25 | return $result; |
||
26 | } |
||
27 | |||
28 | 9 | if (!ArrayHelper::isSubset($value, $rule->values, $rule->strict)) { |
|
29 | 3 | $values = is_array($rule->values) ? $rule->values : iterator_to_array($rule->values); |
|
30 | 3 | $valuesString = '"' . implode('", "', $values) . '"'; |
|
31 | |||
32 | 3 | $result->addError($rule->subsetMessage, ['values' => $valuesString]); |
|
33 | } |
||
34 | |||
35 | 9 | return $result; |
|
36 | } |
||
38 |