-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractRequest.php
executable file
·243 lines (208 loc) · 6.27 KB
/
AbstractRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace Laravel\Foundation\Abstracts;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Illuminate\Http\Request;
use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidatesWhenResolvedTrait;
use Laravel\Foundation\Exceptions\ValidationException;
use Laravel\Foundation\Validation\Validator;
abstract class AbstractRequest extends Request implements ValidatesWhenResolved
{
use ValidatesWhenResolvedTrait;
/**Имя класс DTO
* @var string|null
*/
protected ?string $dtoClassName = null;
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'default';
/**
* The validator instance.
*
* @var \Illuminate\Contracts\Validation\Validator
*/
protected $validator;
/**
* Get the validated data from the request.
*
* @return array|AbstractDto
* @throws ValidationException
*/
public function validated(): array|AbstractDto
{
return $this->prepareValidatedData($this->getValidatorInstance()->validated());
}
public function all($keys = null)
{
//на фронте в объекте FormData пустой массив выглядит как массив с null внутри. Такое валидация не пропускает, поэтому фильтруем массивы
return array_map(function ($item) {
if (is_array($item) && array_is_list($item)) {
return array_filter($item, static fn($v) => !is_null($v));
}
return $item;
}, parent::all($keys));
}
protected function prepareValidatedData(array $data)
{
//если задан класс DTO, то возвращаем объект DTO, если нет, то массив
$dtoClass = $this->getDtoClassName();
if (!empty($dtoClass) && is_subclass_of($dtoClass, AbstractDto::class)) {
return new $dtoClass($data);
}
return $data;
}
protected function getDtoClassName(): ?string
{
return $this->dtoClassName;
}
/**
* Get the validator instance for the request.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance(): Validator
{
if ($this->validator) {
return $this->validator;
}
$factory = $this->container->make(ValidationFactory::class);
if (method_exists($this, 'validator')) {
$validator = $this->container->call([$this, 'validator'], compact('factory'));
} else {
$validator = $this->createDefaultValidator($factory);
}
if (method_exists($this, 'withValidator')) {
$this->withValidator($validator);
}
$this->setValidator($validator);
return $this->validator;
}
/**
* Create the default validator instance.
*
* @param \Illuminate\Contracts\Validation\Factory $factory
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function createDefaultValidator(Factory $factory): Validator
{
$validator = new Validator($factory->getTranslator(),
$this->validationData(), $this->container->call([$this, 'rules']),
$this->messages(), $this->attributes()
);
$validator->setPresenceVerifier($factory->getPresenceVerifier());
return $validator;
}
/**
* Get data to be validated from the request.
*
* @return array
*/
public function validationData(): array
{
return $this->all();
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages(): array
{
return [];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [];
}
/**
* Set the Validator instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return $this
*/
public function setValidator(Validator $validator): static
{
$this->validator = $validator;
return $this;
}
/**
* Set the container implementation.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return $this
*/
public function setContainer(Container $container): static
{
$this->container = $container;
return $this;
}
/**
* @param string $param
* @param null $default
* @return string|object|null
*/
public function getRouteParameter(string $param, $default = null): string|null|object
{
return $this->route()->parameter($param, $default);
}
public function sorted(bool $required = true): array
{
$isRequired = $this->isRequiredOrNullable($required);
return [
'sort' => "$isRequired|array",
'sort.*' => "$isRequired|string|in:asc,desc",
];
}
private function isRequiredOrNullable(bool $required): string
{
return $required ? 'required' : 'nullable';
}
public function paginated(bool $required = true): array
{
$isRequired = $this->isRequiredOrNullable($required);
return [
'page' => "$isRequired|integer",
'per_page' => "$isRequired|integer",
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws ValidationException
*/
protected function failedValidation(Validator $validator): void
{
throw new ValidationException($validator->errors()->messages());
}
/**
* Determine if the request passes the authorization check.
*
* @return bool
*/
protected function passesAuthorization()
{
if (method_exists($this, 'authorize')) {
return $this->container->call([$this, 'authorize']);
}
return true;
}
}