From 3f403ba077c8d020305a0beecb619a826e22ecf8 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Sun, 8 Aug 2021 14:01:51 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D0=BC=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B2=D0=BE=D0=B2=20=D0=94=D0=A2=D0=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attribute/DTOMeta.php | 11 ++++++++++ src/Data2DtoConverter.php | 22 +++++++++++++++++++ tests/unit/Converter/FillTest.php | 12 ++++++++++ .../Converter/fixtures/FillTest/Author.php | 8 +++++++ .../fixtures/FillTest/HelloRequest.php | 3 +++ 5 files changed, 56 insertions(+) create mode 100644 src/Attribute/DTOMeta.php create mode 100644 tests/unit/Converter/fixtures/FillTest/Author.php diff --git a/src/Attribute/DTOMeta.php b/src/Attribute/DTOMeta.php new file mode 100644 index 0000000..33e2ae7 --- /dev/null +++ b/src/Attribute/DTOMeta.php @@ -0,0 +1,11 @@ +setValue($object, $value); continue; } + $attributedPropertyClass = null; + $propertyName = $property->getName(); + $propertyExtractor = new PropertyExtractor($property->class, $propertyName); + /** @var DTOMeta $dtoMeta */ + if ($dtoMeta = $propertyExtractor->fetch(DTOMeta::class)) { + $attributedPropertyClass = $dtoMeta->class; + } + + // Если массив и есть атрибут с указанием класса, то также преобразуем структуру + if ($propertyType === 'array' && $attributedPropertyClass) { + // Если тип у ДТО - массив, а в значении не массив - пропустим + if (!is_array($value)) { + continue; + } + $tempValue = []; + foreach ($value as $itemValue) { + $tempValue[] = $this->convert($itemValue, $attributedPropertyClass); + } + $value = $tempValue; + } + // Если это class, то рекурсивно заполняем дальше if (class_exists($propertyType)) { $value = $this->convert($value, $propertyType); diff --git a/tests/unit/Converter/FillTest.php b/tests/unit/Converter/FillTest.php index a446cf3..e950a09 100644 --- a/tests/unit/Converter/FillTest.php +++ b/tests/unit/Converter/FillTest.php @@ -33,6 +33,14 @@ class FillTest extends \Codeception\Test\Unit 'af234f', 'asdf33333' ], + 'authors' => [ + [ + 'name' => 'Tolkien', + ], + [ + 'name' => 'Sapkovsky' + ] + ], 'buy' => [ 'phrase' => 'Buy buy!!!', 'length' => 10, @@ -53,5 +61,9 @@ class FillTest extends \Codeception\Test\Unit $this->assertEquals('Buy buy!!!', $dto->buy->phrase); $this->assertEquals(10, $dto->buy->length); $this->assertEquals(true, $dto->buy->isFirst); + + $this->assertCount(2, $dto->authors); + $this->assertEquals('Tolkien', $dto->authors[0]->name); + $this->assertEquals('Sapkovsky', $dto->authors[1]->name); } } diff --git a/tests/unit/Converter/fixtures/FillTest/Author.php b/tests/unit/Converter/fixtures/FillTest/Author.php new file mode 100644 index 0000000..ef071ac --- /dev/null +++ b/tests/unit/Converter/fixtures/FillTest/Author.php @@ -0,0 +1,8 @@ +