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