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

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

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

@ -28,18 +28,25 @@ class Dto2DataConverter
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 = [];
$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);
$value = $this->getValue($object, $property);
if ($childMap && !is_scalar($value)) {
$childMap = $property;
if ($childMap && is_array($childMap) && !is_scalar($value)) {
if (is_array($value)) {
$tmpValue = [];
foreach($value as $objectDataItem) {
foreach ($value as $objectDataItem) {
$tmpValue[] = $this->convertByMap($objectDataItem, $childMap, $tags);
}
$value = $tmpValue;
@ -53,23 +60,29 @@ class Dto2DataConverter
$data[$dataPath] = $value;
}
foreach ($map->methods as $methodName => $childMap) {
$method = new \ReflectionMethod($object, $methodName);
$value = $this->getMethodValue($object, $method);
if ($childMap) {
$value = $this->convertByMap($value, $childMap, $tags);
}
$this->processMethodTransformers($method, $value, $tags);
$dataPath = $this->grabMethodDataPath($method, $tags);
$dataPath = $dataPath ?? $propertyName;
$data[$dataPath] = $value;
}
// foreach ($map->methods as $methodName => $childMap) {
// $method = new \ReflectionMethod($object, $methodName);
// $value = $this->getMethodValue($object, $method);
// if ($childMap) {
// $value = $this->convertByMap($value, $childMap, $tags);
// }
// $this->processMethodTransformers($method, $value, $tags);
// $dataPath = $this->grabMethodDataPath($method, $tags);
// $dataPath = $dataPath ?? $propertyName;
// $data[$dataPath] = $value;
// }
$this->processClassTransformers($reflectionObject, $data, $tags);
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)]
class HelloSchema extends Schema
{
public function __construct(
) {
parent::__construct(
(new Map())
->addProperty('surname')
->addProperty('age')
->addProperty('emails')
->addProperty(
'authors',
(new Map())
->addProperty('name')
)
->addProperty('buy',
(new Map)
->addProperty('phrase')
->addProperty('length')
->addProperty('isFirst')
)
->addProperty('bar',
(new Map())
->addProperty('barField')
)
,
['default']
);
}
public ?array $map = [
'surname',
'age',
'emails',
'authors' => [
'name',
],
'buy' => [
'phrase',
'length',
'isFirst',
],
'bar' => [
'barField'
]
];
}
Loading…
Cancel
Save