| Total Complexity | 4 |
| Total Lines | 55 |
| Duplicated Lines | 0 % |
| Coverage | 66.67% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | class Regex implements RuleInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array Rule properties |
||
| 21 | */ |
||
| 22 | public static $config = [ |
||
| 23 | 'class' => 'Regex', |
||
| 24 | 'full_class' => __CLASS__, |
||
| 25 | 'alias' => ['regex', 'rex', 'rx'], |
||
| 26 | 'args_count' => 1, |
||
| 27 | 'args_type' => ['string'], |
||
| 28 | 'has_validate' => true, |
||
| 29 | 'has_sanitize' => false |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string Error message |
||
| 34 | */ |
||
| 35 | private $message = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Validate. |
||
| 39 | * |
||
| 40 | * @param string $received |
||
| 41 | * @param string $regex |
||
| 42 | * |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | 2 | public function validate(string $received, string $regex): bool |
|
| 46 | { |
||
| 47 | 2 | $matches = []; |
|
| 48 | |||
| 49 | 2 | $result = preg_match($regex, $received, $matches); |
|
| 50 | |||
| 51 | 2 | if ($result === 0) { |
|
| 52 | 1 | $this->message = "Received value must match regex {$regex}"; |
|
| 53 | 1 | return true; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | if ($result === false) { |
|
|
|
|||
| 57 | $this->message = "Invalid regex provided {$regex}"; |
||
| 58 | return true; |
||
| 59 | } |
||
| 60 | |||
| 61 | 1 | return false; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Return error message. |
||
| 66 | * |
||
| 67 | * @return string Error message |
||
| 68 | */ |
||
| 69 | public function getMessage(): string |
||
| 72 | } |
||
| 73 | } |
||
| 74 |