diff --git a/src/Attribute/Schema.php b/src/Attribute/Schema.php index 1190496..ec26674 100644 --- a/src/Attribute/Schema.php +++ b/src/Attribute/Schema.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'] - ) {} -} \ No newline at end of file + ) { + $this->map = $map ?? $this->map; + } +} diff --git a/src/Dto2DataConverter.php b/src/Dto2DataConverter.php index 6067de6..91931fd 100644 --- a/src/Dto2DataConverter.php +++ b/src/Dto2DataConverter.php @@ -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 + { + + } + + /** * Получаем теги для обработки */ diff --git a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php index ca67e79..787c2ef 100644 --- a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php +++ b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php @@ -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'] - ); - } -} \ No newline at end of file + public ?array $map = [ + 'surname', + 'age', + 'emails', + 'authors' => [ + 'name', + ], + 'buy' => [ + 'phrase', + 'length', + 'isFirst', + ], + 'bar' => [ + 'barField' + ] + ]; +}