mark-gerarts / automapper-plus

An AutoMapper for PHP
MIT License
551 stars 30 forks source link

Custom Mapper with reverse map #61

Closed metham closed 3 years ago

metham commented 3 years ago

Hello everyone,

This package looks amazing and I am currently trying to use it for a project. I was wondering if it was possible to use the method reverseMap() on a custom mapper if it only maps attributes to other attributes without any work on the values itself ?

Or doing a custom mapper for this isn't the best practice with this package ? I wanted to use a custom mapper since I will have a lot of mappers to create and it seemed cleaner to do it that way.

Thanks in advance, Have a nice day !

mark-gerarts commented 3 years ago

Hi @metham. reverseMap applies to configuration you set with forMember (and only operations that are reversible). By setting a custom mapper you bypass this, since all the work is done by your custom mapper instead. Calling reverseMap won't work then.

Do you have an example of the data you're mapping? Since you mention that you don't do anything with the values itself, do you mean that you're dealling with differently name property names, or are you dealing with nested properties?

If you're dealing with nested properties, you will have to handle that manually for now. An idea for v2 of the library is to allow this using the Symfony property path notation, but that's still a long way ahead.

For differently named properties you should be able to implement your own NameResolver based on an associative array of properties. I haven't done this myself before, but I think it should be possible. In any case it would be a nice feature to have in v2 as well.

metham commented 3 years ago

Hello @mark-gerarts, thank you for your fast answer.

Acutally a lot of things changed with the values I am dealing with and I have to make some modification before mapping them, I am now sure I can't use reverseMap in anyway now.

I am dealing with nested properties but only on one side of the mapping.

Thank you for the idea on the NameResolver, I will look into it !

I should close this issue since it's not a problem anymore, thank you for your time !