Добавил резолве тегов
This commit is contained in:
parent
2a24a0eb49
commit
8030d3b3b2
11
src/Attribute/Tags.php
Normal file
11
src/Attribute/Tags.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rinsvent\Data2DTO\Attribute;
|
||||||
|
|
||||||
|
#[\Attribute]
|
||||||
|
class Tags
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $method,
|
||||||
|
) {}
|
||||||
|
}
|
@ -7,6 +7,7 @@ use Rinsvent\AttributeExtractor\ClassExtractor;
|
|||||||
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
||||||
use Rinsvent\Data2DTO\Attribute\DTOMeta;
|
use Rinsvent\Data2DTO\Attribute\DTOMeta;
|
||||||
use Rinsvent\Data2DTO\Attribute\PropertyPath;
|
use Rinsvent\Data2DTO\Attribute\PropertyPath;
|
||||||
|
use Rinsvent\Data2DTO\Attribute\Tags;
|
||||||
use Rinsvent\Data2DTO\Attribute\VirtualProperty;
|
use Rinsvent\Data2DTO\Attribute\VirtualProperty;
|
||||||
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage;
|
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage;
|
||||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||||
@ -20,6 +21,8 @@ class Data2DtoConverter
|
|||||||
$tags = empty($tags) ? ['default'] : $tags;
|
$tags = empty($tags) ? ['default'] : $tags;
|
||||||
$reflectionObject = new \ReflectionObject($object);
|
$reflectionObject = new \ReflectionObject($object);
|
||||||
|
|
||||||
|
$tags = $this->processTags($object, $data, $tags);
|
||||||
|
|
||||||
$this->processClassTransformers($reflectionObject, $data, $tags);
|
$this->processClassTransformers($reflectionObject, $data, $tags);
|
||||||
if (is_object($data)) {
|
if (is_object($data)) {
|
||||||
return $data;
|
return $data;
|
||||||
@ -180,6 +183,30 @@ class Data2DtoConverter
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Получаем теги для обработки
|
||||||
|
*/
|
||||||
|
protected function processTags(object $object, array $data, array $tags): array
|
||||||
|
{
|
||||||
|
$classExtractor = new ClassExtractor($object::class);
|
||||||
|
/** @var Tags $tagsMeta */
|
||||||
|
if ($tagsMeta = $classExtractor->fetch(Tags::class)) {
|
||||||
|
if (method_exists($object, $tagsMeta->method)) {
|
||||||
|
$reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method);
|
||||||
|
if (!$reflectionMethod->isPublic()) {
|
||||||
|
$reflectionMethod->setAccessible(true);
|
||||||
|
}
|
||||||
|
$methodTags = $reflectionMethod->invoke($object, ...[$data]);
|
||||||
|
if (!$reflectionMethod->isPublic()) {
|
||||||
|
$reflectionMethod->setAccessible(false);
|
||||||
|
}
|
||||||
|
return $methodTags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Трнансформируем на уровне класса
|
* Трнансформируем на уровне класса
|
||||||
*/
|
*/
|
||||||
|
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\Bar;
|
||||||
|
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
||||||
|
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||||
|
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();
|
||||||
|
$dto = $data2DtoConverter->convert([
|
||||||
|
'surname' => 'Surname1234',
|
||||||
|
'fake_age' => 3,
|
||||||
|
'fake_age2' => 7,
|
||||||
|
'emails' => [
|
||||||
|
'sfdgsa',
|
||||||
|
'af234f',
|
||||||
|
'asdf33333'
|
||||||
|
],
|
||||||
|
], new HelloTagsRequest);
|
||||||
|
$this->assertInstanceOf(HelloTagsRequest::class, $dto);
|
||||||
|
$this->assertEquals(7, $dto->age);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
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\Tags;
|
||||||
|
|
||||||
|
#[Tags(method: 'getTags')]
|
||||||
|
class HelloTagsRequest extends HelloRequest
|
||||||
|
{
|
||||||
|
#[PropertyPath('fake_age2', tags: ['surname-group'])]
|
||||||
|
public int $age;
|
||||||
|
|
||||||
|
public function getTags(array $data)
|
||||||
|
{
|
||||||
|
return 'Surname1234' === ($data['surname'] ?? null) ? ['surname-group'] : ['default'];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user