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

Commit

Permalink
Merge pull request #304 from spatie/fix-positional-after-named
Browse files Browse the repository at this point in the history
Fix positional after named
  • Loading branch information
aidan-casey authored Sep 16, 2022
2 parents 5e913d4 + 54235a5 commit 1df0906
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Casters/DataTransferObjectCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function cast(mixed $value): DataTransferObject
}
}

return new $this->classNames[0](...$value);
return new $this->classNames[0]($value);
}
}
40 changes: 40 additions & 0 deletions tests/MapFromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1df0906

Please sign in to comment.