Переименовал TagsResolver

Починил тесты
Вынес модификацию тегов из конвертера.
В будущем можно добавить опционально
master v0.0.15
Sipachev Igor 3 years ago
parent 9e42589419
commit dabeee4a72
  1. 2
      src/Attribute/HandleTags.php
  2. 13
      src/Data2DtoConverter.php
  3. 10
      tests/unit/Converter/TagsTest.php
  4. 4
      tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php

@ -3,7 +3,7 @@
namespace Rinsvent\Data2DTO\Attribute; namespace Rinsvent\Data2DTO\Attribute;
#[\Attribute] #[\Attribute]
class TagsResolver class HandleTags
{ {
public function __construct( public function __construct(
public string $method, public string $method,

@ -7,7 +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\TagsResolver; use Rinsvent\Data2DTO\Attribute\HandleTags;
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;
@ -16,13 +16,16 @@ use function Symfony\Component\String\u;
class Data2DtoConverter class Data2DtoConverter
{ {
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 public function convert(array $data, object $object, array $tags = []): object
{ {
$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;
@ -189,8 +192,8 @@ class Data2DtoConverter
protected function processTags(object $object, array $data, array $tags): array protected function processTags(object $object, array $data, array $tags): array
{ {
$classExtractor = new ClassExtractor($object::class); $classExtractor = new ClassExtractor($object::class);
/** @var TagsResolver $tagsMeta */ /** @var HandleTags $tagsMeta */
if ($tagsMeta = $classExtractor->fetch(TagsResolver::class)) { if ($tagsMeta = $classExtractor->fetch(HandleTags::class)) {
if (method_exists($object, $tagsMeta->method)) { if (method_exists($object, $tagsMeta->method)) {
$reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method); $reflectionMethod = new \ReflectionMethod($object, $tagsMeta->method);
if (!$reflectionMethod->isPublic()) { if (!$reflectionMethod->isPublic()) {

@ -3,9 +3,6 @@
namespace Rinsvent\Data2DTO\Tests\Converter; namespace Rinsvent\Data2DTO\Tests\Converter;
use Rinsvent\Data2DTO\Data2DtoConverter; 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; use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest;
class TagsTest extends \Codeception\Test\Unit class TagsTest extends \Codeception\Test\Unit
@ -27,7 +24,8 @@ class TagsTest extends \Codeception\Test\Unit
public function testSuccessFillRequestData() public function testSuccessFillRequestData()
{ {
$data2DtoConverter = new Data2DtoConverter(); $data2DtoConverter = new Data2DtoConverter();
$dto = $data2DtoConverter->convert([
$data = [
'surname' => 'Surname1234', 'surname' => 'Surname1234',
'fake_age' => 3, 'fake_age' => 3,
'fake_age2' => 7, 'fake_age2' => 7,
@ -36,7 +34,9 @@ class TagsTest extends \Codeception\Test\Unit
'af234f', 'af234f',
'asdf33333' 'asdf33333'
], ],
], new HelloTagsRequest); ];
$tags = $data2DtoConverter->getTags($data, new HelloTagsRequest);
$dto = $data2DtoConverter->convert($data, new HelloTagsRequest, $tags);
$this->assertInstanceOf(HelloTagsRequest::class, $dto); $this->assertInstanceOf(HelloTagsRequest::class, $dto);
$this->assertEquals(7, $dto->age); $this->assertEquals(7, $dto->age);

@ -3,9 +3,9 @@
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest; namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
use Rinsvent\Data2DTO\Attribute\PropertyPath; use Rinsvent\Data2DTO\Attribute\PropertyPath;
use Rinsvent\Data2DTO\Attribute\TagsResolver; use Rinsvent\Data2DTO\Attribute\HandleTags;
#[TagsResolver(method: 'getTags')] #[HandleTags(method: 'getTags')]
class HelloTagsRequest extends HelloRequest class HelloTagsRequest extends HelloRequest
{ {
#[PropertyPath('fake_age2', tags: ['surname-group'])] #[PropertyPath('fake_age2', tags: ['surname-group'])]

Loading…
Cancel
Save