| Conditions | 5 |
| Paths | 5 |
| Total Lines | 29 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 13 |
| CRAP Score | 5.5359 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | 10 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
| 17 | { |
||
| 18 | 10 | if (!$rule instanceof Subset) { |
|
| 19 | 1 | throw new UnexpectedRuleException(Subset::class, $rule); |
|
| 20 | } |
||
| 21 | |||
| 22 | 9 | $result = new Result(); |
|
| 23 | |||
| 24 | 9 | if (!is_iterable($value)) { |
|
| 25 | $result->addError( |
||
| 26 | message: $rule->getIterableMessage(), |
||
| 27 | parameters: ['value' => $value] |
||
| 28 | ); |
||
| 29 | return $result; |
||
| 30 | } |
||
| 31 | |||
| 32 | 9 | if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) { |
|
| 33 | 3 | $values = $rule->getValues() instanceof Traversable |
|
| 34 | ? iterator_to_array($rule->getValues()) |
||
| 35 | 3 | : $rule->getValues(); |
|
| 36 | 3 | $valuesString = '"' . implode('", "', $values) . '"'; |
|
|
|
|||
| 37 | |||
| 38 | 3 | $result->addError( |
|
| 39 | 3 | message: $rule->getSubsetMessage(), |
|
| 40 | 3 | parameters: ['value' => $value, 'values' => $valuesString] |
|
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | 9 | return $result; |
|
| 45 | } |
||
| 47 |