Поправил логику обхода массивов.

добавил тесты
master
Rinsvent 3 years ago
parent 5d35178bdf
commit 333de3fdc8
  1. 43
      src/Dto2DataConverter.php
  2. 70
      tests/unit/Converter/FillTest.php
  3. 2
      tests/unit/Converter/fixtures/FillTest/HelloRequest.php
  4. 4
      tests/unit/Converter/fixtures/FillTest/HelloSchema.php

@ -42,27 +42,12 @@ class Dto2DataConverter
$value = $this->grabValue($object, $sourceName); $value = $this->grabValue($object, $sourceName);
if (is_object($value)) { // Если нет карты, то не сериализуем.
// Если у объекта нет карты, то не сериализуем. if (is_object($value) && is_array($propertyInfo)) {
if (!is_array($propertyInfo)) {
continue;
}
$value = $this->convertObjectByMap($value, $propertyInfo, $tags); $value = $this->convertObjectByMap($value, $propertyInfo, $tags);
} elseif (is_iterable($value)) { } elseif (is_iterable($value)) {
$tempValue = []; $map = is_array($propertyInfo) ? $propertyInfo : null;
foreach ($value as $item) { $value = $this->convertArrayByMap($value, $map, $tags);
if (is_scalar($item)) {
$tempValue[] = $item;
continue;
}
if (is_array($item)) {
continue;
}
if (is_object($item)) {
$tempValue[] = $this->convertObjectByMap($item, $propertyInfo, $tags);
}
}
$value = $tempValue;
} elseif (!is_scalar($value) && null !== $value) { } elseif (!is_scalar($value) && null !== $value) {
continue; continue;
} }
@ -77,6 +62,26 @@ class Dto2DataConverter
return $data; return $data;
} }
public function convertArrayByMap($data, ?array $map, array $tags = []): ?array
{
$tempValue = [];
foreach ($data as $key => $item) {
if (is_scalar($item)) {
$tempValue[$key] = $item;
continue;
}
if (is_iterable($item) && $map) {
$tempValue[$key] = $this->convertArrayByMap($item, $map, $tags);
continue;
}
if (is_object($item) && $map) {
$tempValue[$key] = $this->convertObjectByMap($item, $map, $tags);
continue;
}
}
return $tempValue ?: null;
}
protected function grabValue(object $object, $sourceName) protected function grabValue(object $object, $sourceName)
{ {
if (method_exists($object, $sourceName)) { if (method_exists($object, $sourceName)) {

@ -43,6 +43,22 @@ class FillTest extends \Codeception\Test\Unit
$author1, $author1,
$author2 $author2
]; ];
$helloRequest->authors2 = [
[
"name" => "Tolkien"
],
[
"name" => "Sapkovsky"
]
];
$helloRequest->authors3 = [
[
"name" => "Tolkien"
],
[
"name" => "Sapkovsky"
]
];
$buy = new BuyRequest(); $buy = new BuyRequest();
$buy->phrase = 'Buy buy!!!'; $buy->phrase = 'Buy buy!!!';
$buy->length = 10; $buy->length = 10;
@ -53,25 +69,41 @@ class FillTest extends \Codeception\Test\Unit
$helloRequest->bar = $bar; $helloRequest->bar = $bar;
$dto = $dto2DataConverter->convert($helloRequest); $dto = $dto2DataConverter->convert($helloRequest);
// codecept_debug(json_encode($dto));
// $this->assertEquals('asdf', $dto->surname); $this->assertEquals([
// $this->assertEquals(3, $dto->age); "surname" => "asdf",
// $this->assertEquals([ "fake_age" => 3,
// 'sfdgsa', "emails" => [
// 'af234f', "sfdgsa",
// 'asdf33333' "af234f",
// ], $dto->emails); "asdf33333"
// $this->assertInstanceOf(BuyRequest::class, $dto->buy); ],
// $this->assertEquals('Buy buy!!!', $dto->buy->phrase); "authors" => [
// $this->assertEquals(10, $dto->buy->length); [
// $this->assertEquals(true, $dto->buy->isFirst); "name" => "Tolkien"
// ],
// $this->assertCount(2, $dto->authors); [
// $this->assertEquals('Tolkien', $dto->authors[0]->name); "name" => "Sapkovsky"
// $this->assertEquals('Sapkovsky', $dto->authors[1]->name); ]
// ],
// $this->assertInstanceOf(Bar::class, $dto->bar); "authors2" => [
// $this->assertIsFloat($dto->bar->barField); [
// $this->assertEquals(32.0, $dto->bar->barField); "name" => "Tolkien"
],
[
"name" => "Sapkovsky"
]
],
"authors3" => null,
"buy" => [
"phrase" => "Buy buy!!!",
"length" => 10,
"isFirst" => true
],
"bar" => [
"barField" => 32
]
], $dto);
} }
} }

@ -14,6 +14,8 @@ class HelloRequest
public int $age; public int $age;
public array $emails; public array $emails;
public array $authors; public array $authors;
public array $authors2;
public array $authors3;
public BuyRequest $buy; public BuyRequest $buy;
public BarInterface $bar; public BarInterface $bar;
} }

@ -14,6 +14,10 @@ class HelloSchema extends Schema
'authors' => [ 'authors' => [
'name', 'name',
], ],
'authors2' => [
'name',
],
'authors3',
'buy' => [ 'buy' => [
'phrase', 'phrase',
'length', 'length',

Loading…
Cancel
Save