Skip to content

[Question] Is it possible to map objects with different property names without creating a custom transformer? #2

Answered by priyadi
ToshY asked this question in Q&A
Discussion options

You must be logged in to vote

Currently, it is possible to do that using #[AsPropertyMapper]. No, you don't have to create an entire transformer for that.

use Rekalogika\Mapper\Attribute\AsPropertyMapper;

class CustomMapper
{
    #[AsPropertyMapper(
        targetClass: BookDto::class,
        property: 'name',
    )]
    public function mapBookTitle(Book $book): string
    {
        return $book->getName();
    }
}

Docs: https://rekalogika.dev/mapper/object#custom-property-mapper

However, MapFrom (or a similar attribute) is in my to-do list.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ToshY
Comment options

Answer selected by ToshY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1 on February 11, 2024 03:33.