Рефакторинг

Настроил чтобы обрабатывались все property dto
This commit is contained in:
Sipachev Igor 2021-08-17 12:42:24 +07:00
parent 44ef3f1143
commit 5fa62d248e

View File

@ -36,32 +36,30 @@ class Data2DtoConverter
continue; continue;
} }
if ($dataPath = $this->grabDataPath($property, $data, $tags)) { $value = $this->grabValue($property, $data, $tags);
$value = $data[$dataPath];
$this->processTransformers($property, $value, $tags); $this->processTransformers($property, $value, $tags);
if ($this->processDataObject($object, $property, $value)) { if ($this->processDataObject($object, $property, $value)) {
continue; continue;
}
if ($this->processArray($value, $property, $tags)) {
continue;
}
$preparedPropertyType = $propertyType;
if ($this->processInterface($property, $preparedPropertyType)) {
continue;
}
$this->processClass($object, $property, $preparedPropertyType, $value, $tags);
if ($this->processNull($value, $reflectionPropertyType)) {
continue;
}
$property->setValue($object, $value);
} }
if ($this->processArray($property, $value, $tags)) {
continue;
}
$preparedPropertyType = $propertyType;
if ($this->processInterface($property, $preparedPropertyType)) {
continue;
}
$this->processClass($object, $property, $preparedPropertyType, $value, $tags);
if ($this->processNull($reflectionPropertyType, $value)) {
continue;
}
$property->setValue($object, $value);
} }
return $object; 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 protected function grabDataPath(\ReflectionProperty $property, array $data, array $tags): ?string
{ {
$propertyName = $property->getName(); $propertyName = $property->getName();
@ -141,13 +148,13 @@ class Data2DtoConverter
if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) { if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) {
$filteredTags = array_diff($tags, $propertyPath->tags); $filteredTags = array_diff($tags, $propertyPath->tags);
if (count($filteredTags) !== count($tags)) { if (count($filteredTags) !== count($tags)) {
if (key_exists($propertyPath->path, $data)) { if (array_key_exists($propertyPath->path, $data)) {
return $propertyPath->path; return $propertyPath->path;
} }
} }
} }
if (key_exists($propertyName, $data)) { if (array_key_exists($propertyName, $data)) {
return $propertyName; return $propertyName;
} }
@ -157,7 +164,7 @@ class Data2DtoConverter
u($propertyName)->snake()->upper()->toString(), u($propertyName)->snake()->upper()->toString(),
]; ];
foreach ($variants as $variant) { foreach ($variants as $variant) {
if (key_exists($variant, $data)) { if (array_key_exists($variant, $data)) {
return $variant; return $variant;
} }
} }
@ -218,12 +225,12 @@ class Data2DtoConverter
/** /**
* Если значение в $data = null, но поле не может его принять - пропустим * Если значение в $data = null, но поле не может его принять - пропустим
*/ */
private function processNull($value, \ReflectionNamedType $reflectionPropertyType): bool private function processNull(\ReflectionNamedType $reflectionPropertyType, $value): bool
{ {
return $value === null && !$reflectionPropertyType->allowsNull(); 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); $attributedPropertyClass = $this->grabPropertyDTOClass($property);