feature: вынес трансформер в отдельную библиотеку

This commit is contained in:
Rinsvent 2022-04-02 14:32:55 +07:00
parent d7facd9cc7
commit c1c7702b30
15 changed files with 40 additions and 4572 deletions

View File

@ -8,7 +8,8 @@
"ext-iconv": "*", "ext-iconv": "*",
"ext-json": "*", "ext-json": "*",
"symfony/string": "^5.3", "symfony/string": "^5.3",
"rinsvent/attribute-extractor": "^0.0" "rinsvent/attribute-extractor": "^0.0",
"rinsvent/transformer": "^0.0.2"
}, },
"require-dev": { "require-dev": {
"codeception/codeception": "^4.1", "codeception/codeception": "^4.1",

4417
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -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, но поле не может его принять - пропустим
*/ */

View File

@ -1,16 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Resolver;
use Rinsvent\Data2DTO\Transformer\Meta;
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
class SimpleResolver implements TransformerResolverInterface
{
public function resolve(Meta $meta): TransformerInterface
{
$metaClass = $meta::class;
$transformerClass = $metaClass . 'Transformer';
return new $transformerClass;
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Resolver;
use Rinsvent\Data2DTO\Transformer\Meta;
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
interface TransformerResolverInterface
{
public function resolve(Meta $meta): TransformerInterface;
}

View File

@ -1,32 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Resolver;
class TransformerResolverStorage
{
private array $items = [];
public static function getInstance(): self
{
static $instance = null;
if ($instance) {
return $instance;
}
$instance = new self();
$instance->add('simple', new SimpleResolver());
return $instance;
}
public function add(string $code, TransformerResolverInterface $transformerResolver): void
{
$this->items[$code] = $transformerResolver;
}
public function get(string $code): TransformerResolverInterface
{
return $this->items[$code];
}
}

View File

@ -1,15 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Transformer;
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
abstract class Meta
{
public const TYPE = 'simple';
public ?string $returnType = null;
public ?bool $allowsNull = null;
public function __construct(
public array $tags = ['default']
) {}
}

View File

@ -1,8 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Transformer;
interface TransformerInterface
{
public function transform(&$data, Meta $meta): void;
}

View File

@ -1,14 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Transformer;
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
class Trim extends Meta
{
public function __construct(
public array $tags = ['default'],
public string $characters = " \t\n\r\0\x0B"
) {
parent::__construct(...func_get_args());
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace Rinsvent\Data2DTO\Transformer;
class TrimTransformer implements TransformerInterface
{
/**
* @param string|null $data
* @param Trim $meta
*/
public function transform(&$data, Meta $meta): void
{
if ($data === null) {
return;
}
$data = trim($data, $meta->characters);
}
}

View File

@ -4,7 +4,7 @@ namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
use Rinsvent\Data2DTO\Attribute\DTOMeta; use Rinsvent\Data2DTO\Attribute\DTOMeta;
use Rinsvent\Data2DTO\Attribute\PropertyPath; use Rinsvent\Data2DTO\Attribute\PropertyPath;
use Rinsvent\Data2DTO\Transformer\Trim; use Rinsvent\Transformer\Transformer\Trim;
class HelloRequest class HelloRequest
{ {

View File

@ -2,7 +2,7 @@
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer; namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
use Rinsvent\Data2DTO\Transformer\Meta; use Rinsvent\Transformer\Transformer\Meta;
#[\Attribute] #[\Attribute]
class ClassData extends Meta class ClassData extends Meta

View File

@ -2,8 +2,8 @@
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer; namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
use Rinsvent\Data2DTO\Transformer\Meta; use Rinsvent\Transformer\Transformer\Meta;
use Rinsvent\Data2DTO\Transformer\TransformerInterface; use Rinsvent\Transformer\Transformer\TransformerInterface;
class ClassDataTransformer implements TransformerInterface class ClassDataTransformer implements TransformerInterface
{ {
@ -11,14 +11,15 @@ class ClassDataTransformer implements TransformerInterface
* @param array|null $data * @param array|null $data
* @param ClassData $meta * @param ClassData $meta
*/ */
public function transform(&$data, Meta $meta): void public function transform(mixed $data, Meta $meta): mixed
{ {
if ($data === null) { if ($data === null) {
return; return $data;
} }
if (isset($data['surname'])) { if (isset($data['surname'])) {
$data['surname'] = '123454321'; $data['surname'] = '123454321';
} }
return $data;
} }
} }

View File

@ -2,7 +2,7 @@
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer; namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
use Rinsvent\Data2DTO\Transformer\Meta; use Rinsvent\Transformer\Transformer\Meta;
#[\Attribute] #[\Attribute]
class ClassObject extends Meta class ClassObject extends Meta

View File

@ -3,8 +3,8 @@
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer; namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2; use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
use Rinsvent\Data2DTO\Transformer\Meta; use Rinsvent\Transformer\Transformer\Meta;
use Rinsvent\Data2DTO\Transformer\TransformerInterface; use Rinsvent\Transformer\Transformer\TransformerInterface;
class ClassObjectTransformer implements TransformerInterface class ClassObjectTransformer implements TransformerInterface
{ {
@ -12,13 +12,13 @@ class ClassObjectTransformer implements TransformerInterface
* @param array|null $data * @param array|null $data
* @param ClassData $meta * @param ClassData $meta
*/ */
public function transform(&$data, Meta $meta): void public function transform(mixed $data, Meta $meta): mixed
{ {
if ($data === null) { if ($data === null) {
return; return $data;
} }
$object = new HelloClassTransformersRequest2(); $object = new HelloClassTransformersRequest2();
$object->surname = '98789'; $object->surname = '98789';
$data = $object; return $object;
} }
} }