1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Enjoys\Forms\Rule; |
||||
6 | |||||
7 | use Enjoys\Forms\Element; |
||||
8 | use Enjoys\Forms\Interfaces\Ruleable; |
||||
9 | use Enjoys\Forms\Traits\Request; |
||||
10 | |||||
11 | /** |
||||
12 | * @example |
||||
13 | * new Equal($message, ['expect']); or |
||||
14 | * new Equal($message, (array) 'expect'); or |
||||
15 | * new Equal($message, ['expect', 1, '255']); |
||||
16 | */ |
||||
17 | class Equal implements RuleInterface |
||||
18 | { |
||||
19 | use Request; |
||||
20 | |||||
21 | private string $message; |
||||
22 | private array $params; |
||||
23 | |||||
24 | |||||
25 | /** |
||||
26 | * @param array<array-key, scalar>|scalar $params |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
27 | * @param string|null $message |
||||
28 | */ |
||||
29 | 13 | public function __construct(mixed $params, ?string $message = null) |
|||
30 | { |
||||
31 | |||||
32 | 13 | $this->params = (array) $params; |
|||
33 | 13 | $this->message = $message ?? 'Допустимые значения (указаны через запятую): ' . \implode(', ', $this->params); |
|||
34 | } |
||||
35 | |||||
36 | |||||
37 | /** |
||||
38 | * @psalm-suppress PossiblyNullReference |
||||
39 | * @param Ruleable&Element $element |
||||
40 | * @return bool |
||||
41 | */ |
||||
42 | 13 | public function validate(Ruleable $element): bool |
|||
43 | { |
||||
44 | |||||
45 | 13 | $method = $this->getRequest()->getMethod(); |
|||
46 | /** @var array $requestData */ |
||||
47 | 13 | $requestData = match (strtolower($method)) { |
|||
48 | 13 | 'get' => $this->getRequest()->getQueryParams(), |
|||
49 | 13 | 'post' => $this->getRequest()->getParsedBody(), |
|||
50 | 13 | default => [] |
|||
51 | 13 | }; |
|||
52 | |||||
53 | /** @var mixed $value */ |
||||
54 | 13 | $value = \getValueByIndexPath($element->getName(), $requestData); |
|||
0 ignored issues
–
show
The method
getName() does not exist on Enjoys\Forms\Interfaces\Ruleable . Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
55 | |||||
56 | 13 | if (false === $this->check($value)) { |
|||
57 | 5 | $element->setRuleError($this->message); |
|||
58 | 5 | return false; |
|||
59 | } |
||||
60 | |||||
61 | 8 | return true; |
|||
62 | } |
||||
63 | |||||
64 | /** |
||||
65 | * @param mixed $value |
||||
66 | * @return array-key|bool |
||||
0 ignored issues
–
show
|
|||||
67 | * @noinspection PhpMissingReturnTypeInspection |
||||
68 | */ |
||||
69 | 13 | private function check(mixed $value) |
|||
70 | { |
||||
71 | |||||
72 | 13 | if ($value === false) { |
|||
73 | 1 | return true; |
|||
74 | } |
||||
75 | 12 | if (is_array($value)) { |
|||
76 | 4 | foreach ($value as $_val) { |
|||
77 | 4 | if (false === $this->check($_val)) { |
|||
78 | 1 | return false; |
|||
79 | } |
||||
80 | } |
||||
81 | 3 | return true; |
|||
82 | } |
||||
83 | |||||
84 | 12 | return array_search($value, $this->params); |
|||
85 | } |
||||
86 | } |
||||
87 |