Newer
Older
use App\Shared\Dto\DtoCollection;
use App\Shared\Dto\ValidateErrorDto;
use App\Shared\DtoFactory\ValidateErrorDtoFactory;
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
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/** Абстракный реквест, необходим удобной валидации */
abstract class AbstractRequest
{
/**
* активация автовалидации
* перезаписать на false для отключение автовалидации
*/
protected const AUTO_VALIDATE = true;
public function __construct(protected ValidatorInterface $validator)
{
$this->populate();
if (self::AUTO_VALIDATE) {
$this->validate();
}
}
/**
* Маппинг реквеста
* @return void
*/
protected function populate(): void
{
foreach ($this->getRequest()->toArray() as $property => $value) {
if (property_exists($this, $property)) {
$this->{$property} = $value;
}
}
}
/**
* Валидация и выброкса ошибки при валидации
* @return void
*/
public function validate(): void
{
$errors = $this->validator->validate($this);
$messages = new DtoCollection(ValidateErrorDto::class);
foreach ($errors as $error) {
$messages->add((new ValidateErrorDtoFactory($error))->create());
}
if ($messages->count() > 0) {
$response = new JsonResponse($messages, 422);
$response->send();
throw new ValidatorException('Validation failed', $messages);
}
}
/**
* Возвращает HttpFoundation реквест
* @return Request
*/
public function getRequest(): Request
{
return Request::createFromGlobals();
}
}