Coder Social home page Coder Social logo

rekalogika / mapper Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 0.0 1.03 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.70% Makefile 0.07% Twig 2.22%
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

Watchers

 avatar  avatar  avatar

mapper's Issues

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

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

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