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()->getRequest()->getMethod(); |
47
|
17 |
|
$requestData = match (strtolower($method)) { |
48
|
17 |
|
'get' => $this->getRequest()->getQueryData()->toArray(), |
49
|
|
|
'post' => $this->getRequest()->getPostData()->toArray(), |
50
|
|
|
default => [] |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
/** @var string|int|array|false $value */ |
54
|
17 |
|
$value = \getValueByIndexPath($element->getName(), $requestData); |
|
|
|
|
55
|
|
|
|
56
|
17 |
|
if (!$this->check($value)) { |
57
|
6 |
|
$element->setRuleError($this->message); |
58
|
6 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
11 |
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|int|array|false $value |
67
|
|
|
* @return bool |
68
|
|
|
* @throws ExceptionRule |
69
|
|
|
*/ |
70
|
66 |
|
private function check(string|int|array|false $value): bool |
71
|
|
|
{ |
72
|
66 |
|
if (is_array($value) || $value === false) { |
|
|
|
|
73
|
7 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
59 |
|
$length = \mb_strlen(\trim((string)$value), 'UTF-8'); |
77
|
59 |
|
if (empty($value)) { |
78
|
7 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @var string $operator */ |
82
|
|
|
/** @var int|string $threshold */ |
83
|
52 |
|
foreach ($this->params as $operator => $threshold) { |
84
|
52 |
|
$method = $this->operatorToMethodTranslation[$operator] ?? 'unknown'; |
85
|
|
|
|
86
|
52 |
|
if (!method_exists(Length::class, $method)) { |
87
|
1 |
|
throw new ExceptionRule('Unknown Compare Operator.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
51 |
|
if (!$this->$method($length, $threshold)) { |
91
|
22 |
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
29 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @param int $value |
101
|
|
|
* @param int|string $threshold |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
7 |
|
private function equal(int $value, int|string $threshold): bool |
105
|
|
|
{ |
106
|
7 |
|
return $value == $threshold; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @param int $value |
112
|
|
|
* @param int|string $threshold |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
14 |
|
private function notEqual(int $value, int|string $threshold): bool |
116
|
|
|
{ |
117
|
14 |
|
return $value != $threshold; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @param int $value |
123
|
|
|
* @param int|string $threshold |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
8 |
|
private function greaterThan(int $value, int|string $threshold): bool |
127
|
|
|
{ |
128
|
8 |
|
return $value > $threshold; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* |
133
|
|
|
* @param int $value |
134
|
|
|
* @param int|string $threshold |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
8 |
|
private function lessThan(int $value, int|string $threshold): bool |
138
|
|
|
{ |
139
|
8 |
|
return $value < $threshold; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @param int $value |
145
|
|
|
* @param int|string $threshold |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
7 |
|
private function greaterThanOrEqual(int $value, int|string $threshold): bool |
149
|
|
|
{ |
150
|
7 |
|
return $value >= $threshold; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* |
155
|
|
|
* @param int $value |
156
|
|
|
* @param int|string $threshold |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
7 |
|
private function lessThanOrEqual(int $value, int|string $threshold): bool |
160
|
|
|
{ |
161
|
7 |
|
return $value <= $threshold; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|