rekalogika / mapper Goto Github PK
View Code? Open in Web Editor NEWAn object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.
License: MIT License
An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.
License: MIT License
Problem
Trying to map one object to another object, $this->mapper->map($dto, $entity);
fails due to invalid cache key name:
Cache key "App\DTO\BookDto:App\Entity\Book" contains reserved characters "{}()/\@:".
Cause
Invalid cache key name is created here:
Reproduction
AbstractAdapter
, which use the AbstractAdapterTrait
to validate the key name.Version
rekalogika/mapper
: v0.8.0
symfony/cache
: v7.0.3
Problem
I've noticed that nullable properties get cast to empty string (""
) instead of being kept as null
value when not using constructor property promotion in the target class.
Reproduction
src/Dto/BookDto.php
namespace App\Dto;
class BookDto
{
public function __construct(
public string|null $title = null,
) {
}
}
src/Entity/Book.php
namespace App\Entity;
class Book
{
private string|null $title = null;
public function __construct(
private int $id,
) {
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
}
Run mapper
$book = new \App\Entity\Book(1);
$this->mapper->map(new \App\Dto\BookDto(null), $book);
Result
App\Entity\Book {#2485 ▼
-id: 1
-notes: ""
}
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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.