| Conditions | 6 |
| Paths | 5 |
| Total Lines | 29 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 24 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
||
| 25 | { |
||
| 26 | if (!$rule instanceof OneOf) { |
||
| 27 | throw new UnexpectedRuleException(OneOf::class, $rule); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** @var mixed $value */ |
||
| 31 | $value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value; |
||
| 32 | |||
| 33 | $result = new Result(); |
||
| 34 | |||
| 35 | if (!is_array($value) && !is_object($value)) { |
||
| 36 | return $result->addError($rule->getIncorrectInputMessage(), [ |
||
| 37 | 'attribute' => $context->getTranslatedAttribute(), |
||
| 38 | 'type' => get_debug_type($value), |
||
| 39 | ]); |
||
| 40 | } |
||
| 41 | |||
| 42 | foreach ($rule->getAttributes() as $attribute) { |
||
| 43 | if (!(new WhenEmpty())(ArrayHelper::getValue($value, $attribute), $context->isAttributeMissing())) { |
||
| 44 | return $result; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | $result->addError($rule->getMessage(), [ |
||
| 49 | 'attribute' => $context->getTranslatedAttribute(), |
||
| 50 | ]); |
||
| 51 | |||
| 52 | return $result; |
||
| 53 | } |
||
| 55 |