Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

Commit 1df0906

Browse files
authored
Merge pull request #304 from spatie/fix-positional-after-named
Fix positional after named
2 parents 5e913d4 + 54235a5 commit 1df0906

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Casters/DataTransferObjectCaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function cast(mixed $value): DataTransferObject
2020
}
2121
}
2222

23-
return new $this->classNames[0](...$value);
23+
return new $this->classNames[0]($value);
2424
}
2525
}

tests/MapFromTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,44 @@ public function mapped_from_works_with_default_values()
102102
$this->assertFalse($dto->isPublic);
103103
$this->assertEquals(42, $dto->randomInt);
104104
}
105+
106+
/** @test */
107+
public function dto_can_have_numeric_keys()
108+
{
109+
$data = [
110+
'title' => 'Hello world',
111+
'0' => 10,
112+
];
113+
114+
$dto = new DTOInner($data);
115+
116+
$this->assertEquals('Hello world', $dto->title);
117+
$this->assertEquals(10, $dto->zero);
118+
}
119+
120+
/** @test */
121+
public function dto_can_have_numeric_keys_in_nested_dto()
122+
{
123+
$data = [
124+
'innerDTO' => [
125+
'title' => 'Hello world',
126+
'0' => 10,
127+
],
128+
];
129+
130+
$dtoOuter = new class ($data) extends DataTransferObject {
131+
public DTOInner $innerDTO;
132+
};
133+
134+
$this->assertEquals('Hello world', $dtoOuter->innerDTO->title);
135+
$this->assertEquals(10, $dtoOuter->innerDTO->zero);
136+
}
137+
}
138+
139+
class DTOInner extends DataTransferObject
140+
{
141+
public string $title;
142+
143+
#[MapFrom('0')]
144+
public int $zero;
105145
}

0 commit comments

Comments
 (0)