Добавил резолве тегов

This commit is contained in:
Sipachev Igor 2021-08-18 13:11:41 +07:00
parent 2a24a0eb49
commit 8030d3b3b2
4 changed files with 100 additions and 0 deletions

11
src/Attribute/Tags.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace Rinsvent\Data2DTO\Attribute;
#[\Attribute]
class Tags
{
public function __construct(
public string $method,
) {}
}

View File

@ -7,6 +7,7 @@ use Rinsvent\AttributeExtractor\ClassExtractor;
use Rinsvent\AttributeExtractor\PropertyExtractor;
use Rinsvent\Data2DTO\Attribute\DTOMeta;
use Rinsvent\Data2DTO\Attribute\PropertyPath;
use Rinsvent\Data2DTO\Attribute\Tags;
use Rinsvent\Data2DTO\Attribute\VirtualProperty;
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage;
use Rinsvent\Data2DTO\Transformer\Meta;
@ -20,6 +21,8 @@ class Data2DtoConverter
$tags = empty($tags) ? ['default'] : $tags;
$reflectionObject = new \ReflectionObject($object);
$tags = $this->processTags($object, $data, $tags);
$this->processClassTransformers($reflectionObject, $data, $tags);
if (is_object($data)) {
return $data;
@ -180,6 +183,30 @@ class Data2DtoConverter
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;
}
/**
* Трнансформируем на уровне класса
*/

View 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);
}
}

View 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'];
}
}