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
Question
Is it possible to map a DTO with nested objects to an entity and also fetch those relations?
This problems occures from DTO -> entity, from entity -> DTO the mapping populates everything as expected.
Here's an example, the simple types are fetched correctly, but not the relations, either collection (colours) or simple entity (featuredColour). Am I missing some sort of configuration?
Example:
class ProductDto
{
public ?int $productId = null;
public ?ColourDto $$featuredColour = null;
#[Assert\NotNull]
#[Assert\Type('boolean')]
public ?bool $enabled = null;
#[Assert\NotNull]
#[Assert\Type('boolean')]
public ?bool $featured = null;
/** @var ?array<int,ColourDto> */
public ?array $colours = null;
}
I would also like to know if it's possible to map array to array, is it possible in this current version?
So let's imagine an API where it returns a collection of objects, I would image it something like below
return $this->mapper->mapArray($entityCollection, Dto:class)
Thank you!
I am wondering if there can be an alternative way to define InheritanceMap? Cause current one expects me to set an attribute on the target abstract class. However, for certain architectures like Onion, Clean Architecture and other layered examples this can be a violation of responsibility, when your classes "know" about classes from higher layer. It happens on mapping from DTO to Entity object. For example:
use Rekalogika\Mapper\Attribute\InheritanceMap;
use Domain\ConcreteClassA;
use Domain\ConcreteClassB;
use Domain\ConcreteClassC;
use Presentation\RequestAObjDTO;
use Presentation\RequestBObjDTO;
use Presentation\RequestCObjDTO;
#[InheritanceMap([
RequestAObjDTO::class => ConcreteClassA::class,
RequestBObjDTO::class => ConcreteClassB::class,
RequestCObjDTO::class => ConcreteClassC::class,
])]
abstract class ConcreteClass
{
}
So in this case, to convert Presentation layer object into Domain layer object I have to define this mapping in the Domain layer. That means I will get an alert in architecture control tools like phparkitect/deptrac that my Domain layer has dependencies on Presentation layer.
I guess it would be nice to have a way to define this inheritance mapping on Presentation layer side, maybe it's possible to do this on the source class providing 2 attributes smth like SourceInheritanceMap
and TargetInheritanceMap
, depending if this class treats as a source or as a target? Or maybe pass it in Context for this specific mapping action. Just brainstorming ๐คทโโ๏ธ
P.S. Offtop, I see you've added support for ramsey/uuid - that's great, cause we are using it widely - can you pls create new release so we don't need to make "dev-master" dependency? ๐
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: ""
}
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
Just after installation of your package I got an error while running cache:clear:
In DecoratorServicePass.php line 88:
The service "rekalogika.mapper.cache.property_info.traceable" has a dependency on a non-existent service "rekalogika.mapper.cache.propert
y_info".
Symfony version 7, package version 1.1.1, environment: dev, installed with Flex, APP_DEBUG: true
Seems the reason is in DebugPass, which removes this definition from container if app_debug is enabled. But on dev environment I obviously have it enabled...
So the issue for me is - when you are trying to map onto an existing entity, which has constructor and setters - mapping does not work.
Here is an example:
class ParentObjDto
{
public string $name;
public ChildObjDto $child;
}
class ChildObjDto
{
public string $a;
}
class ParentObj
{
public function __construct(
private string $name,
private ChildObj $child,
) {
}
public function getChild(): ChildObj
{
return $this->child;
}
public function setChild(ChildObj $child): void
{
$this->child = $child;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
class ChildObj
{
public function __construct(
private string $a,
) {
}
public function getA(): string
{
return $this->a;
}
public function setA(string $a): void
{
$this->a = $a;
}
}
When you do mapping with a new object, passing target object class:
$dto = new ParentObjDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjDto();
$dto->child->a = 'dto-a';
$entity = $this->map($dto, ParentObj::class);
... you get a perfect result:
App\ParentObj {#1358
-name: "dto-name"
-child:
App\ChildObj {#1362
-a: "dto-a"
}
}
However, when you try to map onto existing object, let's say, doctrine entity fetched from DB:
$dto = new ParentObjDto();
$dto->name = 'dto-name';
$dto->child = new ChildObjDto();
$dto->child->a = 'dto-a';
$entity = new ParentObj('entity-name', new ChildObj('entity-a'));
$entity = $this->map($dto, $entity);
... you properties are not changed
App\ParentObj {#1258
-name: "entity-name"
-child:
App\ChildObj {#1362
-a: "entity-a"
}
}
Dunno, if it should be like that - but with the existence of setters, I would expect them to be used, cause you already have initialized object - no need for constructor. To make it work I need to remove properties from constructor.
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.