Поменял сигнатуру схемы

master
Rinsvent 3 years ago
parent 6166de2fb7
commit 5ee9eddb08
  1. 11
      src/Attribute/Schema.php
  2. 20
      src/Dto2DataConverter.php
  3. 2
      tests/unit/Converter/fixtures/FillTest/HelloSchema.php

@ -6,13 +6,20 @@ namespace Rinsvent\DTO2Data\Attribute;
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)] #[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
class Schema class Schema
{ {
protected array $baseMap = [];
public function __construct( public function __construct(
public ?array $map = null, public array $map = [],
) { ) {
} }
public function getMap(): array public function getMap(): array
{ {
return $this->map; return $this->baseMap ?: $this->map;
}
public function getTags(mixed $data = null): array
{
return ['default'];
} }
} }

@ -34,23 +34,23 @@ class Dto2DataConverter
'Schema should be instance of Rinsvent\DTO2Data\Attribute\Schema' '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 = []; $result = [];
if (is_iterable($data)) { if (is_iterable($data)) {
foreach ($data as $item) { foreach ($data as $item) {
$result[] = $this->processItem($item, $map); $result[] = $this->processItem($item, $map, $tags);
} }
} else { } else {
$result = $this->processItem($data, $map); $result = $this->processItem($data, $map, $tags);
} }
return $result; return $result;
} }
private function processItem($data, array $map): array private function processItem($data, array $map, array $tags): array
{ {
$result = []; $result = [];
foreach ($map as $key => $item) { foreach ($map as $key => $item) {
@ -59,16 +59,16 @@ class Dto2DataConverter
// key -> propertyPath (===). // key -> propertyPath (===).
case is_int($key) && is_string($item): case is_int($key) && is_string($item):
$result[$item] = $this->grabValue($data, $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; break;
// key -> propertyPath (!==) // key -> propertyPath (!==)
case is_string($key) && is_string($item): case is_string($key) && is_string($item):
$result[$key] = $this->grabValue($data, $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; break;
// key -> recursive data processing // key -> recursive data processing
case is_string($key) && is_array($item): 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; break;
// key -> virtual field // key -> virtual field
case is_string($key) && is_callable($item): case is_string($key) && is_callable($item):
@ -96,11 +96,11 @@ class Dto2DataConverter
return $result; 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); $metas = $this->grabTransformMetas($data, $path);
foreach ($metas as $meta) { foreach ($metas as $meta) {
$value = $this->transformer->transform($value, $meta); $value = $this->transformer->transform($value, $meta, $tags);
} }
return $value; return $value;
} }

@ -7,7 +7,7 @@ use Rinsvent\DTO2Data\Attribute\Schema;
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)] #[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
class HelloSchema extends Schema class HelloSchema extends Schema
{ {
public ?array $baseMap = [ protected array $baseMap = [
'surname', 'surname',
'fake_age' => 'age', 'fake_age' => 'age',
'emails', 'emails',

Loading…
Cancel
Save