|
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\BeforeValidationInterface; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Validates that the value is of certain length. |
|
19
|
|
|
* |
|
20
|
|
|
* Note, this rule should only be used with strings. |
|
21
|
|
|
*/ |
|
22
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
|
23
|
|
|
final class HasLength implements ParametrizedRuleInterface, BeforeValidationInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use BeforeValidationTrait; |
|
26
|
|
|
use HandlerClassNameTrait; |
|
27
|
|
|
use RuleNameTrait; |
|
28
|
|
|
|
|
29
|
7 |
|
public function __construct( |
|
30
|
|
|
/** |
|
31
|
|
|
* @var int|null minimum length. null means no minimum length limit. |
|
32
|
|
|
* |
|
33
|
|
|
* @see $tooShortMessage for the customized message for a too short string. |
|
34
|
|
|
*/ |
|
35
|
|
|
private ?int $min = null, |
|
36
|
|
|
/** |
|
37
|
|
|
* @var int|null maximum length. null means no maximum length limit. |
|
38
|
|
|
* |
|
39
|
|
|
* @see $tooLongMessage for the customized message for a too long string. |
|
40
|
|
|
*/ |
|
41
|
|
|
private ?int $max = null, |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string user-defined error message used when the value is not a string. |
|
44
|
|
|
*/ |
|
45
|
|
|
private string $message = 'This value must be a string.', |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string user-defined error message used when the length of the value is smaller than {@see $min}. |
|
48
|
|
|
*/ |
|
49
|
|
|
private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.', |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string user-defined error message used when the length of the value is greater than {@see $max}. |
|
52
|
|
|
*/ |
|
53
|
|
|
private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.', |
|
54
|
|
|
/** |
|
55
|
|
|
* @var string the encoding of the string value to be validated (e.g. 'UTF-8'). |
|
56
|
|
|
* If this property is not set, application wide encoding will be used. |
|
57
|
|
|
*/ |
|
58
|
|
|
private string $encoding = 'UTF-8', |
|
59
|
|
|
private bool $skipOnEmpty = false, |
|
60
|
|
|
private bool $skipOnError = false, |
|
61
|
|
|
/** |
|
62
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
63
|
|
|
*/ |
|
64
|
|
|
private ?Closure $when = null |
|
65
|
|
|
) { |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return int|null |
|
70
|
|
|
*/ |
|
71
|
28 |
|
public function getMin(): ?int |
|
72
|
|
|
{ |
|
73
|
28 |
|
return $this->min; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return int|null |
|
78
|
|
|
*/ |
|
79
|
28 |
|
public function getMax(): ?int |
|
80
|
|
|
{ |
|
81
|
28 |
|
return $this->max; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function getMessage(): string |
|
88
|
|
|
{ |
|
89
|
6 |
|
return $this->message; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
5 |
|
public function getTooShortMessage(): string |
|
96
|
|
|
{ |
|
97
|
5 |
|
return $this->tooShortMessage; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function getTooLongMessage(): string |
|
104
|
|
|
{ |
|
105
|
4 |
|
return $this->tooLongMessage; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
28 |
|
public function getEncoding(): string |
|
112
|
|
|
{ |
|
113
|
28 |
|
return $this->encoding; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
5 |
|
#[ArrayShape([ |
|
117
|
|
|
'min' => 'int|null', |
|
118
|
|
|
'max' => 'int|null', |
|
119
|
|
|
'message' => 'string[]', |
|
120
|
|
|
'tooShortMessage' => 'array', |
|
121
|
|
|
'tooLongMessage' => 'array', |
|
122
|
|
|
'encoding' => 'string', |
|
123
|
|
|
'skipOnEmpty' => 'bool', |
|
124
|
|
|
'skipOnError' => 'bool', |
|
125
|
|
|
])] |
|
126
|
|
|
public function getOptions(): array |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
5 |
|
'min' => $this->min, |
|
130
|
5 |
|
'max' => $this->max, |
|
131
|
|
|
'message' => [ |
|
132
|
5 |
|
'message' => $this->message, |
|
133
|
|
|
], |
|
134
|
|
|
'tooShortMessage' => [ |
|
135
|
5 |
|
'message' => $this->tooShortMessage, |
|
136
|
5 |
|
'parameters' => ['min' => $this->min], |
|
137
|
|
|
], |
|
138
|
|
|
'tooLongMessage' => [ |
|
139
|
5 |
|
'message' => $this->tooLongMessage, |
|
140
|
5 |
|
'parameters' => ['max' => $this->max], |
|
141
|
|
|
], |
|
142
|
5 |
|
'encoding' => $this->encoding, |
|
143
|
5 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
|
144
|
5 |
|
'skipOnError' => $this->skipOnError, |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|