Прокинул теги в валидатор

master
Rinsvent 3 years ago
parent 0d805314f1
commit ccd530eeaa
  1. 29
      src/EventListener/RequestListener.php

@ -31,17 +31,26 @@ class RequestListener
} }
$methodExtractor = new MethodExtractor($controller[0], $controller[1]); $methodExtractor = new MethodExtractor($controller[0], $controller[1]);
$tags = [
'default',
'request',
'request_body',
'request_headers',
'request_server',
'route_' . $request->get('_route')
];
/** @var RequestDTO $requestDTO */ /** @var RequestDTO $requestDTO */
while ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) { while ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) {
$requestDTOInstance = $this->grabRequestDTO( $requestDTOInstance = $this->grabRequestDTO(
$requestDTO, $requestDTO,
$request->get('_route'), $tags,
$request->getContent(), $request->getContent(),
$request->query->all(), $request->query->all(),
$request->request->all() $request->request->all()
); );
$errorCollection = $this->validate($requestDTOInstance); $errorCollection = $this->validate($requestDTOInstance, $tags);
if ($errorCollection->hasErrors()) { if ($errorCollection->hasErrors()) {
$event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST)); $event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST));
} else { } else {
@ -61,14 +70,15 @@ class RequestListener
} }
} }
protected function validate(object $data): ErrorCollection protected function validate(object $data, array $tags): ErrorCollection
{ {
$tags = array_merge($tags, ['Default']);
$validator = Validation::createValidatorBuilder() $validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true) ->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader() ->addDefaultDoctrineAnnotationReader()
->getValidator(); ->getValidator();
$violations = $validator->validate($data); $violations = $validator->validate($data, null, $tags);
$errorCollection = new ErrorCollection(); $errorCollection = new ErrorCollection();
if ($violations->count()) { if ($violations->count()) {
@ -81,7 +91,7 @@ class RequestListener
protected function grabRequestDTO( protected function grabRequestDTO(
RequestDTO $requestDTO, RequestDTO $requestDTO,
string $routeName, array $tags,
string $content, string $content,
array $queryParameters = [], array $queryParameters = [],
array $parameters = [] array $parameters = []
@ -101,14 +111,7 @@ class RequestListener
$result = $data2dtoConverter->convert( $result = $data2dtoConverter->convert(
$data, $data,
new $requestDTO->className, new $requestDTO->className,
[ $tags
'default',
'request',
'request_body',
'request_headers',
'request_server',
'route_' . $routeName
]
); );
return $result; return $result;

Loading…
Cancel
Save