Conditions | 7 |
Paths | 4 |
Total Lines | 25 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 7 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
21 | 19 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
22 | { |
||
23 | 19 | if (!$rule instanceof Regex) { |
|
24 | 1 | throw new UnexpectedRuleException(Regex::class, $rule); |
|
25 | } |
||
26 | |||
27 | 18 | $result = new Result(); |
|
28 | 18 | if (!is_string($value)) { |
|
29 | 9 | return $result->addError($rule->getIncorrectInputMessage(), [ |
|
30 | 9 | 'attribute' => $context->getTranslatedAttribute(), |
|
31 | 9 | 'type' => get_debug_type($value), |
|
32 | ]); |
||
33 | } |
||
34 | |||
35 | if ( |
||
36 | 9 | (!$rule->isNot() && !preg_match($rule->getPattern(), $value)) || |
|
37 | 9 | ($rule->isNot() && preg_match($rule->getPattern(), $value)) |
|
38 | ) { |
||
39 | 6 | $result->addError($rule->getMessage(), [ |
|
40 | 6 | 'attribute' => $context->getTranslatedAttribute(), |
|
41 | 'value' => $value, |
||
42 | ]); |
||
43 | } |
||
44 | |||
45 | 9 | return $result; |
|
46 | } |
||
48 |