|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
|
8
|
|
|
use Yiisoft\Validator\Result; |
|
9
|
|
|
use Yiisoft\Validator\Rule; |
|
10
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
11
|
|
|
|
|
12
|
|
|
use function is_string; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* RegularExpressionValidator validates that the attribute value matches the pattern specified in constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* If the {@see MatchRegularExpression::not()} is used, the validator will ensure the attribute value do NOT match |
|
18
|
|
|
* the pattern. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class MatchRegularExpression extends Rule |
|
21
|
|
|
{ |
|
22
|
|
|
use HasValidationErrorMessage; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string the regular expression to be matched with |
|
26
|
|
|
*/ |
|
27
|
|
|
private string $pattern; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool whether to invert the validation logic. Defaults to false. If set to true, |
|
30
|
|
|
* the regular expression defined via {@see pattern} should NOT match the attribute value. |
|
31
|
|
|
*/ |
|
32
|
|
|
private bool $not = false; |
|
33
|
|
|
|
|
34
|
|
|
private string $incorrectInputMessage = 'Value should be string.'; |
|
35
|
|
|
private string $message = 'Value is invalid.'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $pattern The regular expression to be matched with. |
|
39
|
|
|
*/ |
|
40
|
8 |
|
public static function rule(string $pattern): self |
|
41
|
|
|
{ |
|
42
|
8 |
|
$rule = new self(); |
|
43
|
8 |
|
$rule->pattern = $pattern; |
|
44
|
8 |
|
return $rule; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
7 |
|
protected function validateValue($value, ValidationContext $context = null): Result |
|
48
|
|
|
{ |
|
49
|
7 |
|
$result = new Result(); |
|
50
|
|
|
|
|
51
|
7 |
|
if (!is_string($value)) { |
|
52
|
4 |
|
$result->addError($this->formatMessage($this->incorrectInputMessage)); |
|
53
|
|
|
|
|
54
|
4 |
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ( |
|
58
|
3 |
|
(!$this->not && !preg_match($this->pattern, $value)) || |
|
59
|
3 |
|
($this->not && preg_match($this->pattern, $value)) |
|
60
|
|
|
) { |
|
61
|
3 |
|
$result->addError($this->formatMessage($this->message)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
public function not(): self |
|
68
|
|
|
{ |
|
69
|
5 |
|
$new = clone $this; |
|
70
|
5 |
|
$new->not = true; |
|
71
|
5 |
|
return $new; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function incorrectInputMessage(string $message): self |
|
75
|
|
|
{ |
|
76
|
1 |
|
$new = clone $this; |
|
77
|
1 |
|
$new->incorrectInputMessage = $message; |
|
78
|
1 |
|
return $new; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function getOptions(): array |
|
82
|
|
|
{ |
|
83
|
2 |
|
return array_merge( |
|
84
|
2 |
|
parent::getOptions(), |
|
85
|
|
|
[ |
|
86
|
2 |
|
'message' => $this->formatMessage($this->message), |
|
87
|
2 |
|
'incorrectInputMessage' => $this->formatMessage($this->incorrectInputMessage), |
|
88
|
2 |
|
'not' => $this->not, |
|
89
|
2 |
|
'pattern' => $this->pattern, |
|
90
|
|
|
], |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|