Рефакторинг

Настроил чтобы обрабатывались все property dto
master v0.0.7
Sipachev Igor 3 years ago
parent 44ef3f1143
commit 5fa62d248e
  1. 27
      src/Data2DtoConverter.php

@ -36,8 +36,7 @@ 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);
@ -45,7 +44,7 @@ class Data2DtoConverter
continue; continue;
} }
if ($this->processArray($value, $property, $tags)) { if ($this->processArray($property, $value, $tags)) {
continue; continue;
} }
@ -56,13 +55,12 @@ class Data2DtoConverter
$this->processClass($object, $property, $preparedPropertyType, $value, $tags); $this->processClass($object, $property, $preparedPropertyType, $value, $tags);
if ($this->processNull($value, $reflectionPropertyType)) { if ($this->processNull($reflectionPropertyType, $value)) {
continue; continue;
} }
$property->setValue($object, $value); $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);

Loading…
Cancel
Save