From 2e689c826d685c94dccd286f3d3c09dd3916f319 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Sun, 15 Aug 2021 22:36:24 +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=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D0=B8=20=D0=B2=20=D0=B0=D1=82=D1=82=D1=80=D0=B8=D0=B1?= =?UTF-8?q?=D1=83=D1=82=D1=8B=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=BD?= =?UTF-8?q?=D0=B0=20null=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=86=20?= =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B8=D0=BB=20=D1=81=D0=B5?= =?UTF-8?q?=D1=82=D0=B8=D0=BD=D0=B3=20=D0=BD=D0=B0=20=D1=83=D0=B6=D0=B5=20?= =?UTF-8?q?=D1=81=D1=82=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2=D1=83=D1=8E?= =?UTF-8?q?=D1=89=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attribute/DTOMeta.php | 4 +++- src/Attribute/PropertyPath.php | 4 +++- src/Data2DtoConverter.php | 31 +++++++++++++++++++++---------- src/Transformer/Meta.php | 6 ++++-- src/Transformer/Trim.php | 7 ++++++- 5 files changed, 37 insertions(+), 15 deletions(-) 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