Добавил поддержку Traversable

master v0.0.5
Rinsvent 3 years ago
parent 77c0c14c14
commit 20317698e9
  1. 2
      src/Dto2DataConverter.php
  2. 22
      tests/unit/Converter/FillTest.php
  3. 34
      tests/unit/Converter/fixtures/FillTest/Collection.php
  4. 13
      tests/unit/Converter/fixtures/FillTest/CollectionItem.php
  5. 1
      tests/unit/Converter/fixtures/FillTest/HelloRequest.php
  6. 5
      tests/unit/Converter/fixtures/FillTest/HelloSchema.php

@ -65,7 +65,7 @@ class Dto2DataConverter
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 = [];
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\Bar;
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\UUID;
@ -69,6 +71,13 @@ class FillTest extends \Codeception\Test\Unit
$bar->barField = 32;
$helloRequest->bar = $bar;
$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);
// codecept_debug(json_encode($dto));
@ -106,7 +115,18 @@ class FillTest extends \Codeception\Test\Unit
"bar" => [
"barField" => 32
],
'uuid' => 'qwerqw-qwerqwe-werqw-qwerqw'
'uuid' => 'qwerqw-qwerqwe-werqw-qwerqw',
'collection' => [
[
'value' => '3',
],
[
'value' => '1',
],
[
'value' => '2',
],
],
], $dto);
}
}

@ -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]);
}
}

@ -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;
#[PropertyPath(path: 'uuid.id')]
public UUID $uuid;
public Collection $collection;
}

@ -26,6 +26,9 @@ class HelloSchema extends Schema
'bar' => [
'barField'
],
'uuid'
'uuid',
'collection' => [
'value'
],
];
}

Loading…
Cancel
Save