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); } } diff --git a/tests/MapFromTest.php b/tests/MapFromTest.php index 240cd8eb..cbec422a 100644 --- a/tests/MapFromTest.php +++ b/tests/MapFromTest.php @@ -102,4 +102,44 @@ 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; }