Skip to content

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

@ToshY

Description

@ToshY

Question

Does this mapper allow to map object to another object when the property names differ, without creating a custom transfomer?

Example (from docs)

src/Entity/Book.php

namespace App\Entity;

class Book
{
    public function __construct(
        private int $id,
        private string $title,
    ) {
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }
}

src/Dto/BookDto.php

namespace App\Dto;

class BookDto
{
    public string $id;
    public string $name; // changed this to $name instead of $title
}

How would I map the Book::title to BookDto::name?


I was previously looking at thephpleague/object-mapper, which doesn't really suit fit my use case as it does not allow object-to-object mapping directly but I saw it uses attributes like #[MapFrom] which allows to allow for custom mapping between property names. I'm not sure if there's already something similar in this library, but maybe something similar would be nice to have (if not already).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions