1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\Trait; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A trait attachable to a rule with limits. |
12
|
|
|
* |
13
|
|
|
* @see LimitHandlerTrait |
14
|
|
|
*/ |
15
|
|
|
trait LimitTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var int|null Lower limit. |
19
|
|
|
* |
20
|
|
|
* @see $lessThanMinMessage for according error message. Can't be combined with {@see $exactly}. |
21
|
|
|
*/ |
22
|
|
|
private ?int $min = null; |
23
|
|
|
/** |
24
|
|
|
* @var int|null Upper limit. |
25
|
|
|
* |
26
|
|
|
* @see $greaterThanMaxMessage for according error message. Can't be combined with {@see $exactly}. |
27
|
|
|
*/ |
28
|
|
|
private ?int $max = null; |
29
|
|
|
/** |
30
|
|
|
* @var int|null Exact number (lower and upper limit match). |
31
|
|
|
* |
32
|
|
|
* @see $notExactlyMessage for according error message. `null` means no strict comparison. Mutually exclusive with |
33
|
|
|
* {@see $min} and {@see $max}. |
34
|
|
|
*/ |
35
|
|
|
private ?int $exactly = null; |
36
|
|
|
/** |
37
|
|
|
* @var string Message used when validated value is lower than {@see $min}. |
38
|
|
|
*/ |
39
|
|
|
private string $lessThanMinMessage; |
40
|
|
|
/** |
41
|
|
|
* @var string Message used when validated value is greater than {@see $max}. |
42
|
|
|
*/ |
43
|
|
|
private string $greaterThanMaxMessage; |
44
|
|
|
/** |
45
|
|
|
* @var string Message used when validated value doesn't exactly match {@see $exactly}. |
46
|
|
|
*/ |
47
|
|
|
private string $notExactlyMessage; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes limit related properties and runs checks for required and mutually exclusive properties. |
51
|
|
|
* |
52
|
|
|
* @param int|null $min {@see $min} |
53
|
|
|
* @param int|null $max {@see $max} |
54
|
|
|
* @param int|null $exactly {@see $exactly} |
55
|
|
|
* @param string $lessThanMinMessage {@see $lessThanMinMessage} |
56
|
|
|
* @param string $greaterThanMinMessage {@see $greaterThanMinMessage} |
57
|
|
|
* @param string $notExactlyMessage {@see $notExactlyMessage} |
58
|
|
|
*/ |
59
|
46 |
|
private function initLimitProperties( |
60
|
|
|
?int $min, |
61
|
|
|
?int $max, |
62
|
|
|
?int $exactly, |
63
|
|
|
string $lessThanMinMessage, |
64
|
|
|
string $greaterThanMinMessage, |
65
|
|
|
string $notExactlyMessage |
66
|
|
|
): void { |
67
|
46 |
|
$this->min = $min; |
68
|
46 |
|
$this->max = $max; |
69
|
46 |
|
$this->exactly = $exactly; |
70
|
46 |
|
$this->lessThanMinMessage = $lessThanMinMessage; |
71
|
46 |
|
$this->greaterThanMaxMessage = $greaterThanMinMessage; |
72
|
46 |
|
$this->notExactlyMessage = $notExactlyMessage; |
73
|
|
|
|
74
|
46 |
|
if ($this->min === null && $this->max === null && $this->exactly === null) { |
75
|
2 |
|
throw new InvalidArgumentException( |
76
|
|
|
'At least one of these attributes must be specified: $min, $max, $exactly.' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
44 |
|
if (($this->min !== null || $this->max !== null) && $this->exactly !== null) { |
81
|
6 |
|
throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ( |
85
|
38 |
|
($this->min !== null && $this->min <= 0) || |
86
|
28 |
|
($this->max !== null && $this->max <= 0) || |
87
|
38 |
|
($this->exactly !== null && $this->exactly <= 0) |
88
|
|
|
) { |
89
|
18 |
|
throw new InvalidArgumentException('Only positive values are allowed.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
20 |
|
if ($this->min !== null && $this->max !== null) { |
93
|
4 |
|
if ($this->min > $this->max) { |
94
|
2 |
|
throw new InvalidArgumentException('$min must be lower than $max.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
if ($this->min === $this->max) { |
98
|
2 |
|
throw new InvalidArgumentException('Use $exactly instead.'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets lower limit. |
105
|
|
|
* |
106
|
|
|
* @return int|null {@see $min} |
107
|
|
|
*/ |
108
|
78 |
|
public function getMin(): ?int |
109
|
|
|
{ |
110
|
78 |
|
return $this->min; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets upper limit. |
115
|
|
|
* |
116
|
|
|
* @return int|null {@see $max} |
117
|
|
|
*/ |
118
|
78 |
|
public function getMax(): ?int |
119
|
|
|
{ |
120
|
78 |
|
return $this->max; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Gets "exactly" value. |
125
|
|
|
* |
126
|
|
|
* @return int|null {@see $exactly} |
127
|
|
|
*/ |
128
|
81 |
|
public function getExactly(): ?int |
129
|
|
|
{ |
130
|
81 |
|
return $this->exactly; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets "less than lower limit" error message. |
135
|
|
|
* |
136
|
|
|
* @return string {@see $lessThanMinMessage} |
137
|
|
|
*/ |
138
|
42 |
|
public function getLessThanMinMessage(): string |
139
|
|
|
{ |
140
|
42 |
|
return $this->lessThanMinMessage; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets "greater than upper limit" error message. |
145
|
|
|
* |
146
|
|
|
* @return string {@see $greaterThanMaxMessage} |
147
|
|
|
*/ |
148
|
6 |
|
public function getGreaterThanMaxMessage(): string |
149
|
|
|
{ |
150
|
6 |
|
return $this->greaterThanMaxMessage; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Gets "does not match exactly" error message. |
155
|
|
|
* |
156
|
|
|
* @return string {@see notExactlyMessage} |
157
|
|
|
*/ |
158
|
3 |
|
public function getNotExactlyMessage(): string |
159
|
|
|
{ |
160
|
3 |
|
return $this->notExactlyMessage; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Limit related options intended to be merged with other rule options. |
165
|
|
|
*/ |
166
|
5 |
|
#[ArrayShape([ |
167
|
|
|
'min' => 'int|null', |
168
|
|
|
'max' => 'int|null', |
169
|
|
|
'exactly' => 'int|null', |
170
|
|
|
'lessThanMinMessage' => 'array', |
171
|
|
|
'greaterThanMaxMessage' => 'array', |
172
|
|
|
'notExactlyMessage' => 'array', |
173
|
|
|
])] |
174
|
|
|
private function getLimitOptions(): array |
175
|
|
|
{ |
176
|
|
|
return [ |
177
|
5 |
|
'min' => $this->min, |
178
|
5 |
|
'max' => $this->max, |
179
|
5 |
|
'exactly' => $this->exactly, |
180
|
|
|
'lessThanMinMessage' => [ |
181
|
5 |
|
'message' => $this->lessThanMinMessage, |
182
|
5 |
|
'parameters' => ['min' => $this->min], |
183
|
|
|
], |
184
|
|
|
'greaterThanMaxMessage' => [ |
185
|
5 |
|
'message' => $this->greaterThanMaxMessage, |
186
|
5 |
|
'parameters' => ['max' => $this->max], |
187
|
|
|
], |
188
|
|
|
'notExactlyMessage' => [ |
189
|
5 |
|
'message' => $this->notExactlyMessage, |
190
|
5 |
|
'parameters' => ['exactly' => $this->exactly], |
191
|
|
|
], |
192
|
|
|
]; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|