-
Question Does this mapper allow to map object to another object when the property names differ, without creating a custom transfomer? Example (from docs)
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;
}
}
namespace App\Dto;
class BookDto
{
public string $id;
public string $name; // changed this to $name instead of $title
} How would I map the 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 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently, it is possible to do that using 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, |
Beta Was this translation helpful? Give feedback.
Currently, it is possible to do that using
#[AsPropertyMapper]
. No, you don't have to create an entire transformer for that.Docs: https://rekalogika.dev/mapper/object#custom-property-mapper
However,
MapFrom
(or a similar attribute) is in my to-do list.