|
|
@ -9,13 +9,19 @@ 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\Data2DTO\Resolver\TransformerResolverStorage; |
|
|
|
use Rinsvent\Transformer\Transformer; |
|
|
|
use Rinsvent\Data2DTO\Transformer\Meta; |
|
|
|
use Rinsvent\Transformer\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); |
|
|
@ -78,8 +84,12 @@ class Data2DtoConverter |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Для виртуальных полей добавляем пустой масиив, чтобы заполнить поля дто |
|
|
|
* Для виртуальных полей добавляем пустой масиив, чтобы заполнить поля дто |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function processVirtualProperty(object $object, \ReflectionProperty $property, array $data, array $tags): bool |
|
|
|
protected function processVirtualProperty( |
|
|
|
{ |
|
|
|
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); |
|
|
@ -129,8 +139,13 @@ class Data2DtoConverter |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Если это class, то рекурсивно заполняем дальше |
|
|
|
* Если это class, то рекурсивно заполняем дальше |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
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 = $this->getValue($object, $property); |
|
|
|
$propertyValue = $this->getValue($object, $property); |
|
|
|
if (!is_array($value)) { |
|
|
|
if (!is_array($value)) { |
|
|
@ -220,13 +235,7 @@ 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; |
|
|
|
$filteredTags = array_diff($tags, $transformMeta->tags); |
|
|
|
$data = $this->transformer->transform($data, $transformMeta, $tags); |
|
|
|
if (count($filteredTags) === count($tags)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$transformer = $this->grabTransformer($transformMeta); |
|
|
|
|
|
|
|
$transformer->transform($data, $transformMeta); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -239,27 +248,15 @@ 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->returnType = $propertyType; |
|
|
|
$transformMeta->retrnType = $propertyType; |
|
|
|
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull(); |
|
|
|
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull(); |
|
|
|
$transformer = $this->grabTransformer($transformMeta); |
|
|
|
$data = $this->transformer->transform($data, $transformMeta, $tags); |
|
|
|
$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, но поле не может его принять - пропустим |
|
|
|
*/ |
|
|
|
*/ |
|
|
|