| Conditions | 9 |
| Paths | 15 |
| Total Lines | 29 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 17 |
| CRAP Score | 9.0139 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 18 | 35 | public function validate($value, Constraint $constraint) |
|
| 19 | { |
||
| 20 | 35 | if (!$constraint instanceof Closure) { |
|
| 21 | throw new UnexpectedTypeException($constraint, Closure::class); |
||
| 22 | } |
||
| 23 | |||
| 24 | 35 | $element = $this->context->getRoot() instanceof WeakReference ? $this->context->getRoot()->get() : null; |
|
| 25 | /** @psalm-suppress PossiblyNullArgument */ |
||
| 26 | 35 | $error = ($constraint->callback)($value, $element, $this->context); |
|
| 27 | 35 | $code = null; |
|
| 28 | |||
| 29 | 35 | if ($error === true) { |
|
| 30 | 2 | return; |
|
| 31 | } |
||
| 32 | |||
| 33 | 34 | if ($error === false) { |
|
| 34 | 6 | $error = $constraint->message; |
|
| 35 | } |
||
| 36 | |||
| 37 | 34 | if ($error) { |
|
| 38 | 33 | if (is_array($error)) { |
|
| 39 | 3 | $code = $error['code'] ?? null; |
|
| 40 | 3 | $error = $error['message'] ?? null; |
|
| 41 | } |
||
| 42 | |||
| 43 | 33 | $this->context->buildViolation($error ?: $constraint->message) |
|
| 44 | 33 | ->setParameter('{{ value }}', $this->formatValue($value)) |
|
| 45 | 33 | ->setCode($code ?: 'CUSTOM_ERROR') |
|
| 46 | 33 | ->addViolation() |
|
| 47 | ; |
||
| 51 |