From 5ee9eddb08ca68c0a8b613a691fe2e3bbf135b0a Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Sat, 2 Apr 2022 10:56:35 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B8=D0=B3=D0=BD=D0=B0=D1=82=D1=83=D1=80=D1=83=20=D1=81?= =?UTF-8?q?=D1=85=D0=B5=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attribute/Schema.php | 11 ++++++++-- src/Dto2DataConverter.php | 20 +++++++++---------- .../fixtures/FillTest/HelloSchema.php | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Attribute/Schema.php b/src/Attribute/Schema.php index 799a4d8..27ff29d 100644 --- a/src/Attribute/Schema.php +++ b/src/Attribute/Schema.php @@ -6,13 +6,20 @@ namespace Rinsvent\DTO2Data\Attribute; #[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)] class Schema { + protected array $baseMap = []; + public function __construct( - public ?array $map = null, + public array $map = [], ) { } public function getMap(): array { - return $this->map; + return $this->baseMap ?: $this->map; + } + + public function getTags(mixed $data = null): array + { + return ['default']; } } diff --git a/src/Dto2DataConverter.php b/src/Dto2DataConverter.php index e966d6d..d65570e 100644 --- a/src/Dto2DataConverter.php +++ b/src/Dto2DataConverter.php @@ -34,23 +34,23 @@ class Dto2DataConverter 'Schema should be instance of Rinsvent\DTO2Data\Attribute\Schema' ); } - return $this->convertByMap($data, $schema->map); + return $this->convertByMap($data, $schema->getMap(), $schema->getTags($data)); } - private function convertByMap($data, array $map): array + private function convertByMap($data, array $map, array $tags): array { $result = []; if (is_iterable($data)) { foreach ($data as $item) { - $result[] = $this->processItem($item, $map); + $result[] = $this->processItem($item, $map, $tags); } } else { - $result = $this->processItem($data, $map); + $result = $this->processItem($data, $map, $tags); } return $result; } - private function processItem($data, array $map): array + private function processItem($data, array $map, array $tags): array { $result = []; foreach ($map as $key => $item) { @@ -59,16 +59,16 @@ class Dto2DataConverter // key -> propertyPath (===). case is_int($key) && is_string($item): $result[$item] = $this->grabValue($data, $item); - $result[$item] = $this->transform($data, $item, $result[$item]); + $result[$item] = $this->transform($data, $item, $result[$item], $tags); break; // key -> propertyPath (!==) case is_string($key) && is_string($item): $result[$key] = $this->grabValue($data, $item); - $result[$key] = $this->transform($data, $item, $result[$key]); + $result[$key] = $this->transform($data, $item, $result[$key], $tags); break; // key -> recursive data processing case is_string($key) && is_array($item): - $result[$key] = $this->convertByMap($this->grabValue($data, $key), $item); + $result[$key] = $this->convertByMap($this->grabValue($data, $key), $item, $tags); break; // key -> virtual field case is_string($key) && is_callable($item): @@ -96,11 +96,11 @@ class Dto2DataConverter return $result; } - private function transform(object|array|null $data, string $path, mixed $value): mixed + private function transform(object|array|null $data, string $path, mixed $value, array $tags): mixed { $metas = $this->grabTransformMetas($data, $path); foreach ($metas as $meta) { - $value = $this->transformer->transform($value, $meta); + $value = $this->transformer->transform($value, $meta, $tags); } return $value; } diff --git a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php index 1914e22..14e3e66 100644 --- a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php +++ b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php @@ -7,7 +7,7 @@ use Rinsvent\DTO2Data\Attribute\Schema; #[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)] class HelloSchema extends Schema { - public ?array $baseMap = [ + protected array $baseMap = [ 'surname', 'fake_age' => 'age', 'emails',