Добавил поддержку Traversable
This commit is contained in:
parent
77c0c14c14
commit
20317698e9
@ -65,7 +65,7 @@ class Dto2DataConverter
|
|||||||
|
|
||||||
public function convertArrayByMap($data, ?array $map, array $tags = []): ?array
|
public function convertArrayByMap($data, ?array $map, array $tags = []): ?array
|
||||||
{
|
{
|
||||||
$isAssociative = count($data) && !array_key_exists(0, $data);
|
// $isAssociative = count($data) && !array_key_exists(0, $data);
|
||||||
|
|
||||||
$tempValue = [];
|
$tempValue = [];
|
||||||
foreach ($data as $key => $item) {
|
foreach ($data as $key => $item) {
|
||||||
|
@ -6,6 +6,8 @@ use Rinsvent\DTO2Data\Dto2DataConverter;
|
|||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Author;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Author;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Bar;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Bar;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
||||||
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Collection;
|
||||||
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\CollectionItem;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\UUID;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\UUID;
|
||||||
|
|
||||||
@ -69,6 +71,13 @@ class FillTest extends \Codeception\Test\Unit
|
|||||||
$bar->barField = 32;
|
$bar->barField = 32;
|
||||||
$helloRequest->bar = $bar;
|
$helloRequest->bar = $bar;
|
||||||
$helloRequest->uuid = new UUID('qwerqw-qwerqwe-werqw-qwerqw');
|
$helloRequest->uuid = new UUID('qwerqw-qwerqwe-werqw-qwerqw');
|
||||||
|
$collection = new Collection();
|
||||||
|
$collection->items = [
|
||||||
|
new CollectionItem('3'),
|
||||||
|
new CollectionItem('1'),
|
||||||
|
new CollectionItem('2'),
|
||||||
|
];
|
||||||
|
$helloRequest->collection = $collection;
|
||||||
|
|
||||||
$dto = $dto2DataConverter->convert($helloRequest);
|
$dto = $dto2DataConverter->convert($helloRequest);
|
||||||
// codecept_debug(json_encode($dto));
|
// codecept_debug(json_encode($dto));
|
||||||
@ -106,7 +115,18 @@ class FillTest extends \Codeception\Test\Unit
|
|||||||
"bar" => [
|
"bar" => [
|
||||||
"barField" => 32
|
"barField" => 32
|
||||||
],
|
],
|
||||||
'uuid' => 'qwerqw-qwerqwe-werqw-qwerqw'
|
'uuid' => 'qwerqw-qwerqwe-werqw-qwerqw',
|
||||||
|
'collection' => [
|
||||||
|
[
|
||||||
|
'value' => '3',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'value' => '1',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'value' => '2',
|
||||||
|
],
|
||||||
|
],
|
||||||
], $dto);
|
], $dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
tests/unit/Converter/fixtures/FillTest/Collection.php
Normal file
34
tests/unit/Converter/fixtures/FillTest/Collection.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||||
|
|
||||||
|
class Collection implements \Iterator
|
||||||
|
{
|
||||||
|
public array $items = [];
|
||||||
|
private $position = 0;
|
||||||
|
|
||||||
|
public function rewind()
|
||||||
|
{
|
||||||
|
$this->position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function current()
|
||||||
|
{
|
||||||
|
return $this->items[$this->position];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key()
|
||||||
|
{
|
||||||
|
return $this->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function next()
|
||||||
|
{
|
||||||
|
++$this->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function valid()
|
||||||
|
{
|
||||||
|
return isset($this->items[$this->position]);
|
||||||
|
}
|
||||||
|
}
|
13
tests/unit/Converter/fixtures/FillTest/CollectionItem.php
Normal file
13
tests/unit/Converter/fixtures/FillTest/CollectionItem.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||||
|
|
||||||
|
class CollectionItem
|
||||||
|
{
|
||||||
|
public string $value;
|
||||||
|
|
||||||
|
public function __construct(string $value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
}
|
@ -21,4 +21,5 @@ class HelloRequest
|
|||||||
public BarInterface $bar;
|
public BarInterface $bar;
|
||||||
#[PropertyPath(path: 'uuid.id')]
|
#[PropertyPath(path: 'uuid.id')]
|
||||||
public UUID $uuid;
|
public UUID $uuid;
|
||||||
|
public Collection $collection;
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,9 @@ class HelloSchema extends Schema
|
|||||||
'bar' => [
|
'bar' => [
|
||||||
'barField'
|
'barField'
|
||||||
],
|
],
|
||||||
'uuid'
|
'uuid',
|
||||||
|
'collection' => [
|
||||||
|
'value'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user