From 8bb12cffa28399de5c2b1c8f5814f4a3b774d93d Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Mon, 9 Aug 2021 23:44:00 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B8=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=B3=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D1=83=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B0=20=D0=94=D0=A2=D0=9E=20=D0=A0=D0=B5=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BF=D0=BE=20=D0=BF=D1=83=D1=82=D0=B8=20=D0=9F=D0=BE?= =?UTF-8?q?=D1=87=D0=B8=D0=BD=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Annotation/RequestDTO.php | 1 + src/EventListener/RequestListener.php | 40 +++++++++++++++++++++++---- tests/unit/Listener/FillTest.php | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Annotation/RequestDTO.php b/src/Annotation/RequestDTO.php index dcb919e..f6e176c 100644 --- a/src/Annotation/RequestDTO.php +++ b/src/Annotation/RequestDTO.php @@ -8,5 +8,6 @@ class RequestDTO public function __construct( public string $className, public string $jsonPath = '$', + public ?string $attributePath = null, ) {} } diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index cb6fc94..ac93480 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -32,14 +32,25 @@ class RequestListener } /** @var RequestDTO $requestDTO */ - if ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) { - $requestDTOInstance = $this->grabRequestDTO($requestDTO->className, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all()); + while ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) { + $requestDTOInstance = $this->grabRequestDTO($requestDTO, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all()); $errorCollection = $this->validate($requestDTOInstance); if ($errorCollection->hasErrors()) { $event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST)); } 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; } - 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 = []; try { @@ -73,9 +84,28 @@ class RequestListener $data += $queryParameters; $data += $parameters; + $data = $this->grabDataByJsonPath($data, $requestDTO->jsonPath); $data += $headers; $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; } } diff --git a/tests/unit/Listener/FillTest.php b/tests/unit/Listener/FillTest.php index 6d8fa63..756143b 100644 --- a/tests/unit/Listener/FillTest.php +++ b/tests/unit/Listener/FillTest.php @@ -30,7 +30,7 @@ class FillTest extends \Codeception\Test\Unit $response = $this->tester->send($request); $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()); }