automapper-plus icon indicating copy to clipboard operation
automapper-plus copied to clipboard

What about polymorphism?

Open praad opened this issue 3 years ago • 7 comments

Is there any easy way to map polymorph property?

The polymorph property can be class A or B or C. Both classes extends the same abstract classes. Unfortunately I can not make a mapper for the abstract class itself.

praad avatar Jan 22 '21 15:01 praad

Polymorphism is supported. If no mapping is found, we will look up the inheritance chain to see if a parent mapping is registered. Take a look at the following snipper:

abstract class BaseClass
{
    public $sharedProperty = 'shared';
}

class A extends BaseClass
{
}

$config = new AutoMapperConfig();
$mapper = new AutoMapper($config);

$config->registerMapping(BaseClass::class, \stdClass::class)
    ->forMember('sharedProperty', Operation::setTo('Set in base mapping'));

// This works because A extends the BaseClass, which is registered.
$result = $mapper->map(new A(), \stdClass::class);
var_dump($result->sharedProperty); // "Set in base mapping"

// If you register a more specific mapping for A, that one is used instead
$config->registerMapping(A::class, \stdClass::class);
$result = $mapper->map(new A(), \stdClass::class);
var_dump($result->sharedProperty); // "shared"

// ...but you can copy from the base mapping to prevent duplicate code.
$config->registerMapping(A::class, \stdClass::class)
    ->copyFrom(BaseClass::class, \stdClass::class);
$result = $mapper->map(new A(), \stdClass::class);
var_dump($result->sharedProperty); // "Set in base mapping"

I've used \stdClass here, because I noticed a bug with polymorphism when mapping to/from array - perhaps this is what you are doing? I'll look into fixing it.

mark-gerarts avatar Jan 23 '21 15:01 mark-gerarts

Mapping from an array has been fixed (mapping to an array is not available in 1.x).

Let me know if the above provides an answer for you!

mark-gerarts avatar Jan 23 '21 15:01 mark-gerarts

@praad I believe you want to map that polymorphic property to something like this? A -> ADto B -> BDto C -> CDto

If so check out my recent pull request https://github.com/mark-gerarts/automapper-plus/pull/70 where I added mapToMultiple operation

peter-si avatar Jan 28 '21 14:01 peter-si

Hi Peter! Yes I need a solution like this. How can I include your new feature by composer.json to our existing project to test?

praad avatar Feb 15 '21 15:02 praad

@praad try adding my repo as private repository and add it to composer json as

    "require": {
        "peter-si/automapper-plus": "dev-map_to_any_of"
    },

peter-si avatar Feb 22 '21 08:02 peter-si

I'll look into the PR this weekend, and if all is well I'll tag a new release.

mark-gerarts avatar Feb 22 '21 08:02 mark-gerarts

Can close this issue since #70 already merged.

fd6130 avatar Apr 29 '22 15:04 fd6130