Реализовал поддерку интерфейсов.

Нужен рефакторинг.
Метод разрастается
master
Rinsvent 3 years ago
parent c2de3054b5
commit 283fe21efe
  1. 41
      src/Data2DtoConverter.php
  2. 8
      tests/unit/Converter/FillTest.php
  3. 8
      tests/unit/Converter/fixtures/FillTest/Bar.php
  4. 8
      tests/unit/Converter/fixtures/FillTest/BarInterface.php
  5. 2
      tests/unit/Converter/fixtures/FillTest/HelloRequest.php

@ -42,10 +42,27 @@ class Data2DtoConverter
continue;
}
$preparedPropertyType = $propertyType;
if (interface_exists($preparedPropertyType)) {
$attributedPropertyClass = $this->grabPropertyDTOClass($property);
// Если не указали мета информацию для интерфейса - пропустим
if (!$attributedPropertyClass) {
continue;
}
// Если класс не реализует интерфейс свойства - пропустим
$interfaces = class_implements($attributedPropertyClass);
if (!isset($interfaces[$preparedPropertyType])) {
continue;
}
$preparedPropertyType = $attributedPropertyClass;
}
// Если это class, то рекурсивно заполняем дальше
if (class_exists($propertyType)) {
$value = $this->convert($value, $propertyType);
if (class_exists($preparedPropertyType)) {
$value = $this->convert($value, $preparedPropertyType);
}
// присваиваем получившееся значение
$property->setValue($object, $value);
}
@ -109,13 +126,7 @@ class Data2DtoConverter
private function transformArray(&$value, \ReflectionProperty $property): bool
{
$attributedPropertyClass = null;
$propertyName = $property->getName();
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
/** @var DTOMeta $dtoMeta */
if ($dtoMeta = $propertyExtractor->fetch(DTOMeta::class)) {
$attributedPropertyClass = $dtoMeta->class;
}
$attributedPropertyClass = $this->grabPropertyDTOClass($property);
/** @var \ReflectionNamedType $reflectionPropertyType */
$reflectionPropertyType = $property->getType();
@ -135,4 +146,16 @@ class Data2DtoConverter
}
return true;
}
private function grabPropertyDTOClass(\ReflectionProperty $property): ?string
{
$attributedPropertyClass = null;
$propertyName = $property->getName();
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
/** @var DTOMeta $dtoMeta */
if ($dtoMeta = $propertyExtractor->fetch(DTOMeta::class)) {
$attributedPropertyClass = $dtoMeta->class;
}
return $attributedPropertyClass;
}
}

@ -3,6 +3,7 @@
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;
@ -47,6 +48,9 @@ class FillTest extends \Codeception\Test\Unit
'isFirst' => true,
'extraData2' => '1234'
],
'bar' => [
'barField' => 32
],
'extraData1' => 'qwer'
], HelloRequest::class);
$this->assertInstanceOf(HelloRequest::class, $dto);
@ -65,5 +69,9 @@ class FillTest extends \Codeception\Test\Unit
$this->assertCount(2, $dto->authors);
$this->assertEquals('Tolkien', $dto->authors[0]->name);
$this->assertEquals('Sapkovsky', $dto->authors[1]->name);
$this->assertInstanceOf(Bar::class, $dto->bar);
$this->assertIsFloat($dto->bar->barField);
$this->assertEquals(32.0, $dto->bar->barField);
}
}

@ -0,0 +1,8 @@
<?php
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
class Bar implements BarInterface
{
public float $barField;
}

@ -0,0 +1,8 @@
<?php
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
interface BarInterface
{
}

@ -16,4 +16,6 @@ class HelloRequest
#[DTOMeta(class: Author::class)]
public array $authors;
public BuyRequest $buy;
#[DTOMeta(class: Bar::class)]
public BarInterface $bar;
}

Loading…
Cancel
Save