teamneusta/converter-bundle

[PropertyMappingPopulator] Make null safety possible

mike4git opened this issue · 3 comments

The PropertyMappingPopulator should be able to be configured that in case the source property value is null either accessor should be called or accessor should not be called (null safety).
Therefore it is recommended, that a new property $nullSafety of type bool will be introduced called nullSafety (by default: false - cause this is the current default behaviour).

What's the problem with reading null values?

The reading of null values is no problem but sometimes you don't want to set in case of a null value.
Imagine your target object should NOT be overwritten in case of null. How to handle?

We have in our project context the following situation:

        if (null !== $shopAddress->getSalutation()) {
            $apiAddress->setSalutation($shopAddress->getSalutation());
        }
        if (null !== $shopAddress->getFirstname()) {
            $apiAddress->setFirstname($shopAddress->getFirstname());
        }
        if (null !== $shopAddress->getLastname()) {
            $apiAddress->setLastname($shopAddress->getLastname());
        }
        if (null !== $shopAddress->getCompany()) {
            $apiAddress->setCompany($shopAddress->getCompany());
        }
...
 

You can not use the PropertyMappingPopulator here. Otherwise you would set target properties to null.