Поправил логику обхода массивов.
добавил тесты
This commit is contained in:
parent
5d35178bdf
commit
333de3fdc8
@ -42,27 +42,12 @@ class Dto2DataConverter
|
||||
|
||||
$value = $this->grabValue($object, $sourceName);
|
||||
|
||||
if (is_object($value)) {
|
||||
// Если у объекта нет карты, то не сериализуем.
|
||||
if (!is_array($propertyInfo)) {
|
||||
continue;
|
||||
}
|
||||
// Если нет карты, то не сериализуем.
|
||||
if (is_object($value) && is_array($propertyInfo)) {
|
||||
$value = $this->convertObjectByMap($value, $propertyInfo, $tags);
|
||||
} elseif (is_iterable($value)) {
|
||||
$tempValue = [];
|
||||
foreach ($value as $item) {
|
||||
if (is_scalar($item)) {
|
||||
$tempValue[] = $item;
|
||||
continue;
|
||||
}
|
||||
if (is_array($item)) {
|
||||
continue;
|
||||
}
|
||||
if (is_object($item)) {
|
||||
$tempValue[] = $this->convertObjectByMap($item, $propertyInfo, $tags);
|
||||
}
|
||||
}
|
||||
$value = $tempValue;
|
||||
$map = is_array($propertyInfo) ? $propertyInfo : null;
|
||||
$value = $this->convertArrayByMap($value, $map, $tags);
|
||||
} elseif (!is_scalar($value) && null !== $value) {
|
||||
continue;
|
||||
}
|
||||
@ -77,6 +62,26 @@ class Dto2DataConverter
|
||||
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)
|
||||
{
|
||||
if (method_exists($object, $sourceName)) {
|
||||
|
@ -43,6 +43,22 @@ class FillTest extends \Codeception\Test\Unit
|
||||
$author1,
|
||||
$author2
|
||||
];
|
||||
$helloRequest->authors2 = [
|
||||
[
|
||||
"name" => "Tolkien"
|
||||
],
|
||||
[
|
||||
"name" => "Sapkovsky"
|
||||
]
|
||||
];
|
||||
$helloRequest->authors3 = [
|
||||
[
|
||||
"name" => "Tolkien"
|
||||
],
|
||||
[
|
||||
"name" => "Sapkovsky"
|
||||
]
|
||||
];
|
||||
$buy = new BuyRequest();
|
||||
$buy->phrase = 'Buy buy!!!';
|
||||
$buy->length = 10;
|
||||
@ -53,25 +69,41 @@ class FillTest extends \Codeception\Test\Unit
|
||||
$helloRequest->bar = $bar;
|
||||
|
||||
$dto = $dto2DataConverter->convert($helloRequest);
|
||||
// codecept_debug(json_encode($dto));
|
||||
|
||||
// $this->assertEquals('asdf', $dto->surname);
|
||||
// $this->assertEquals(3, $dto->age);
|
||||
// $this->assertEquals([
|
||||
// 'sfdgsa',
|
||||
// 'af234f',
|
||||
// 'asdf33333'
|
||||
// ], $dto->emails);
|
||||
// $this->assertInstanceOf(BuyRequest::class, $dto->buy);
|
||||
// $this->assertEquals('Buy buy!!!', $dto->buy->phrase);
|
||||
// $this->assertEquals(10, $dto->buy->length);
|
||||
// $this->assertEquals(true, $dto->buy->isFirst);
|
||||
//
|
||||
// $this->assertCount(2, $dto->authors);
|
||||
// $this->assertEquals('Tolkien', $dto->authors[0]->name);
|
||||
// $this->assertEquals('Sapkovsky', $dto->authors[1]->name);
|
||||
//
|
||||
// $this->assertInstanceOf(Bar::class, $dto->bar);
|
||||
// $this->assertIsFloat($dto->bar->barField);
|
||||
// $this->assertEquals(32.0, $dto->bar->barField);
|
||||
$this->assertEquals([
|
||||
"surname" => "asdf",
|
||||
"fake_age" => 3,
|
||||
"emails" => [
|
||||
"sfdgsa",
|
||||
"af234f",
|
||||
"asdf33333"
|
||||
],
|
||||
"authors" => [
|
||||
[
|
||||
"name" => "Tolkien"
|
||||
],
|
||||
[
|
||||
"name" => "Sapkovsky"
|
||||
]
|
||||
],
|
||||
"authors2" => [
|
||||
[
|
||||
"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 array $emails;
|
||||
public array $authors;
|
||||
public array $authors2;
|
||||
public array $authors3;
|
||||
public BuyRequest $buy;
|
||||
public BarInterface $bar;
|
||||
}
|
||||
|
@ -14,6 +14,10 @@ class HelloSchema extends Schema
|
||||
'authors' => [
|
||||
'name',
|
||||
],
|
||||
'authors2' => [
|
||||
'name',
|
||||
],
|
||||
'authors3',
|
||||
'buy' => [
|
||||
'phrase',
|
||||
'length',
|
||||
|
Loading…
Reference in New Issue
Block a user