From 8030d3b3b2b9a91f7c59825bacc50fcf7b2577c2 Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Wed, 18 Aug 2021 13:11:41 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=D0=BE=D0=BB=D0=B2=D0=B5=20=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attribute/Tags.php | 11 +++++ src/Data2DtoConverter.php | 27 ++++++++++++ tests/unit/Converter/TagsTest.php | 44 +++++++++++++++++++ .../fixtures/FillTest/HelloTagsRequest.php | 18 ++++++++ 4 files changed, 100 insertions(+) create mode 100644 src/Attribute/Tags.php create mode 100644 tests/unit/Converter/TagsTest.php create mode 100644 tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php diff --git a/src/Attribute/Tags.php b/src/Attribute/Tags.php new file mode 100644 index 0000000..56e0d68 --- /dev/null +++ b/src/Attribute/Tags.php @@ -0,0 +1,11 @@ +processTags($object, $data, $tags); + $this->processClassTransformers($reflectionObject, $data, $tags); if (is_object($data)) { return $data; @@ -180,6 +183,30 @@ class Data2DtoConverter return null; } + /** + * Получаем теги для обработки + */ + protected function processTags(object $object, array $data, array $tags): array + { + $classExtractor = new ClassExtractor($object::class); + /** @var Tags $tagsMeta */ + if ($tagsMeta = $classExtractor->fetch(Tags::class)) { + if (method_exists($object, $tagsMeta->method)) { + $reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method); + if (!$reflectionMethod->isPublic()) { + $reflectionMethod->setAccessible(true); + } + $methodTags = $reflectionMethod->invoke($object, ...[$data]); + if (!$reflectionMethod->isPublic()) { + $reflectionMethod->setAccessible(false); + } + return $methodTags; + } + } + + return $tags; + } + /** * Трнансформируем на уровне класса */ diff --git a/tests/unit/Converter/TagsTest.php b/tests/unit/Converter/TagsTest.php new file mode 100644 index 0000000..e07e22c --- /dev/null +++ b/tests/unit/Converter/TagsTest.php @@ -0,0 +1,44 @@ +convert([ + 'surname' => 'Surname1234', + 'fake_age' => 3, + 'fake_age2' => 7, + 'emails' => [ + 'sfdgsa', + 'af234f', + 'asdf33333' + ], + ], new HelloTagsRequest); + $this->assertInstanceOf(HelloTagsRequest::class, $dto); + $this->assertEquals(7, $dto->age); + + } +} diff --git a/tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php b/tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php new file mode 100644 index 0000000..10a4453 --- /dev/null +++ b/tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php @@ -0,0 +1,18 @@ +