1 | <?php |
||||
2 | /** |
||||
3 | * @author Nurlan Mukhanov <[email protected]> |
||||
4 | * @copyright 2022 Nurlan Mukhanov |
||||
5 | * @license https://en.wikipedia.org/wiki/MIT_License MIT License |
||||
6 | * @link https://github.com/Falseclock/service-layer |
||||
7 | */ |
||||
8 | |||||
9 | declare(strict_types=1); |
||||
10 | |||||
11 | namespace Falseclock\Service; |
||||
12 | |||||
13 | use Falseclock\Service\Validation\ValidationProcess; |
||||
14 | use Falseclock\Service\Validation\ValidationProcessImpl; |
||||
15 | use Falseclock\Service\Validation\Validator; |
||||
16 | use Falseclock\Service\Validation\ValidatorError; |
||||
17 | use stdClass; |
||||
18 | |||||
19 | abstract class RequestImpl implements Request |
||||
20 | { |
||||
21 | /** @var ValidationProcess */ |
||||
22 | protected $validationProcess; |
||||
23 | |||||
24 | /** |
||||
25 | * Специально определяем конструктор, чтобы у нас везде в потомках конструктор был однотипный |
||||
26 | * |
||||
27 | * @param stdClass|null $stdClass $stdClass |
||||
28 | */ |
||||
29 | public function __construct(?stdClass $stdClass = null) |
||||
30 | { |
||||
31 | if (!is_null($stdClass)) { |
||||
32 | $this->parseObject($stdClass); |
||||
33 | } |
||||
34 | } |
||||
35 | |||||
36 | /** |
||||
37 | * @param $object |
||||
38 | */ |
||||
39 | private function parseObject($object) |
||||
40 | { |
||||
41 | foreach ($object as $key => $value) { |
||||
42 | if (property_exists($this, $key)) |
||||
43 | $this->$key = $value; |
||||
44 | } |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * @return ValidationProcess |
||||
49 | */ |
||||
50 | public function getValidationProcess(): ValidationProcess |
||||
51 | { |
||||
52 | return $this->validationProcess; |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @return bool |
||||
57 | */ |
||||
58 | final public function isValid(): bool |
||||
59 | { |
||||
60 | $this->validate(); |
||||
61 | |||||
62 | if (is_null($this->validationProcess)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
63 | return true; |
||||
64 | } |
||||
65 | |||||
66 | return count($this->validationProcess->getErrors()) == 0; |
||||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * Как правило, вызывается из сервиса перед тем, как начать оперировать запросом, |
||||
71 | * либо может быть вызван из юнит тестов или от куда-то еще |
||||
72 | * |
||||
73 | * @return ValidatorError[] |
||||
74 | * @see Service::validateRequest(); |
||||
75 | */ |
||||
76 | final public function validate(): array |
||||
77 | { |
||||
78 | // Обнуляем валидейшин процесс, так как гипотетически мы можем валидировать один и тот же реквест несколько раз |
||||
79 | $this->clearValidationProcess(); |
||||
80 | |||||
81 | // Инициируем валидаторы |
||||
82 | $this->initiateValidation(); |
||||
83 | |||||
84 | if (!is_null($this->validationProcess)) { |
||||
0 ignored issues
–
show
|
|||||
85 | return $this->validationProcess->validate(); |
||||
86 | } |
||||
87 | |||||
88 | return []; |
||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * @return $this |
||||
93 | */ |
||||
94 | public function clearValidationProcess(): RequestImpl |
||||
95 | { |
||||
96 | $this->validationProcess = null; |
||||
97 | |||||
98 | return $this; |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * @return ValidatorError[] |
||||
103 | */ |
||||
104 | public function getErrors(): array |
||||
105 | { |
||||
106 | if (is_null($this->validationProcess)) { |
||||
107 | return []; |
||||
108 | } |
||||
109 | return $this->validationProcess->getErrors(); |
||||
0 ignored issues
–
show
The method
getErrors() does not exist on Falseclock\Service\Validation\ValidationProcess . Since it exists in all sub-types, consider adding an abstract or default implementation to Falseclock\Service\Validation\ValidationProcess .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * @return false|string |
||||
114 | */ |
||||
115 | public function __toString() |
||||
116 | { |
||||
117 | return json_encode($this, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * Данный метод вызывается несколько раз на все поля в initValidators методе |
||||
122 | * |
||||
123 | * @param mixed $checkingValue |
||||
124 | * @param Validator ...$validators |
||||
125 | * |
||||
126 | * @see RequestImpl::initiateValidation() |
||||
127 | */ |
||||
128 | protected function addValidator($checkingValue, Validator ...$validators) |
||||
129 | { |
||||
130 | if (is_null($this->validationProcess)) { |
||||
131 | $this->validationProcess = new ValidationProcessImpl(); |
||||
132 | } |
||||
133 | |||||
134 | foreach ($validators as $validator) { |
||||
135 | $this->validationProcess->add($checkingValue, $validator); |
||||
136 | } |
||||
137 | } |
||||
138 | } |
||||
139 |