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