Проиниегрировал логику получения множества ДТО

Реализовал получение данных по пути
Починил тесты
master
Rinsvent 3 years ago
parent 1b9586f2f0
commit 8bb12cffa2
  1. 1
      src/Annotation/RequestDTO.php
  2. 40
      src/EventListener/RequestListener.php
  3. 2
      tests/unit/Listener/FillTest.php

@ -8,5 +8,6 @@ class RequestDTO
public function __construct( public function __construct(
public string $className, public string $className,
public string $jsonPath = '$', public string $jsonPath = '$',
public ?string $attributePath = null,
) {} ) {}
} }

@ -32,14 +32,25 @@ class RequestListener
} }
/** @var RequestDTO $requestDTO */ /** @var RequestDTO $requestDTO */
if ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) { while ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) {
$requestDTOInstance = $this->grabRequestDTO($requestDTO->className, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all()); $requestDTOInstance = $this->grabRequestDTO($requestDTO, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all());
$errorCollection = $this->validate($requestDTOInstance); $errorCollection = $this->validate($requestDTOInstance);
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 {
$request->attributes->set(self::REQUEST_DATA, $requestDTOInstance); $reflectionObject = new \ReflectionObject($requestDTOInstance);
$requestDTOName = $reflectionObject->getShortName();
$attributePath = lcfirst($requestDTOName);
if ($requestDTO->attributePath) {
$attributePath = $requestDTO->attributePath;
}
if ($request->attributes->has($attributePath)) {
throw new \Exception('Same request data has already exists!');
}
$request->attributes->set($attributePath, $requestDTOInstance);
} }
} }
} }
@ -62,7 +73,7 @@ class RequestListener
return $errorCollection; return $errorCollection;
} }
protected function grabRequestDTO(string $requestClass, string $content, array $queryParameters = [], array $parameters = [], array $headers = []) protected function grabRequestDTO(RequestDTO $requestDTO, string $content, array $queryParameters = [], array $parameters = [], array $headers = [])
{ {
$data = []; $data = [];
try { try {
@ -73,9 +84,28 @@ class RequestListener
$data += $queryParameters; $data += $queryParameters;
$data += $parameters; $data += $parameters;
$data = $this->grabDataByJsonPath($data, $requestDTO->jsonPath);
$data += $headers; $data += $headers;
$data2dtoConverter = new Data2DtoConverter(); $data2dtoConverter = new Data2DtoConverter();
return $data2dtoConverter->convert($data, $requestClass); return $data2dtoConverter->convert($data, $requestDTO->className);
}
private function grabDataByJsonPath(array $data, string $jsonPath): array
{
if ($jsonPath !== '$') {
$jsonPath = trim($jsonPath, '$');
$jsonPath = trim($jsonPath, '.');
$jsonPath = explode('.', $jsonPath);
if (is_array($jsonPath)) {
foreach ($jsonPath as $item) {
$data = $data[$item] ?? null;
if ($data === null) {
return [];
}
}
}
}
return $data;
} }
} }

@ -30,7 +30,7 @@ class FillTest extends \Codeception\Test\Unit
$response = $this->tester->send($request); $response = $this->tester->send($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname); $this->assertEquals('Surname', $request->get('helloRequest')->surname);
$this->assertEquals('Hello igor', $response->getContent()); $this->assertEquals('Hello igor', $response->getContent());
} }

Loading…
Cancel
Save