From 69c16976fc23eaa6a24e1a32cf0f68ed2496e831 Mon Sep 17 00:00:00 2001 From: Kehet <382965-kehet@users.noreply.gitlab.com> Date: Fri, 16 Sep 2022 02:00:25 +0300 Subject: [PATCH 1/3] Add test for "Cannot use positional argument after named argument during unpacking" error --- tests/MapFromTest.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/MapFromTest.php b/tests/MapFromTest.php index 240cd8eb..234d3023 100644 --- a/tests/MapFromTest.php +++ b/tests/MapFromTest.php @@ -102,4 +102,43 @@ public function mapped_from_works_with_default_values() $this->assertFalse($dto->isPublic); $this->assertEquals(42, $dto->randomInt); } + + /** @test */ + public function dto_can_have_numeric_keys() + { + $data = [ + 'title' => 'Hello world', + '0' => 10, + ]; + + $dto = new DTOInner($data); + + $this->assertEquals('Hello world', $dto->title); + $this->assertEquals(10, $dto->zero); + } + + /** @test */ + public function dto_can_have_numeric_keys_in_nested_dto() + { + $data = [ + 'innerDTO' => [ + 'title' => 'Hello world', + '0' => 10, + ], + ]; + + $dtoOuter = new class ($data) extends DataTransferObject { + public DTOInner $innerDTO; + }; + + $this->assertEquals('Hello world', $dtoOuter->innerDTO->title); + $this->assertEquals(10, $dtoOuter->innerDTO->zero); + } +} + +class DTOInner extends DataTransferObject { + public string $title; + + #[MapFrom('0')] + public int $zero; } From 6f88c8b870ac4232235a7cd610d086b012b28b86 Mon Sep 17 00:00:00 2001 From: aidan-casey Date: Fri, 16 Sep 2022 12:26:40 +0000 Subject: [PATCH 2/3] Fix styling --- tests/MapFromTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/MapFromTest.php b/tests/MapFromTest.php index 234d3023..cbec422a 100644 --- a/tests/MapFromTest.php +++ b/tests/MapFromTest.php @@ -136,7 +136,8 @@ public function dto_can_have_numeric_keys_in_nested_dto() } } -class DTOInner extends DataTransferObject { +class DTOInner extends DataTransferObject +{ public string $title; #[MapFrom('0')] From 54235a5ee670cab859cdc756fbd4185dfc493aaa Mon Sep 17 00:00:00 2001 From: Aidan Casey Date: Fri, 16 Sep 2022 08:40:29 -0400 Subject: [PATCH 3/3] Removes array unpacking during DTO casting. --- src/Casters/DataTransferObjectCaster.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Casters/DataTransferObjectCaster.php b/src/Casters/DataTransferObjectCaster.php index 4f1913e9..6a0383d0 100644 --- a/src/Casters/DataTransferObjectCaster.php +++ b/src/Casters/DataTransferObjectCaster.php @@ -20,6 +20,6 @@ public function cast(mixed $value): DataTransferObject } } - return new $this->classNames[0](...$value); + return new $this->classNames[0]($value); } }