1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
12
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validates that the value matches the pattern specified in constructor. |
16
|
|
|
* |
17
|
|
|
* If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern. |
18
|
|
|
*/ |
19
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
20
|
|
|
final class Regex implements ParametrizedRuleInterface |
21
|
|
|
{ |
22
|
|
|
use HandlerClassNameTrait; |
23
|
|
|
use RuleNameTrait; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
/** |
27
|
|
|
* @var string the regular expression to be matched with |
28
|
|
|
*/ |
29
|
|
|
private string $pattern, |
30
|
|
|
/** |
31
|
|
|
* @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular |
32
|
|
|
* expression defined via {@see $pattern} should NOT match the value. |
33
|
|
|
*/ |
34
|
|
|
private bool $not = false, |
35
|
|
|
private string $incorrectInputMessage = 'Value should be string.', |
36
|
|
|
private string $message = 'Value is invalid.', |
37
|
|
|
private bool $skipOnEmpty = false, |
38
|
|
|
private bool $skipOnError = false, |
39
|
|
|
private ?Closure $when = null, |
40
|
|
|
) { |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
6 |
|
public function getPattern(): string |
47
|
|
|
{ |
48
|
6 |
|
return $this->pattern; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
6 |
|
public function isNot(): bool |
55
|
|
|
{ |
56
|
6 |
|
return $this->not; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
7 |
|
public function getIncorrectInputMessage(): string |
63
|
|
|
{ |
64
|
7 |
|
return $this->incorrectInputMessage; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
3 |
|
public function getMessage(): string |
71
|
|
|
{ |
72
|
3 |
|
return $this->message; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isSkipOnEmpty(): bool |
79
|
|
|
{ |
80
|
|
|
return $this->skipOnEmpty; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isSkipOnError(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->skipOnError; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Closure|null |
93
|
|
|
*/ |
94
|
|
|
public function getWhen(): ?Closure |
95
|
|
|
{ |
96
|
|
|
return $this->when; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
#[ArrayShape([ |
100
|
|
|
'pattern' => 'string', |
101
|
|
|
'not' => 'bool', |
102
|
|
|
'incorrectInputMessage' => 'string[]', |
103
|
|
|
'message' => 'string[]', |
104
|
|
|
'skipOnEmpty' => 'bool', |
105
|
|
|
'skipOnError' => 'bool', |
106
|
|
|
])] |
107
|
|
|
public function getOptions(): array |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
2 |
|
'pattern' => $this->pattern, |
111
|
2 |
|
'not' => $this->not, |
112
|
|
|
'incorrectInputMessage' => [ |
113
|
2 |
|
'message' => $this->incorrectInputMessage, |
114
|
|
|
], |
115
|
|
|
'message' => [ |
116
|
2 |
|
'message' => $this->message, |
117
|
|
|
], |
118
|
2 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
119
|
2 |
|
'skipOnError' => $this->skipOnError, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|