Рефакторинг

Доп проверки
master v0.0.12
Rinsvent 3 years ago
parent a45c2907df
commit 2a24a0eb49
  1. 28
      src/Data2DtoConverter.php

@ -76,7 +76,8 @@ class Data2DtoConverter
{ {
$propertyExtractor = new PropertyExtractor($property->class, $property->getName()); $propertyExtractor = new PropertyExtractor($property->class, $property->getName());
if ($propertyExtractor->fetch(VirtualProperty::class)) { if ($propertyExtractor->fetch(VirtualProperty::class)) {
if ($property->isInitialized($object) && $propertyValue = $property->getValue($object)) { $propertyValue = $this->getValue($object, $property);
if ($property->isInitialized($object) && $propertyValue) {
$value = $this->convert($data, $propertyValue, $tags); $value = $this->convert($data, $propertyValue, $tags);
} else { } else {
$propertyType = $property->getType()->getName(); $propertyType = $property->getType()->getName();
@ -125,11 +126,11 @@ class Data2DtoConverter
protected function processClass(object $object, ReflectionProperty $property, string $preparedPropertyType, &$value, array $tags): bool protected function processClass(object $object, ReflectionProperty $property, string $preparedPropertyType, &$value, array $tags): bool
{ {
if (class_exists($preparedPropertyType)) { if (class_exists($preparedPropertyType)) {
$propertyValue = $property->getValue($object); $propertyValue = $this->getValue($object, $property);
if (!is_array($propertyValue)) { if (!is_array($value)) {
return true; return true;
} }
if ($property->isInitialized($object)) { if ($property->isInitialized($object) && $propertyValue) {
$value = $this->convert($value, $propertyValue, $tags); $value = $this->convert($value, $propertyValue, $tags);
} else { } else {
$value = $this->convert($value, new $preparedPropertyType, $tags); $value = $this->convert($value, new $preparedPropertyType, $tags);
@ -286,4 +287,23 @@ class Data2DtoConverter
$property->setAccessible(false); $property->setAccessible(false);
} }
} }
private function getValue(object $object, \ReflectionProperty $property)
{
if (!$property->isInitialized($object)) {
return null;
}
if (!$property->isPublic()) {
$property->setAccessible(true);
}
$value = $property->getValue($object);
if (!$property->isPublic()) {
$property->setAccessible(false);
}
return $value;
}
} }

Loading…
Cancel
Save