1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\Regex; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use Yiisoft\Validator\Rule\RuleNameTrait; |
10
|
|
|
use Yiisoft\Validator\Rule\HandlerClassNameTrait; |
11
|
|
|
use Yiisoft\Validator\RuleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Validates that the value matches the pattern specified in constructor. |
15
|
|
|
* |
16
|
|
|
* If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern. |
17
|
|
|
*/ |
18
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
19
|
|
|
final class Regex implements RuleInterface |
20
|
|
|
{ |
21
|
|
|
use HandlerClassNameTrait; |
22
|
|
|
use RuleNameTrait; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
/** |
26
|
|
|
* @var string the regular expression to be matched with |
27
|
|
|
*/ |
28
|
|
|
public string $pattern, |
29
|
|
|
/** |
30
|
|
|
* @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular |
31
|
|
|
* expression defined via {@see $pattern} should NOT match the value. |
32
|
|
|
*/ |
33
|
|
|
public bool $not = false, |
34
|
|
|
public string $incorrectInputMessage = 'Value should be string.', |
35
|
|
|
public string $message = 'Value is invalid.', |
36
|
|
|
public bool $skipOnEmpty = false, |
37
|
|
|
public bool $skipOnError = false, |
38
|
|
|
public ?Closure $when = null, |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function getOptions(): array |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
2 |
|
'pattern' => $this->pattern, |
46
|
2 |
|
'not' => $this->not, |
47
|
|
|
'incorrectInputMessage' => [ |
48
|
2 |
|
'message' => $this->incorrectInputMessage, |
49
|
|
|
], |
50
|
|
|
'message' => [ |
51
|
2 |
|
'message' => $this->message, |
52
|
|
|
], |
53
|
2 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
54
|
2 |
|
'skipOnError' => $this->skipOnError, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|