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
|
17 |
|
private function initLimitProperties( |
60
|
|
|
?int $min, |
61
|
|
|
?int $max, |
62
|
|
|
?int $exactly, |
63
|
|
|
string $lessThanMinMessage, |
64
|
|
|
string $greaterThanMinMessage, |
65
|
|
|
string $notExactlyMessage |
66
|
|
|
): void { |
67
|
17 |
|
$this->min = $min; |
68
|
17 |
|
$this->max = $max; |
69
|
17 |
|
$this->exactly = $exactly; |
70
|
17 |
|
$this->lessThanMinMessage = $lessThanMinMessage; |
71
|
17 |
|
$this->greaterThanMaxMessage = $greaterThanMinMessage; |
72
|
17 |
|
$this->notExactlyMessage = $notExactlyMessage; |
73
|
|
|
|
74
|
17 |
|
if (!$this->min && !$this->max && !$this->exactly) { |
|
|
|
|
75
|
2 |
|
throw new InvalidArgumentException( |
76
|
|
|
'At least one of these attributes must be specified: $min, $max, $exactly.' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
15 |
|
if ($this->exactly && ($this->min || $this->max)) { |
|
|
|
|
81
|
6 |
|
throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
if ($this->min && $this->max && $this->min === $this->max) { |
|
|
|
|
85
|
2 |
|
throw new InvalidArgumentException('Use $exactly instead.'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets lower limit. |
91
|
|
|
* |
92
|
|
|
* @return int|null {@see $min} |
93
|
|
|
*/ |
94
|
40 |
|
public function getMin(): ?int |
95
|
|
|
{ |
96
|
40 |
|
return $this->min; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets upper limit. |
101
|
|
|
* |
102
|
|
|
* @return int|null {@see $max} |
103
|
|
|
*/ |
104
|
40 |
|
public function getMax(): ?int |
105
|
|
|
{ |
106
|
40 |
|
return $this->max; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets "exactly" value. |
111
|
|
|
* |
112
|
|
|
* @return int|null {@see $exactly} |
113
|
|
|
*/ |
114
|
43 |
|
public function getExactly(): ?int |
115
|
|
|
{ |
116
|
43 |
|
return $this->exactly; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets "less than lower limit" error message. |
121
|
|
|
* |
122
|
|
|
* @return string {@see $lessThanMinMessage} |
123
|
|
|
*/ |
124
|
13 |
|
public function getLessThanMinMessage(): string |
125
|
|
|
{ |
126
|
13 |
|
return $this->lessThanMinMessage; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets "greater than upper limit" error message. |
131
|
|
|
* |
132
|
|
|
* @return string {@see $greaterThanMaxMessage} |
133
|
|
|
*/ |
134
|
4 |
|
public function getGreaterThanMaxMessage(): string |
135
|
|
|
{ |
136
|
4 |
|
return $this->greaterThanMaxMessage; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets "does not match exactly" error message. |
141
|
|
|
* |
142
|
|
|
* @return string {@see notExactlyMessage} |
143
|
|
|
*/ |
144
|
3 |
|
public function getNotExactlyMessage(): string |
145
|
|
|
{ |
146
|
3 |
|
return $this->notExactlyMessage; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Limit related options intended to be merged with other rule options. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
4 |
|
#[ArrayShape([ |
155
|
|
|
'min' => 'int|null', |
156
|
|
|
'max' => 'int|null', |
157
|
|
|
'exactly' => 'int|null', |
158
|
|
|
'lessThanMinMessage' => 'array', |
159
|
|
|
'greaterThanMaxMessage' => 'array', |
160
|
|
|
'notExactlyMessage' => 'array', |
161
|
|
|
])] |
162
|
|
|
private function getLimitOptions(): array |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
4 |
|
'min' => $this->min, |
166
|
4 |
|
'max' => $this->max, |
167
|
4 |
|
'exactly' => $this->exactly, |
168
|
|
|
'lessThanMinMessage' => [ |
169
|
4 |
|
'message' => $this->lessThanMinMessage, |
170
|
4 |
|
'parameters' => ['min' => $this->min], |
171
|
|
|
], |
172
|
|
|
'greaterThanMaxMessage' => [ |
173
|
4 |
|
'message' => $this->greaterThanMaxMessage, |
174
|
4 |
|
'parameters' => ['max' => $this->max], |
175
|
|
|
], |
176
|
|
|
'notExactlyMessage' => [ |
177
|
4 |
|
'message' => $this->notExactlyMessage, |
178
|
4 |
|
'parameters' => ['exactly' => $this->exactly], |
179
|
|
|
], |
180
|
|
|
]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: