Compare commits

..

No commits in common. 'master' and 'd7facd9cc7caf0b35c3b5e605aa7a484fc3dd0a3' have entirely different histories.

  1. 3
      composer.json
  2. 4417
      composer.lock
  3. 53
      src/Data2DtoConverter.php
  4. 16
      src/Resolver/SimpleResolver.php
  5. 11
      src/Resolver/TransformerResolverInterface.php
  6. 32
      src/Resolver/TransformerResolverStorage.php
  7. 15
      src/Transformer/Meta.php
  8. 8
      src/Transformer/TransformerInterface.php
  9. 14
      src/Transformer/Trim.php
  10. 18
      src/Transformer/TrimTransformer.php
  11. 2
      tests/unit/Converter/fixtures/FillTest/HelloRequest.php
  12. 2
      tests/unit/Converter/fixtures/FillTest/Transformer/ClassData.php
  13. 9
      tests/unit/Converter/fixtures/FillTest/Transformer/ClassDataTransformer.php
  14. 2
      tests/unit/Converter/fixtures/FillTest/Transformer/ClassObject.php
  15. 10
      tests/unit/Converter/fixtures/FillTest/Transformer/ClassObjectTransformer.php

@ -8,8 +8,7 @@
"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."
}, },
"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

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

@ -0,0 +1,16 @@
<?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;
}
}

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

@ -0,0 +1,32 @@
<?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];
}
}

@ -0,0 +1,15 @@
<?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']
) {}
}

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

@ -0,0 +1,14 @@
<?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());
}
}

@ -0,0 +1,18 @@
<?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);
}
}

@ -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\Transformer\Transformer\Trim; use Rinsvent\Data2DTO\Transformer\Trim;
class HelloRequest class HelloRequest
{ {

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

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

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

@ -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\Transformer\Transformer\Meta; use Rinsvent\Data2DTO\Transformer\Meta;
use Rinsvent\Transformer\Transformer\TransformerInterface; use Rinsvent\Data2DTO\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(mixed $data, Meta $meta): mixed public function transform(&$data, Meta $meta): void
{ {
if ($data === null) { if ($data === null) {
return $data; return;
} }
$object = new HelloClassTransformersRequest2(); $object = new HelloClassTransformersRequest2();
$object->surname = '98789'; $object->surname = '98789';
return $object; $data = $object;
} }
} }

Loading…
Cancel
Save