|
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
|
|
|
trait LimitTrait |
|
11
|
|
|
{ |
|
12
|
|
|
private ?int $min = null; |
|
13
|
|
|
private ?int $max = null; |
|
14
|
|
|
private ?int $exactly = null; |
|
15
|
|
|
private string $lessThanMinMessage; |
|
16
|
|
|
private string $greaterThanMaxMessage; |
|
17
|
|
|
private string $notExactlyMessage; |
|
18
|
|
|
|
|
19
|
12 |
|
private function initLimitProperties( |
|
20
|
|
|
?int $min, |
|
21
|
|
|
?int $max, |
|
22
|
|
|
?int $exactly, |
|
23
|
|
|
string $lessThanMinMessage, |
|
24
|
|
|
string $greaterThanMinMessage, |
|
25
|
|
|
string $notExactlyMessage |
|
26
|
|
|
): void { |
|
27
|
12 |
|
$this->min = $min; |
|
28
|
12 |
|
$this->max = $max; |
|
29
|
12 |
|
$this->exactly = $exactly; |
|
30
|
12 |
|
$this->lessThanMinMessage = $lessThanMinMessage; |
|
31
|
12 |
|
$this->greaterThanMaxMessage = $greaterThanMinMessage; |
|
32
|
12 |
|
$this->notExactlyMessage = $notExactlyMessage; |
|
33
|
|
|
|
|
34
|
12 |
|
if (!$this->min && !$this->max && !$this->exactly) { |
|
|
|
|
|
|
35
|
1 |
|
throw new InvalidArgumentException( |
|
36
|
|
|
'At least one of these attributes must be specified: $min, $max, $exactly.' |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
if ($this->exactly && ($this->min || $this->max)) { |
|
|
|
|
|
|
41
|
3 |
|
throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
if ($this->min && $this->max && $this->min === $this->max) { |
|
|
|
|
|
|
45
|
1 |
|
throw new InvalidArgumentException('Use $exactly instead.'); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return int|null |
|
51
|
|
|
*/ |
|
52
|
40 |
|
public function getMin(): ?int |
|
53
|
|
|
{ |
|
54
|
40 |
|
return $this->min; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int|null |
|
59
|
|
|
*/ |
|
60
|
40 |
|
public function getMax(): ?int |
|
61
|
|
|
{ |
|
62
|
40 |
|
return $this->max; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return int|null |
|
67
|
|
|
*/ |
|
68
|
43 |
|
public function getExactly(): ?int |
|
69
|
|
|
{ |
|
70
|
43 |
|
return $this->exactly; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
13 |
|
public function getLessThanMinMessage(): string |
|
77
|
|
|
{ |
|
78
|
13 |
|
return $this->lessThanMinMessage; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function getGreaterThanMaxMessage(): string |
|
85
|
|
|
{ |
|
86
|
4 |
|
return $this->greaterThanMaxMessage; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function getNotExactlyMessage(): string |
|
93
|
|
|
{ |
|
94
|
3 |
|
return $this->notExactlyMessage; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
#[ArrayShape([ |
|
98
|
|
|
'min' => 'int|null', |
|
99
|
|
|
'max' => 'int|null', |
|
100
|
|
|
'exactly' => 'int|null', |
|
101
|
|
|
'lessThanMinMessage' => 'array', |
|
102
|
|
|
'greaterThanMaxMessage' => 'array', |
|
103
|
|
|
'notExactlyMessage' => 'array', |
|
104
|
|
|
])] |
|
105
|
|
|
private function getLimitOptions(): array |
|
106
|
|
|
{ |
|
107
|
|
|
return [ |
|
108
|
4 |
|
'min' => $this->min, |
|
109
|
4 |
|
'max' => $this->max, |
|
110
|
4 |
|
'exactly' => $this->exactly, |
|
111
|
|
|
'lessThanMinMessage' => [ |
|
112
|
4 |
|
'message' => $this->lessThanMinMessage, |
|
113
|
4 |
|
'parameters' => ['min' => $this->min], |
|
114
|
|
|
], |
|
115
|
|
|
'greaterThanMaxMessage' => [ |
|
116
|
4 |
|
'message' => $this->greaterThanMaxMessage, |
|
117
|
4 |
|
'parameters' => ['max' => $this->max], |
|
118
|
|
|
], |
|
119
|
|
|
'notExactlyMessage' => [ |
|
120
|
4 |
|
'message' => $this->notExactlyMessage, |
|
121
|
4 |
|
'parameters' => ['exactly' => $this->exactly], |
|
122
|
|
|
], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: