Изменил структуру карты.

на ДТО получается громоздко
будут простые массивы.
В перспективе возможно yaml описание
и можно плагин для шторма запилить
master
Sipachev Igor 3 years ago
parent 8adc3f19be
commit befb6f3e1e
  1. 8
      src/Attribute/Schema.php
  2. 43
      src/Dto2DataConverter.php
  3. 45
      tests/unit/Converter/fixtures/FillTest/HelloSchema.php

@ -8,8 +8,10 @@ use Rinsvent\DTO2Data\DTO\Map;
class Schema class Schema
{ {
public function __construct( public function __construct(
public Map $map, public ?array $map = null,
/** @var string[] $tags */ /** @var string[] $tags */
public array $tags = ['default'] public array $tags = ['default']
) {} ) {
} $this->map = $map ?? $this->map;
}
}

@ -28,18 +28,25 @@ class Dto2DataConverter
return $this->convertByMap($object, $schema->map, $tags); return $this->convertByMap($object, $schema->map, $tags);
} }
public function convertByMap($objectData, Map $map, array $tags = []): array public function convertByMap(object $object, array $map, array $tags = []): array
{ {
$data = []; $data = [];
$reflectionObject = new \ReflectionObject($object); $reflectionObject = new \ReflectionObject($object);
foreach ($map->properties as $propertyName => $childMap) { foreach ($map as $propertyKey => $property) {
if (is_array($property)) {
$propertyName = $propertyKey;
} else {
$propertyName = $property;
}
$property = new ReflectionProperty($object, $propertyName); $property = new ReflectionProperty($object, $propertyName);
$value = $this->getValue($object, $property); $value = $this->getValue($object, $property);
if ($childMap && !is_scalar($value)) { $childMap = $property;
if ($childMap && is_array($childMap) && !is_scalar($value)) {
if (is_array($value)) { if (is_array($value)) {
$tmpValue = []; $tmpValue = [];
foreach($value as $objectDataItem) { foreach ($value as $objectDataItem) {
$tmpValue[] = $this->convertByMap($objectDataItem, $childMap, $tags); $tmpValue[] = $this->convertByMap($objectDataItem, $childMap, $tags);
} }
$value = $tmpValue; $value = $tmpValue;
@ -53,23 +60,29 @@ class Dto2DataConverter
$data[$dataPath] = $value; $data[$dataPath] = $value;
} }
foreach ($map->methods as $methodName => $childMap) { // foreach ($map->methods as $methodName => $childMap) {
$method = new \ReflectionMethod($object, $methodName); // $method = new \ReflectionMethod($object, $methodName);
$value = $this->getMethodValue($object, $method); // $value = $this->getMethodValue($object, $method);
if ($childMap) { // if ($childMap) {
$value = $this->convertByMap($value, $childMap, $tags); // $value = $this->convertByMap($value, $childMap, $tags);
} // }
$this->processMethodTransformers($method, $value, $tags); // $this->processMethodTransformers($method, $value, $tags);
$dataPath = $this->grabMethodDataPath($method, $tags); // $dataPath = $this->grabMethodDataPath($method, $tags);
$dataPath = $dataPath ?? $propertyName; // $dataPath = $dataPath ?? $propertyName;
$data[$dataPath] = $value; // $data[$dataPath] = $value;
} // }
$this->processClassTransformers($reflectionObject, $data, $tags); $this->processClassTransformers($reflectionObject, $data, $tags);
return $data; return $data;
} }
public function convertArrayByMap(array $data, array $map, array $tags = []): array
{
}
/** /**
* Получаем теги для обработки * Получаем теги для обработки
*/ */

@ -8,31 +8,20 @@ use Rinsvent\DTO2Data\DTO\Map;
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)] #[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
class HelloSchema extends Schema class HelloSchema extends Schema
{ {
public function __construct( public ?array $map = [
) { 'surname',
'age',
parent::__construct( 'emails',
(new Map()) 'authors' => [
->addProperty('surname') 'name',
->addProperty('age') ],
->addProperty('emails') 'buy' => [
->addProperty( 'phrase',
'authors', 'length',
(new Map()) 'isFirst',
->addProperty('name') ],
) 'bar' => [
->addProperty('buy', 'barField'
(new Map) ]
->addProperty('phrase') ];
->addProperty('length') }
->addProperty('isFirst')
)
->addProperty('bar',
(new Map())
->addProperty('barField')
)
,
['default']
);
}
}

Loading…
Cancel
Save