From 333de3fdc8ab0a1ca5bcd63c45106904b68bade4 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Thu, 26 Aug 2021 00:02:43 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=D0=BE=D0=B2.=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Dto2DataConverter.php | 43 +++++++----- tests/unit/Converter/FillTest.php | 70 ++++++++++++++----- .../fixtures/FillTest/HelloRequest.php | 2 + .../fixtures/FillTest/HelloSchema.php | 4 ++ 4 files changed, 81 insertions(+), 38 deletions(-) diff --git a/src/Dto2DataConverter.php b/src/Dto2DataConverter.php index c3c3a9d..8071b4a 100644 --- a/src/Dto2DataConverter.php +++ b/src/Dto2DataConverter.php @@ -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)) { diff --git a/tests/unit/Converter/FillTest.php b/tests/unit/Converter/FillTest.php index 8bbfa6e..531a173 100644 --- a/tests/unit/Converter/FillTest.php +++ b/tests/unit/Converter/FillTest.php @@ -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); } } diff --git a/tests/unit/Converter/fixtures/FillTest/HelloRequest.php b/tests/unit/Converter/fixtures/FillTest/HelloRequest.php index b9f8589..bd42c9b 100644 --- a/tests/unit/Converter/fixtures/FillTest/HelloRequest.php +++ b/tests/unit/Converter/fixtures/FillTest/HelloRequest.php @@ -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; } diff --git a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php index 5d26d30..d21b5c9 100644 --- a/tests/unit/Converter/fixtures/FillTest/HelloSchema.php +++ b/tests/unit/Converter/fixtures/FillTest/HelloSchema.php @@ -14,6 +14,10 @@ class HelloSchema extends Schema 'authors' => [ 'name', ], + 'authors2' => [ + 'name', + ], + 'authors3', 'buy' => [ 'phrase', 'length',