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