mark-gerarts / automapper-plus

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

Combine Operation::fromProperty and Operation::mapTo #17

Closed smoelker closed 6 years ago

smoelker commented 6 years ago

I'm trying to map a nested property (using mapTo) where the source property name and the target property name are different but I can't seem to find a way.

I tried mapTo and fromProperty combined:

 ->forMember('address', Operation::fromProperty('adres'))
 ->forMember('address', Operation::mapTo(Address::class))

I tried a mapTo combined with a name resolver:

->forMember('address', Operation::mapTo(Address::class))
->withNameResolver(new CallbackNameResolver(function ($targetProperty) {
    if ($targetProperty == 'address') {
        return 'adres';
    }
    return $targetProperty;
}))

None of them seem to work.

I suspect that MapFromWithMapper would do the trick but that change has not been published to packagist it seems. Am I missing something here?

mark-gerarts commented 6 years ago

You are right, MapFromWithMapper should be a solution for this. I'll bump the version so it'll become available on pacakagist.

I'll look into the following things later:

smoelker commented 6 years ago

A new operation that is also fluent would be convenient, like this:

Operation::fromProperty('adres)->mapTo(Address::class);

Just a suggestion :)

mark-gerarts commented 6 years ago

The nameresolver not working in your example was indeed a bug. I've fixed it on master, so if you want to use that, you can pull the latest version. Meanwhile I'm gonna start looking into your suggestion and see what I can do. No promises though :)

mark-gerarts commented 6 years ago

FromProperty has been extended to allow chaining as you suggested. It's sitting on master for now, so it'll be included in the next release.