From dabeee4a72f5cf618ef34f9e85aafe478d3edc22 Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Thu, 19 Aug 2021 12:44:58 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D0=BB=20TagsResolver=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?= =?UTF-8?q?=20=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20=D0=BC=D0=BE=D0=B4=D0=B8?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8E=20=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B8=D0=B7=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=82=D0=B5=D1=80=D0=B0.=20=D0=92=20=D0=B1=D1=83=D0=B4?= =?UTF-8?q?=D1=83=D1=89=D0=B5=D0=BC=20=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BE=D0=BF?= =?UTF-8?q?=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attribute/{TagsResolver.php => HandleTags.php} | 2 +- src/Data2DtoConverter.php | 13 ++++++++----- tests/unit/Converter/TagsTest.php | 10 +++++----- .../fixtures/FillTest/HelloTagsRequest.php | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) rename src/Attribute/{TagsResolver.php => HandleTags.php} (87%) diff --git a/src/Attribute/TagsResolver.php b/src/Attribute/HandleTags.php similarity index 87% rename from src/Attribute/TagsResolver.php rename to src/Attribute/HandleTags.php index 5f8b45c..b44b9f3 100644 --- a/src/Attribute/TagsResolver.php +++ b/src/Attribute/HandleTags.php @@ -3,7 +3,7 @@ namespace Rinsvent\Data2DTO\Attribute; #[\Attribute] -class TagsResolver +class HandleTags { public function __construct( public string $method, diff --git a/src/Data2DtoConverter.php b/src/Data2DtoConverter.php index 1346f26..560eade 100644 --- a/src/Data2DtoConverter.php +++ b/src/Data2DtoConverter.php @@ -7,7 +7,7 @@ use Rinsvent\AttributeExtractor\ClassExtractor; use Rinsvent\AttributeExtractor\PropertyExtractor; use Rinsvent\Data2DTO\Attribute\DTOMeta; use Rinsvent\Data2DTO\Attribute\PropertyPath; -use Rinsvent\Data2DTO\Attribute\TagsResolver; +use Rinsvent\Data2DTO\Attribute\HandleTags; use Rinsvent\Data2DTO\Attribute\VirtualProperty; use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage; use Rinsvent\Data2DTO\Transformer\Meta; @@ -16,13 +16,16 @@ use function Symfony\Component\String\u; class Data2DtoConverter { + public function getTags(array $data, object $object, array $tags = []): array + { + return $this->processTags($object, $data, $tags); + } + public function convert(array $data, object $object, array $tags = []): object { $tags = empty($tags) ? ['default'] : $tags; $reflectionObject = new \ReflectionObject($object); - $tags = $this->processTags($object, $data, $tags); - $this->processClassTransformers($reflectionObject, $data, $tags); if (is_object($data)) { return $data; @@ -189,8 +192,8 @@ class Data2DtoConverter protected function processTags(object $object, array $data, array $tags): array { $classExtractor = new ClassExtractor($object::class); - /** @var TagsResolver $tagsMeta */ - if ($tagsMeta = $classExtractor->fetch(TagsResolver::class)) { + /** @var HandleTags $tagsMeta */ + if ($tagsMeta = $classExtractor->fetch(HandleTags::class)) { if (method_exists($object, $tagsMeta->method)) { $reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method); if (!$reflectionMethod->isPublic()) { diff --git a/tests/unit/Converter/TagsTest.php b/tests/unit/Converter/TagsTest.php index e07e22c..23e56c4 100644 --- a/tests/unit/Converter/TagsTest.php +++ b/tests/unit/Converter/TagsTest.php @@ -3,9 +3,6 @@ namespace Rinsvent\Data2DTO\Tests\Converter; use Rinsvent\Data2DTO\Data2DtoConverter; -use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Bar; -use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\BuyRequest; -use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloRequest; use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest; class TagsTest extends \Codeception\Test\Unit @@ -27,7 +24,8 @@ class TagsTest extends \Codeception\Test\Unit public function testSuccessFillRequestData() { $data2DtoConverter = new Data2DtoConverter(); - $dto = $data2DtoConverter->convert([ + + $data = [ 'surname' => 'Surname1234', 'fake_age' => 3, 'fake_age2' => 7, @@ -36,7 +34,9 @@ class TagsTest extends \Codeception\Test\Unit 'af234f', 'asdf33333' ], - ], new HelloTagsRequest); + ]; + $tags = $data2DtoConverter->getTags($data, new HelloTagsRequest); + $dto = $data2DtoConverter->convert($data, new HelloTagsRequest, $tags); $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 index 458a615..3502ef8 100644 --- a/tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php +++ b/tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php @@ -3,9 +3,9 @@ namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest; use Rinsvent\Data2DTO\Attribute\PropertyPath; -use Rinsvent\Data2DTO\Attribute\TagsResolver; +use Rinsvent\Data2DTO\Attribute\HandleTags; -#[TagsResolver(method: 'getTags')] +#[HandleTags(method: 'getTags')] class HelloTagsRequest extends HelloRequest { #[PropertyPath('fake_age2', tags: ['surname-group'])]