Skip to content

Commit

Permalink
[Serializer] Improve AttributeLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored and nicolas-grekas committed Oct 22, 2024
1 parent 7c0c971 commit 56bcd41
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions Mapping/Loader/AttributeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,12 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
$attributesMetadata = $classMetadata->getAttributesMetadata();

foreach ($this->loadAttributes($reflectionClass) as $annotation) {
if ($annotation instanceof DiscriminatorMap) {
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
$annotation->getTypeProperty(),
$annotation->getMapping()
));
continue;
}

if ($annotation instanceof Groups) {
$classGroups = $annotation->getGroups();

continue;
}

if ($annotation instanceof Context) {
$classContextAnnotation = $annotation;
}
match (true) {
$annotation instanceof DiscriminatorMap => $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping($annotation->getTypeProperty(), $annotation->getMapping())),
$annotation instanceof Groups => $classGroups = $annotation->getGroups(),
$annotation instanceof Context => $classContextAnnotation = $annotation,
default => null,
};
}

foreach ($reflectionClass->getProperties() as $property) {
Expand All @@ -83,33 +72,35 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
}

$attributeMetadata = $attributesMetadata[$property->name];
if ($property->getDeclaringClass()->name === $className) {
if ($classContextAnnotation) {
$this->setAttributeContextsForGroups($classContextAnnotation, $attributesMetadata[$property->name]);
$this->setAttributeContextsForGroups($classContextAnnotation, $attributeMetadata);
}

foreach ($classGroups as $group) {
$attributesMetadata[$property->name]->addGroup($group);
$attributeMetadata->addGroup($group);
}

foreach ($this->loadAttributes($property) as $annotation) {
$loaded = true;

if ($annotation instanceof Groups) {
foreach ($annotation->getGroups() as $group) {
$attributesMetadata[$property->name]->addGroup($group);
$attributeMetadata->addGroup($group);
}
} elseif ($annotation instanceof MaxDepth) {
$attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
} elseif ($annotation instanceof SerializedName) {
$attributesMetadata[$property->name]->setSerializedName($annotation->getSerializedName());
} elseif ($annotation instanceof SerializedPath) {
$attributesMetadata[$property->name]->setSerializedPath($annotation->getSerializedPath());
} elseif ($annotation instanceof Ignore) {
$attributesMetadata[$property->name]->setIgnore(true);
} elseif ($annotation instanceof Context) {
$this->setAttributeContextsForGroups($annotation, $attributesMetadata[$property->name]);

continue;
}

$loaded = true;
match (true) {
$annotation instanceof MaxDepth => $attributeMetadata->setMaxDepth($annotation->getMaxDepth()),
$annotation instanceof SerializedName => $attributeMetadata->setSerializedName($annotation->getSerializedName()),
$annotation instanceof SerializedPath => $attributeMetadata->setSerializedPath($annotation->getSerializedPath()),
$annotation instanceof Ignore => $attributeMetadata->setIgnore(true),
$annotation instanceof Context => $this->setAttributeContextsForGroups($annotation, $attributeMetadata),
default => null,
};
}
}
}
Expand Down Expand Up @@ -206,17 +197,17 @@ private function loadAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionPr

private function setAttributeContextsForGroups(Context $annotation, AttributeMetadataInterface $attributeMetadata): void
{
if ($annotation->getContext()) {
$attributeMetadata->setNormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
$attributeMetadata->setDenormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
}
$context = $annotation->getContext();
$groups = $annotation->getGroups();
$normalizationContext = $annotation->getNormalizationContext();
$denormalizationContext = $annotation->getDenormalizationContext();

if ($annotation->getNormalizationContext()) {
$attributeMetadata->setNormalizationContextForGroups($annotation->getNormalizationContext(), $annotation->getGroups());
if ($normalizationContext || $context) {
$attributeMetadata->setNormalizationContextForGroups($normalizationContext ?: $context, $groups);
}

if ($annotation->getDenormalizationContext()) {
$attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups());
if ($denormalizationContext || $context) {
$attributeMetadata->setDenormalizationContextForGroups($denormalizationContext ?: $context, $groups);
}
}

Expand Down

0 comments on commit 56bcd41

Please sign in to comment.