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