diff --git a/src/Attribute/DTOMeta.php b/src/Attribute/DTOMeta.php index 33e2ae7..4e978c3 100644 --- a/src/Attribute/DTOMeta.php +++ b/src/Attribute/DTOMeta.php @@ -6,6 +6,8 @@ namespace Rinsvent\Data2DTO\Attribute; class DTOMeta { public function __construct( - public string $class + public string $class, + /** @var string[] $tags */ + public array $tags = ['default'] ) {} } \ No newline at end of file diff --git a/src/Attribute/PropertyPath.php b/src/Attribute/PropertyPath.php index 2a18566..39fa929 100644 --- a/src/Attribute/PropertyPath.php +++ b/src/Attribute/PropertyPath.php @@ -6,6 +6,8 @@ namespace Rinsvent\Data2DTO\Attribute; class PropertyPath { public function __construct( - public string $path + public string $path, + /** @var string[] $tags */ + public array $tags = ['default'] ) {} } \ No newline at end of file diff --git a/src/Data2DtoConverter.php b/src/Data2DtoConverter.php index a063a7c..bfc00c4 100644 --- a/src/Data2DtoConverter.php +++ b/src/Data2DtoConverter.php @@ -30,21 +30,18 @@ class Data2DtoConverter $reflectionPropertyType = $property->getType(); $propertyType = $reflectionPropertyType->getName(); - if ($dataPath = $this->grabDataPath($property, $data)) { + if ($dataPath = $this->grabDataPath($property, $data, $tags)) { $value = $data[$dataPath]; // Трансформируем данные $this->processTransformers($property, $value, $tags); - if ($this->checkNullRule($value, $reflectionPropertyType)) { - continue; - } // В данных лежит объект, то дальше его не заполняем. Только присваиваем. Например, entity, document if (is_object($value)) { $property->setValue($object, $value); continue; } - if (!$this->transformArray($value, $property)) { + if (!$this->transformArray($value, $property, $tags)) { continue; } @@ -66,7 +63,16 @@ class Data2DtoConverter // Если это class, то рекурсивно заполняем дальше if (class_exists($preparedPropertyType)) { - $value = $this->convert($value, $preparedPropertyType); + if ($property->isInitialized($object)) { + $propertyValue = $property->getValue($object); + $value = $this->convert($value, $preparedPropertyType, $tags, $propertyValue); + } else { + $value = $this->convert($value, $preparedPropertyType, $tags); + } + } + + if ($this->checkNullRule($value, $reflectionPropertyType)) { + continue; } // присваиваем получившееся значение @@ -77,13 +83,18 @@ class Data2DtoConverter return $object; } - protected function grabDataPath(\ReflectionProperty $property, array $data): ?string + protected function grabDataPath(\ReflectionProperty $property, array $data, array $tags): ?string { $propertyName = $property->getName(); $propertyExtractor = new PropertyExtractor($property->class, $propertyName); /** @var PropertyPath $propertyPath */ if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) { - return $propertyPath->path; + $filteredTags = array_diff($tags, $propertyPath->tags); + if (count($filteredTags) !== count($tags)) { + if (key_exists($propertyPath->path, $data)) { + return $propertyPath->path; + } + } } if (key_exists($propertyName, $data)) { @@ -156,7 +167,7 @@ class Data2DtoConverter return $value === null && !$reflectionPropertyType->allowsNull(); } - private function transformArray(&$value, \ReflectionProperty $property): bool + private function transformArray(&$value, \ReflectionProperty $property, array $tags): bool { $attributedPropertyClass = $this->grabPropertyDTOClass($property); @@ -172,7 +183,7 @@ class Data2DtoConverter } $tempValue = []; foreach ($value as $itemValue) { - $tempValue[] = $this->convert($itemValue, $attributedPropertyClass); + $tempValue[] = $this->convert($itemValue, $attributedPropertyClass, $tags); } $value = $tempValue; } diff --git a/src/Transformer/Meta.php b/src/Transformer/Meta.php index c0ac356..ec834a6 100644 --- a/src/Transformer/Meta.php +++ b/src/Transformer/Meta.php @@ -8,6 +8,8 @@ abstract class Meta public const TYPE = 'simple'; public ?string $returnType = null; public ?bool $allowsNull = null; - /** @var string[] $tags */ - public array $tags = ['default']; + + public function __construct( + public array $tags = ['default'] + ) {} } \ No newline at end of file diff --git a/src/Transformer/Trim.php b/src/Transformer/Trim.php index 513a30a..70e6202 100644 --- a/src/Transformer/Trim.php +++ b/src/Transformer/Trim.php @@ -5,5 +5,10 @@ namespace Rinsvent\Data2DTO\Transformer; #[\Attribute] class Trim extends Meta { - public string $characters = " \t\n\r\0\x0B"; + public function __construct( + public array $tags = ['default'], + public string $characters = " \t\n\r\0\x0B" + ) { + parent::__construct(...func_get_args()); + } } \ No newline at end of file