Skip to content

Allows nested private properties nullation#6

Open
marcortola wants to merge 1 commit intotarifhaus:masterfrom
marcortola:feature/property-accessor-closure-nullator
Open

Allows nested private properties nullation#6
marcortola wants to merge 1 commit intotarifhaus:masterfrom
marcortola:feature/property-accessor-closure-nullator

Conversation

@marcortola
Copy link

Allows nested private properties nullation by using PropertyAccessor and Reflection

@marcortola
Copy link
Author

marcortola commented Feb 9, 2020

Another solution would consist of simplifying the implementation of Nullator, slightly modifying the Listener and making the Listener configuration more verbose (cascade mapping):

public function postLoad($object)
{
    $entity = get_class($object);

    if (empty($this->propertyMap[$entity])) {
        return;
    }

    $entries = $this->propertyMap[$entity];

    foreach ($entries as $property) {
        if ($this->isNull($object, $property)) {
            $this->setNull($object, $property);
        }

        $guessedChildEmbeddable = $this->getGuessedChildEmbeddable($object, $property);

        if (null !== $guessedChildEmbeddable && !empty($this->propertyMap[get_class($guessedChildEmbeddable)])) {
            $this->postLoad($guessedChildEmbeddable);

            return;
        }
   }
}

private function getGuessedChildEmbeddable($object, $property)
{
    try {
        $reflectionClass = new \ReflectionClass(get_class($object));
        $reflectionProperty = $reflectionClass->getProperty($property);
    } catch (\ReflectionException $exception) {
        return null;
    }

    $reflectionProperty->setAccessible(true);

    return $reflectionProperty->getValue($object);
}
public function setNull(&$object, $property): void
{
    try {
        $this->propertyAccessor->setValue($object, $property, null);
    } catch (NoSuchPropertyException $exception) {
        $this->closureNullator->setNull($object, $property);
    }
}
$listener = NullableEmbeddableListenerFactory::createWithPropertyAccessorReflectionNullator();
$listener->addMapping('App\Domain\User\Model\UserProfile', 'fiscalData');
$listener->addMapping('App\Domain\Taxation\Model\FiscalData', 'address');

// Instead of
$listener = NullableEmbeddableListenerFactory::createWithPropertyAccessorReflectionNullator();
$listener->addMapping('App\Domain\User\Model\UserProfile', 'fiscalData.address');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant