From 5fa62d248e3270f3bbdafdb64b73115fda464937 Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Tue, 17 Aug 2021 12:42:24 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=B0=D1=82=D1=8B=D0=B2=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D1=81=D1=8C=20=D0=B2=D1=81=D0=B5=20property=20dto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Data2DtoConverter.php | 55 ++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Data2DtoConverter.php b/src/Data2DtoConverter.php index fba3d2e..f1447e7 100644 --- a/src/Data2DtoConverter.php +++ b/src/Data2DtoConverter.php @@ -36,32 +36,30 @@ class Data2DtoConverter continue; } - if ($dataPath = $this->grabDataPath($property, $data, $tags)) { - $value = $data[$dataPath]; + $value = $this->grabValue($property, $data, $tags); - $this->processTransformers($property, $value, $tags); + $this->processTransformers($property, $value, $tags); - if ($this->processDataObject($object, $property, $value)) { - continue; - } - - if ($this->processArray($value, $property, $tags)) { - continue; - } + if ($this->processDataObject($object, $property, $value)) { + continue; + } - $preparedPropertyType = $propertyType; - if ($this->processInterface($property, $preparedPropertyType)) { - continue; - } + if ($this->processArray($property, $value, $tags)) { + continue; + } - $this->processClass($object, $property, $preparedPropertyType, $value, $tags); + $preparedPropertyType = $propertyType; + if ($this->processInterface($property, $preparedPropertyType)) { + continue; + } - if ($this->processNull($value, $reflectionPropertyType)) { - continue; - } + $this->processClass($object, $property, $preparedPropertyType, $value, $tags); - $property->setValue($object, $value); + if ($this->processNull($reflectionPropertyType, $value)) { + continue; } + + $property->setValue($object, $value); } return $object; @@ -133,6 +131,15 @@ class Data2DtoConverter } } + protected function grabValue(\ReflectionProperty $property, array $data, array $tags) + { + if ($dataPath = $this->grabDataPath($property, $data, $tags)) { + return $data[$dataPath] ?? null; + } + + return null; + } + protected function grabDataPath(\ReflectionProperty $property, array $data, array $tags): ?string { $propertyName = $property->getName(); @@ -141,13 +148,13 @@ class Data2DtoConverter if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) { $filteredTags = array_diff($tags, $propertyPath->tags); if (count($filteredTags) !== count($tags)) { - if (key_exists($propertyPath->path, $data)) { + if (array_key_exists($propertyPath->path, $data)) { return $propertyPath->path; } } } - if (key_exists($propertyName, $data)) { + if (array_key_exists($propertyName, $data)) { return $propertyName; } @@ -157,7 +164,7 @@ class Data2DtoConverter u($propertyName)->snake()->upper()->toString(), ]; foreach ($variants as $variant) { - if (key_exists($variant, $data)) { + if (array_key_exists($variant, $data)) { return $variant; } } @@ -218,12 +225,12 @@ class Data2DtoConverter /** * Если значение в $data = null, но поле не может его принять - пропустим */ - private function processNull($value, \ReflectionNamedType $reflectionPropertyType): bool + private function processNull(\ReflectionNamedType $reflectionPropertyType, $value): bool { return $value === null && !$reflectionPropertyType->allowsNull(); } - private function processArray(&$value, \ReflectionProperty $property, array $tags): bool + private function processArray(\ReflectionProperty $property, &$value, array $tags): bool { $attributedPropertyClass = $this->grabPropertyDTOClass($property);