|
|
@ -9,19 +9,13 @@ use Rinsvent\Data2DTO\Attribute\DTOMeta; |
|
|
|
use Rinsvent\Data2DTO\Attribute\PropertyPath; |
|
|
|
use Rinsvent\Data2DTO\Attribute\PropertyPath; |
|
|
|
use Rinsvent\Data2DTO\Attribute\HandleTags; |
|
|
|
use Rinsvent\Data2DTO\Attribute\HandleTags; |
|
|
|
use Rinsvent\Data2DTO\Attribute\VirtualProperty; |
|
|
|
use Rinsvent\Data2DTO\Attribute\VirtualProperty; |
|
|
|
use Rinsvent\Transformer\Transformer; |
|
|
|
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage; |
|
|
|
use Rinsvent\Transformer\Transformer\Meta; |
|
|
|
use Rinsvent\Data2DTO\Transformer\Meta; |
|
|
|
|
|
|
|
use Rinsvent\Data2DTO\Transformer\TransformerInterface; |
|
|
|
use function Symfony\Component\String\u; |
|
|
|
use function Symfony\Component\String\u; |
|
|
|
|
|
|
|
|
|
|
|
class Data2DtoConverter |
|
|
|
class Data2DtoConverter |
|
|
|
{ |
|
|
|
{ |
|
|
|
private Transformer $transformer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->transformer = new Transformer(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getTags(array $data, object $object, array $tags = []): array |
|
|
|
public function getTags(array $data, object $object, array $tags = []): array |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->processTags($object, $data, $tags); |
|
|
|
return $this->processTags($object, $data, $tags); |
|
|
@ -84,12 +78,8 @@ class Data2DtoConverter |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Для виртуальных полей добавляем пустой масиив, чтобы заполнить поля дто |
|
|
|
* Для виртуальных полей добавляем пустой масиив, чтобы заполнить поля дто |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function processVirtualProperty( |
|
|
|
protected function processVirtualProperty(object $object, \ReflectionProperty $property, array $data, array $tags): bool |
|
|
|
object $object, |
|
|
|
{ |
|
|
|
\ReflectionProperty $property, |
|
|
|
|
|
|
|
array $data, |
|
|
|
|
|
|
|
array $tags |
|
|
|
|
|
|
|
): bool { |
|
|
|
|
|
|
|
$propertyExtractor = new PropertyExtractor($property->class, $property->getName()); |
|
|
|
$propertyExtractor = new PropertyExtractor($property->class, $property->getName()); |
|
|
|
if ($propertyExtractor->fetch(VirtualProperty::class)) { |
|
|
|
if ($propertyExtractor->fetch(VirtualProperty::class)) { |
|
|
|
$propertyValue = $this->getValue($object, $property); |
|
|
|
$propertyValue = $this->getValue($object, $property); |
|
|
@ -139,13 +129,8 @@ class Data2DtoConverter |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Если это class, то рекурсивно заполняем дальше |
|
|
|
* Если это class, то рекурсивно заполняем дальше |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function processClass( |
|
|
|
protected function processClass(object $object, ReflectionProperty $property, string $preparedPropertyType, &$value, array $tags): bool |
|
|
|
object $object, |
|
|
|
{ |
|
|
|
ReflectionProperty $property, |
|
|
|
|
|
|
|
string $preparedPropertyType, |
|
|
|
|
|
|
|
&$value, |
|
|
|
|
|
|
|
array $tags |
|
|
|
|
|
|
|
): bool { |
|
|
|
|
|
|
|
if (class_exists($preparedPropertyType)) { |
|
|
|
if (class_exists($preparedPropertyType)) { |
|
|
|
$propertyValue = $this->getValue($object, $property); |
|
|
|
$propertyValue = $this->getValue($object, $property); |
|
|
|
if (!is_array($value)) { |
|
|
|
if (!is_array($value)) { |
|
|
@ -235,7 +220,13 @@ class Data2DtoConverter |
|
|
|
/** @var Meta $transformMeta */ |
|
|
|
/** @var Meta $transformMeta */ |
|
|
|
while ($transformMeta = $classExtractor->fetch(Meta::class)) { |
|
|
|
while ($transformMeta = $classExtractor->fetch(Meta::class)) { |
|
|
|
$transformMeta->returnType = $className; |
|
|
|
$transformMeta->returnType = $className; |
|
|
|
$data = $this->transformer->transform($data, $transformMeta, $tags); |
|
|
|
$filteredTags = array_diff($tags, $transformMeta->tags); |
|
|
|
|
|
|
|
if (count($filteredTags) === count($tags)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$transformer = $this->grabTransformer($transformMeta); |
|
|
|
|
|
|
|
$transformer->transform($data, $transformMeta); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -248,15 +239,27 @@ class Data2DtoConverter |
|
|
|
$propertyExtractor = new PropertyExtractor($property->class, $propertyName); |
|
|
|
$propertyExtractor = new PropertyExtractor($property->class, $propertyName); |
|
|
|
/** @var Meta $transformMeta */ |
|
|
|
/** @var Meta $transformMeta */ |
|
|
|
while ($transformMeta = $propertyExtractor->fetch(Meta::class)) { |
|
|
|
while ($transformMeta = $propertyExtractor->fetch(Meta::class)) { |
|
|
|
|
|
|
|
$filteredTags = array_diff($tags, $transformMeta->tags); |
|
|
|
|
|
|
|
if (count($filteredTags) === count($tags)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
/** @var \ReflectionNamedType $reflectionPropertyType */ |
|
|
|
/** @var \ReflectionNamedType $reflectionPropertyType */ |
|
|
|
$reflectionPropertyType = $property->getType(); |
|
|
|
$reflectionPropertyType = $property->getType(); |
|
|
|
$propertyType = $reflectionPropertyType->getName(); |
|
|
|
$propertyType = $reflectionPropertyType->getName(); |
|
|
|
$transformMeta->retrnType = $propertyType; |
|
|
|
$transformMeta->returnType = $propertyType; |
|
|
|
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull(); |
|
|
|
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull(); |
|
|
|
$data = $this->transformer->transform($data, $transformMeta, $tags); |
|
|
|
$transformer = $this->grabTransformer($transformMeta); |
|
|
|
|
|
|
|
$transformer->transform($data, $transformMeta); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function grabTransformer(Meta $meta): TransformerInterface |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$storage = TransformerResolverStorage::getInstance(); |
|
|
|
|
|
|
|
$resolver = $storage->get($meta::TYPE); |
|
|
|
|
|
|
|
return $resolver->resolve($meta); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Если значение в $data = null, но поле не может его принять - пропустим |
|
|
|
* Если значение в $data = null, но поле не может его принять - пропустим |
|
|
|
*/ |
|
|
|
*/ |
|
|
|