Conditions | 5 |
Paths | 5 |
Total Lines | 27 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 5 |
Changes | 0 |
1 | <?php |
||
23 | 8 | public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
|
24 | { |
||
25 | 8 | if (!$rule instanceof Callback) { |
|
26 | 1 | throw new UnexpectedRuleException(Callback::class, $rule); |
|
27 | } |
||
28 | |||
29 | 7 | $callback = $rule->getCallback(); |
|
30 | 7 | $callbackResult = $callback($value, $context); |
|
31 | |||
32 | 7 | if (!$callbackResult instanceof Result) { |
|
33 | 1 | throw new InvalidCallbackReturnTypeException($callbackResult); |
|
34 | } |
||
35 | |||
36 | 6 | $result = new Result(); |
|
37 | 6 | if ($callbackResult->isValid()) { |
|
38 | 1 | return $result; |
|
39 | } |
||
40 | |||
41 | 5 | foreach ($callbackResult->getErrors() as $error) { |
|
42 | 5 | $formattedMessage = $this->formatter->format( |
|
43 | 5 | $error->getMessage(), |
|
44 | 5 | ['attribute' => $context?->getAttribute(), 'value' => $value] |
|
45 | ); |
||
46 | 5 | $result->addError($formattedMessage, $error->getValuePath()); |
|
47 | } |
||
48 | |||
49 | 5 | return $result; |
|
50 | } |
||
52 |