| Conditions | 5 |
| Paths | 5 |
| Total Lines | 35 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 13 |
| CRAP Score | 5.7873 |
| 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 | $rule->getIterableMessage(), |
||
| 27 | [ |
||
| 28 | 'attribute' => $context->getAttribute(), |
||
| 29 | 'value' => (string) $value, |
||
| 30 | ], |
||
| 31 | ); |
||
| 32 | return $result; |
||
| 33 | } |
||
| 34 | |||
| 35 | 9 | if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) { |
|
| 36 | 3 | $values = $rule->getValues() instanceof Traversable |
|
| 37 | ? iterator_to_array($rule->getValues()) |
||
| 38 | 3 | : $rule->getValues(); |
|
| 39 | 3 | $valuesString = '"' . implode('", "', $values) . '"'; |
|
|
|
|||
| 40 | |||
| 41 | 3 | $result->addError( |
|
| 42 | 3 | $rule->getSubsetMessage(), |
|
| 43 | [ |
||
| 44 | 3 | 'attribute' => $context->getAttribute(), |
|
| 45 | 'values' => $valuesString, |
||
| 46 | ], |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | 9 | return $result; |
|
| 51 | } |
||
| 53 |