Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows nested private properties nullation #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Allows nested private properties nullation #6

wants to merge 1 commit into from

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.

None yet

1 participant