Conditions | 5 |
Paths | 5 |
Total Lines | 29 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 5 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
26 | 16 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
27 | { |
||
28 | 16 | if (!$rule instanceof IsTrue) { |
|
29 | 1 | throw new UnexpectedRuleException(IsTrue::class, $rule); |
|
30 | } |
||
31 | |||
32 | 15 | if ($rule->isStrict()) { |
|
33 | 7 | $valid = $value === $rule->getTrueValue(); |
|
34 | } else { |
||
35 | 8 | $valid = $value == $rule->getTrueValue(); |
|
36 | } |
||
37 | |||
38 | 15 | $result = new Result(); |
|
39 | |||
40 | 15 | if ($valid) { |
|
41 | 4 | return $result; |
|
42 | } |
||
43 | |||
44 | 11 | $formattedMessage = $this->formatter->format( |
|
45 | 11 | $rule->getMessage(), |
|
46 | [ |
||
47 | 11 | 'true' => $rule->getTrueValue() === true ? '1' : $rule->getTrueValue(), |
|
48 | 11 | 'attribute' => $context->getAttribute(), |
|
49 | 'value' => $value, |
||
50 | ] |
||
51 | ); |
||
52 | 11 | $result->addError($formattedMessage); |
|
53 | |||
54 | 11 | return $result; |
|
55 | } |
||
57 |