|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Forms\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Forms\Element; |
|
8
|
|
|
use Enjoys\Forms\Exception\ExceptionRule; |
|
9
|
|
|
use Enjoys\Forms\Interfaces\Ruleable; |
|
10
|
|
|
use Enjoys\Forms\Traits\Request; |
|
11
|
|
|
|
|
12
|
|
|
class Length implements RuleInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use Request; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private array $operatorToMethodTranslation = [ |
|
20
|
|
|
'==' => 'equal', |
|
21
|
|
|
'!=' => 'notEqual', |
|
22
|
|
|
'>' => 'greaterThan', |
|
23
|
|
|
'<' => 'lessThan', |
|
24
|
|
|
'>=' => 'greaterThanOrEqual', |
|
25
|
|
|
'<=' => 'lessThanOrEqual', |
|
26
|
|
|
]; |
|
27
|
|
|
private string $message; |
|
28
|
|
|
private array $params; |
|
29
|
|
|
|
|
30
|
66 |
|
public function __construct(array $params, ?string $message = null) |
|
31
|
|
|
{ |
|
32
|
66 |
|
$this->message = $message ?? 'Ошибка ввода'; |
|
33
|
66 |
|
$this->params = $params; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @psalm-suppress PossiblyNullReference |
|
40
|
|
|
* @param Ruleable&Element $element |
|
41
|
|
|
* @return bool |
|
42
|
|
|
* @throws ExceptionRule |
|
43
|
|
|
*/ |
|
44
|
17 |
|
public function validate(Ruleable $element): bool |
|
45
|
|
|
{ |
|
46
|
17 |
|
$method = $this->getRequest()->getMethod(); |
|
47
|
|
|
|
|
48
|
|
|
/** @var array $requestData */ |
|
49
|
17 |
|
$requestData = match (strtolower($method)) { |
|
50
|
17 |
|
'get' => $this->getRequest()->getQueryParams(), |
|
51
|
17 |
|
'post' => $this->getRequest()->getParsedBody(), |
|
52
|
17 |
|
default => [] |
|
53
|
17 |
|
}; |
|
54
|
|
|
|
|
55
|
|
|
/** @var string|int|array|false $value */ |
|
56
|
17 |
|
$value = \getValueByIndexPath($element->getName(), $requestData); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
17 |
|
if (!$this->check($value)) { |
|
59
|
6 |
|
$element->setRuleError($this->message); |
|
60
|
6 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
11 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string|int|array|false $value |
|
69
|
|
|
* @return bool |
|
70
|
|
|
* @throws ExceptionRule |
|
71
|
|
|
*/ |
|
72
|
66 |
|
private function check(string|int|array|false $value): bool |
|
73
|
|
|
{ |
|
74
|
66 |
|
if (is_array($value) || $value === false) { |
|
|
|
|
|
|
75
|
7 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
59 |
|
$length = \mb_strlen(\trim((string)$value), 'UTF-8'); |
|
79
|
59 |
|
if (empty($value)) { |
|
80
|
7 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @var string $operator */ |
|
84
|
|
|
/** @var int|string $threshold */ |
|
85
|
52 |
|
foreach ($this->params as $operator => $threshold) { |
|
86
|
52 |
|
$method = $this->operatorToMethodTranslation[$operator] ?? 'unknown'; |
|
87
|
|
|
|
|
88
|
52 |
|
if (!method_exists(Length::class, $method)) { |
|
89
|
1 |
|
throw new ExceptionRule('Unknown Compare Operator.'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
51 |
|
if (!$this->$method($length, $threshold)) { |
|
93
|
22 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
29 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $value |
|
103
|
|
|
* @param int|string $threshold |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
7 |
|
private function equal(int $value, int|string $threshold): bool |
|
107
|
|
|
{ |
|
108
|
7 |
|
return $value == $threshold; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $value |
|
114
|
|
|
* @param int|string $threshold |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
14 |
|
private function notEqual(int $value, int|string $threshold): bool |
|
118
|
|
|
{ |
|
119
|
14 |
|
return $value != $threshold; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* |
|
124
|
|
|
* @param int $value |
|
125
|
|
|
* @param int|string $threshold |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
8 |
|
private function greaterThan(int $value, int|string $threshold): bool |
|
129
|
|
|
{ |
|
130
|
8 |
|
return $value > $threshold; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* |
|
135
|
|
|
* @param int $value |
|
136
|
|
|
* @param int|string $threshold |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
8 |
|
private function lessThan(int $value, int|string $threshold): bool |
|
140
|
|
|
{ |
|
141
|
8 |
|
return $value < $threshold; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* |
|
146
|
|
|
* @param int $value |
|
147
|
|
|
* @param int|string $threshold |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
7 |
|
private function greaterThanOrEqual(int $value, int|string $threshold): bool |
|
151
|
|
|
{ |
|
152
|
7 |
|
return $value >= $threshold; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* |
|
157
|
|
|
* @param int $value |
|
158
|
|
|
* @param int|string $threshold |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
7 |
|
private function lessThanOrEqual(int $value, int|string $threshold): bool |
|
162
|
|
|
{ |
|
163
|
7 |
|
return $value <= $threshold; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|