Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
a62a65babd | |||
c1c7702b30 | |||
d7facd9cc7 | |||
|
59f37226ee | ||
642dd026f1 | |||
|
dabeee4a72 | ||
9e42589419 | |||
859a7c3e0f | |||
|
8030d3b3b2 | ||
2a24a0eb49 | |||
a45c2907df | |||
|
ef336c0d2e | ||
|
5e470a4497 |
@ -8,7 +8,8 @@
|
||||
"ext-iconv": "*",
|
||||
"ext-json": "*",
|
||||
"symfony/string": "^5.3",
|
||||
"rinsvent/attribute-extractor": "^0.0"
|
||||
"rinsvent/attribute-extractor": "^0.0",
|
||||
"rinsvent/transformer": "^0."
|
||||
},
|
||||
"require-dev": {
|
||||
"codeception/codeception": "^4.1",
|
||||
|
4417
composer.lock
generated
4417
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Rinsvent\Data2DTO\Attribute;
|
||||
|
||||
#[\Attribute]
|
||||
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
|
||||
class DTOMeta
|
||||
{
|
||||
public function __construct(
|
||||
@ -10,4 +10,4 @@ class DTOMeta
|
||||
/** @var string[] $tags */
|
||||
public array $tags = ['default']
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
11
src/Attribute/HandleTags.php
Normal file
11
src/Attribute/HandleTags.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Attribute;
|
||||
|
||||
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
|
||||
class HandleTags
|
||||
{
|
||||
public function __construct(
|
||||
public string $method,
|
||||
) {}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Rinsvent\Data2DTO\Attribute;
|
||||
|
||||
#[\Attribute]
|
||||
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
|
||||
class PropertyPath
|
||||
{
|
||||
public function __construct(
|
||||
@ -10,4 +10,4 @@ class PropertyPath
|
||||
/** @var string[] $tags */
|
||||
public array $tags = ['default']
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,10 @@
|
||||
namespace Rinsvent\Data2DTO\Attribute;
|
||||
|
||||
/** @property string[] $tags */
|
||||
#[\Attribute]
|
||||
#[\Attribute(\Attribute::IS_REPEATABLE|\Attribute::TARGET_ALL)]
|
||||
class VirtualProperty
|
||||
{
|
||||
public function __construct(
|
||||
public array $tags = ['default']
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,26 @@ use Rinsvent\AttributeExtractor\ClassExtractor;
|
||||
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
||||
use Rinsvent\Data2DTO\Attribute\DTOMeta;
|
||||
use Rinsvent\Data2DTO\Attribute\PropertyPath;
|
||||
use Rinsvent\Data2DTO\Attribute\HandleTags;
|
||||
use Rinsvent\Data2DTO\Attribute\VirtualProperty;
|
||||
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage;
|
||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
|
||||
use Rinsvent\Transformer\Transformer;
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
use function Symfony\Component\String\u;
|
||||
|
||||
class Data2DtoConverter
|
||||
{
|
||||
private Transformer $transformer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->transformer = new Transformer();
|
||||
}
|
||||
|
||||
public function getTags(array $data, object $object, array $tags = []): array
|
||||
{
|
||||
return $this->processTags($object, $data, $tags);
|
||||
}
|
||||
|
||||
public function convert(array $data, object $object, array $tags = []): object
|
||||
{
|
||||
$tags = empty($tags) ? ['default'] : $tags;
|
||||
@ -28,6 +40,8 @@ class Data2DtoConverter
|
||||
$properties = $reflectionObject->getProperties();
|
||||
/** @var \ReflectionProperty $property */
|
||||
foreach ($properties as $property) {
|
||||
// todo добавить атрибут на пропуск обработки и еще атрибут на допустимые поля
|
||||
|
||||
/** @var \ReflectionNamedType $reflectionPropertyType */
|
||||
$reflectionPropertyType = $property->getType();
|
||||
$propertyType = $reflectionPropertyType->getName();
|
||||
@ -53,7 +67,9 @@ class Data2DtoConverter
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->processClass($object, $property, $preparedPropertyType, $value, $tags);
|
||||
if ($this->processClass($object, $property, $preparedPropertyType, $value, $tags)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->processNull($reflectionPropertyType, $value)) {
|
||||
continue;
|
||||
@ -68,12 +84,16 @@ 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());
|
||||
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);
|
||||
} else {
|
||||
$propertyType = $property->getType()->getName();
|
||||
@ -119,16 +139,25 @@ class Data2DtoConverter
|
||||
/**
|
||||
* Если это class, то рекурсивно заполняем дальше
|
||||
*/
|
||||
protected function processClass(object $object, ReflectionProperty $property, string $preparedPropertyType, &$value, array $tags)
|
||||
{
|
||||
protected function processClass(
|
||||
object $object,
|
||||
ReflectionProperty $property,
|
||||
string $preparedPropertyType,
|
||||
&$value,
|
||||
array $tags
|
||||
): bool {
|
||||
if (class_exists($preparedPropertyType)) {
|
||||
if ($property->isInitialized($object)) {
|
||||
$propertyValue = $property->getValue($object);
|
||||
$propertyValue = $this->getValue($object, $property);
|
||||
if (!is_array($value)) {
|
||||
return true;
|
||||
}
|
||||
if ($property->isInitialized($object) && $propertyValue) {
|
||||
$value = $this->convert($value, $propertyValue, $tags);
|
||||
} else {
|
||||
$value = $this->convert($value, new $preparedPropertyType, $tags);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function grabValue(\ReflectionProperty $property, array $data, array $tags)
|
||||
@ -172,6 +201,30 @@ class Data2DtoConverter
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получаем теги для обработки
|
||||
*/
|
||||
protected function processTags(object $object, array $data, array $tags): array
|
||||
{
|
||||
$classExtractor = new ClassExtractor($object::class);
|
||||
/** @var HandleTags $tagsMeta */
|
||||
if ($tagsMeta = $classExtractor->fetch(HandleTags::class)) {
|
||||
if (method_exists($object, $tagsMeta->method)) {
|
||||
$reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method);
|
||||
if (!$reflectionMethod->isPublic()) {
|
||||
$reflectionMethod->setAccessible(true);
|
||||
}
|
||||
$methodTags = $reflectionMethod->invoke($object, ...[$data, $tags]);
|
||||
if (!$reflectionMethod->isPublic()) {
|
||||
$reflectionMethod->setAccessible(false);
|
||||
}
|
||||
return $methodTags;
|
||||
}
|
||||
}
|
||||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Трнансформируем на уровне класса
|
||||
*/
|
||||
@ -182,13 +235,7 @@ class Data2DtoConverter
|
||||
/** @var Meta $transformMeta */
|
||||
while ($transformMeta = $classExtractor->fetch(Meta::class)) {
|
||||
$transformMeta->returnType = $className;
|
||||
$filteredTags = array_diff($tags, $transformMeta->tags);
|
||||
if (count($filteredTags) === count($tags)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$transformer = $this->grabTransformer($transformMeta);
|
||||
$transformer->transform($data, $transformMeta);
|
||||
$data = $this->transformer->transform($data, $transformMeta, $tags);
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,27 +248,15 @@ class Data2DtoConverter
|
||||
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
|
||||
/** @var Meta $transformMeta */
|
||||
while ($transformMeta = $propertyExtractor->fetch(Meta::class)) {
|
||||
$filteredTags = array_diff($tags, $transformMeta->tags);
|
||||
if (count($filteredTags) === count($tags)) {
|
||||
continue;
|
||||
}
|
||||
/** @var \ReflectionNamedType $reflectionPropertyType */
|
||||
$reflectionPropertyType = $property->getType();
|
||||
$propertyType = $reflectionPropertyType->getName();
|
||||
$transformMeta->returnType = $propertyType;
|
||||
$transformMeta->retrnType = $propertyType;
|
||||
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull();
|
||||
$transformer = $this->grabTransformer($transformMeta);
|
||||
$transformer->transform($data, $transformMeta);
|
||||
$data = $this->transformer->transform($data, $transformMeta, $tags);
|
||||
}
|
||||
}
|
||||
|
||||
protected function grabTransformer(Meta $meta): TransformerInterface
|
||||
{
|
||||
$storage = TransformerResolverStorage::getInstance();
|
||||
$resolver = $storage->get($meta::TYPE);
|
||||
return $resolver->resolve($meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Если значение в $data = null, но поле не может его принять - пропустим
|
||||
*/
|
||||
@ -246,6 +281,9 @@ class Data2DtoConverter
|
||||
}
|
||||
$tempValue = [];
|
||||
foreach ($value as $itemValue) {
|
||||
if (!is_array($itemValue)) {
|
||||
continue;
|
||||
}
|
||||
$tempValue[] = $this->convert($itemValue, new $attributedPropertyClass, $tags);
|
||||
}
|
||||
$value = $tempValue;
|
||||
@ -276,4 +314,26 @@ class Data2DtoConverter
|
||||
$property->setAccessible(false);
|
||||
}
|
||||
}
|
||||
|
||||
private function getValue(object $object, \ReflectionProperty $property)
|
||||
{
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(true);
|
||||
}
|
||||
|
||||
if (!$property->isInitialized($object)) {
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = $property->getValue($object);
|
||||
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(false);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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];
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Transformer;
|
||||
|
||||
#[\Attribute]
|
||||
abstract class Meta
|
||||
{
|
||||
public const TYPE = 'simple';
|
||||
public ?string $returnType = null;
|
||||
public ?bool $allowsNull = null;
|
||||
|
||||
public function __construct(
|
||||
public array $tags = ['default']
|
||||
) {}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Transformer;
|
||||
|
||||
interface TransformerInterface
|
||||
{
|
||||
public function transform(&$data, Meta $meta): void;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Transformer;
|
||||
|
||||
#[\Attribute]
|
||||
class Trim extends Meta
|
||||
{
|
||||
public function __construct(
|
||||
public array $tags = ['default'],
|
||||
public string $characters = " \t\n\r\0\x0B"
|
||||
) {
|
||||
parent::__construct(...func_get_args());
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
51
tests/unit/Converter/ClassTransformersTest.php
Normal file
51
tests/unit/Converter/ClassTransformersTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\Converter;
|
||||
|
||||
use Rinsvent\Data2DTO\Data2DtoConverter;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest;
|
||||
|
||||
class ClassTransformersTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessWithReturnData()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$data = [
|
||||
'surname' => 'Surname1234',
|
||||
'age' => 3,
|
||||
];
|
||||
$dto = $data2DtoConverter->convert($data, new HelloClassTransformersRequest);
|
||||
$this->assertInstanceOf(HelloClassTransformersRequest::class, $dto);
|
||||
$this->assertEquals(123454321, $dto->surname);
|
||||
}
|
||||
|
||||
public function testSuccessWithReturnObject()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$data = [
|
||||
'surname' => 'Surname1234',
|
||||
'age' => 3,
|
||||
];
|
||||
$dto = $data2DtoConverter->convert($data, new HelloClassTransformersRequest2());
|
||||
$this->assertInstanceOf(HelloClassTransformersRequest2::class, $dto);
|
||||
$this->assertEquals(98789, $dto->surname);
|
||||
}
|
||||
}
|
43
tests/unit/Converter/DataObjectTest.php
Normal file
43
tests/unit/Converter/DataObjectTest.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\Converter;
|
||||
|
||||
use Rinsvent\Data2DTO\Data2DtoConverter;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Bar;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||
|
||||
class DataObjectTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessFillRequestData()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$buy = new BuyRequest();
|
||||
$buy->phrase = 'Buy buy!!!';
|
||||
$buy->length = 10;
|
||||
$buy->isFirst = true;
|
||||
|
||||
$dto = $data2DtoConverter->convert([
|
||||
'surname' => ' asdf',
|
||||
'buy' => $buy
|
||||
], new HelloRequest);
|
||||
|
||||
$this->assertInstanceOf(HelloRequest::class, $dto);
|
||||
$this->assertEquals($buy, $dto->buy);
|
||||
}
|
||||
}
|
44
tests/unit/Converter/TagsTest.php
Normal file
44
tests/unit/Converter/TagsTest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\Converter;
|
||||
|
||||
use Rinsvent\Data2DTO\Data2DtoConverter;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest;
|
||||
|
||||
class TagsTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessFillRequestData()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$data = [
|
||||
'surname' => 'Surname1234',
|
||||
'fake_age' => 3,
|
||||
'fake_age2' => 7,
|
||||
'emails' => [
|
||||
'sfdgsa',
|
||||
'af234f',
|
||||
'asdf33333'
|
||||
],
|
||||
];
|
||||
$tags = $data2DtoConverter->getTags($data, new HelloTagsRequest);
|
||||
$dto = $data2DtoConverter->convert($data, new HelloTagsRequest, $tags);
|
||||
$this->assertInstanceOf(HelloTagsRequest::class, $dto);
|
||||
$this->assertEquals(7, $dto->age);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassData;
|
||||
|
||||
#[ClassData]
|
||||
class HelloClassTransformersRequest
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassObject;
|
||||
|
||||
#[ClassObject]
|
||||
class HelloClassTransformersRequest2
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
@ -4,7 +4,7 @@ namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Attribute\DTOMeta;
|
||||
use Rinsvent\Data2DTO\Attribute\PropertyPath;
|
||||
use Rinsvent\Data2DTO\Transformer\Trim;
|
||||
use Rinsvent\Transformer\Transformer\Trim;
|
||||
|
||||
class HelloRequest
|
||||
{
|
||||
@ -18,4 +18,16 @@ class HelloRequest
|
||||
public BuyRequest $buy;
|
||||
#[DTOMeta(class: Bar::class)]
|
||||
public BarInterface $bar;
|
||||
|
||||
private BuyRequest $buy2;
|
||||
|
||||
public function getBuy2(): BuyRequest
|
||||
{
|
||||
return $this->buy2;
|
||||
}
|
||||
|
||||
public function setBuy2(BuyRequest $buy2): void
|
||||
{
|
||||
$this->buy2 = $buy2;
|
||||
}
|
||||
}
|
||||
|
18
tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php
Normal file
18
tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Attribute\PropertyPath;
|
||||
use Rinsvent\Data2DTO\Attribute\HandleTags;
|
||||
|
||||
#[HandleTags(method: 'getTags')]
|
||||
class HelloTagsRequest extends HelloRequest
|
||||
{
|
||||
#[PropertyPath('fake_age2', tags: ['surname-group'])]
|
||||
public int $age;
|
||||
|
||||
public function getTags(array $data, array $tags)
|
||||
{
|
||||
return 'Surname1234' === ($data['surname'] ?? null) ? ['surname-group'] : $tags;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassData extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||
|
||||
class ClassDataTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(mixed $data, Meta $meta): mixed
|
||||
{
|
||||
if ($data === null) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (isset($data['surname'])) {
|
||||
$data['surname'] = '123454321';
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassObject extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||
|
||||
class ClassObjectTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(mixed $data, Meta $meta): mixed
|
||||
{
|
||||
if ($data === null) {
|
||||
return $data;
|
||||
}
|
||||
$object = new HelloClassTransformersRequest2();
|
||||
$object->surname = '98789';
|
||||
return $object;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user