GithubHelp home page GithubHelp logo

rekalogika / mapper Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 0.0 1.05 MB

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

PHP 97.69% Makefile 0.07% Twig 2.24%
api api-platform automapper dto mapper mapping php symfony transformation

mapper's People

Contributors

dependabot[bot] avatar priyadi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mapper's Issues

[Question] - Nested objects

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!

Alternative way to define InheritanceMap

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? ๐Ÿ˜‰

Mapper casts nulllable property to string when not using constructor property promotion in target class

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: ""
}

Cache key contains reserved characters `{}()/\@:`

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

  • Symfony setup (6.4+) with Redis cache adapter
    • It should not matter which type of cache adapter you'd use to reproduce, as all adapters extend from the Symfony AbstractAdapter, which use the AbstractAdapterTrait to validate the key name.

Version

  • rekalogika/mapper: v0.8.0
  • symfony/cache: v7.0.3

Non-existent service "rekalogika.mapper.cache.property_info"

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...

Setter does not get called if the property is also in the constructor

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] Is it possible to map objects with different property names without creating a custom transformer?

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.