Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mapper): allow mapping value to object via cast method #887

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Jeroen-G
Copy link
Contributor

This PR allows the following code:

$data = ['name' => 'Jeroen', 'updated' => new DateTime('now')];
$user = map($data)->to(User::class);
final class User
{
    public function __construct(
        public Name $name,
        public DateTime $updated,
    ){}
}
final class Name
{
    private function __construct(
        private string $name,
    )
    {}

    public static function cast(mixed $value): self
    {
        return new self((string) $value);
    }
}

So what is now possible is the mapping of any data, also nested within arrays, to be mapped to objects without having to create a CastWith caster for each of them. For me this removes the papercut that for every (small) value object I had to create a corresponding caster.

@Jeroen-G Jeroen-G changed the title allow mapping value to object via cast method feat(Mapper): allow mapping value to object via cast method Jan 24, 2025
@Jeroen-G Jeroen-G force-pushed the objectmapper branch 2 times, most recently from 9055f20 to 0acc323 Compare January 24, 2025 09:29
@Jeroen-G
Copy link
Contributor Author

I don't see how all those tests fail due to these changes 🤨

@innocenzi innocenzi changed the title feat(Mapper): allow mapping value to object via cast method feat(mapper): allow mapping value to object via cast method Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant