| Conditions | 7 |
| Paths | 4 |
| Total Lines | 34 |
| Code Lines | 17 |
| 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 | $rule->getIncorrectInputMessage(), |
|
| 32 | [ |
||
| 33 | 7 | 'attribute' => $context->getAttribute(), |
|
| 34 | 'value' => $value, |
||
| 35 | ], |
||
| 36 | ); |
||
| 37 | |||
| 38 | 7 | return $result; |
|
| 39 | } |
||
| 40 | |||
| 41 | if ( |
||
| 42 | 7 | (!$rule->isNot() && !preg_match($rule->getPattern(), $value)) || |
|
| 43 | 7 | ($rule->isNot() && preg_match($rule->getPattern(), $value)) |
|
| 44 | ) { |
||
| 45 | 4 | $result->addError( |
|
| 46 | 4 | $rule->getMessage(), |
|
| 47 | [ |
||
| 48 | 4 | 'attribute' => $context->getAttribute(), |
|
| 49 | 'value' => $value, |
||
| 50 | ], |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | 7 | return $result; |
|
| 55 | } |
||
| 57 |