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 @@ +