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#6marcortola wants to merge 1 commit intotarifhaus:masterfrom marcortola:feature/property-accessor-closure-nullator
marcortola wants to merge 1 commit intotarifhaus:masterfrom
marcortola:feature/property-accessor-closure-nullator
Conversation
Author
|
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allows nested private properties nullation by using PropertyAccessor and Reflection