Skip to content

Commit 56bcd41

Browse files
dunglasnicolas-grekas
authored andcommitted
[Serializer] Improve AttributeLoader
1 parent 7c0c971 commit 56bcd41

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

Mapping/Loader/AttributeLoader.php

+30-39
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,12 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
5858
$attributesMetadata = $classMetadata->getAttributesMetadata();
5959

6060
foreach ($this->loadAttributes($reflectionClass) as $annotation) {
61-
if ($annotation instanceof DiscriminatorMap) {
62-
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
63-
$annotation->getTypeProperty(),
64-
$annotation->getMapping()
65-
));
66-
continue;
67-
}
68-
69-
if ($annotation instanceof Groups) {
70-
$classGroups = $annotation->getGroups();
71-
72-
continue;
73-
}
74-
75-
if ($annotation instanceof Context) {
76-
$classContextAnnotation = $annotation;
77-
}
61+
match (true) {
62+
$annotation instanceof DiscriminatorMap => $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping($annotation->getTypeProperty(), $annotation->getMapping())),
63+
$annotation instanceof Groups => $classGroups = $annotation->getGroups(),
64+
$annotation instanceof Context => $classContextAnnotation = $annotation,
65+
default => null,
66+
};
7867
}
7968

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

75+
$attributeMetadata = $attributesMetadata[$property->name];
8676
if ($property->getDeclaringClass()->name === $className) {
8777
if ($classContextAnnotation) {
88-
$this->setAttributeContextsForGroups($classContextAnnotation, $attributesMetadata[$property->name]);
78+
$this->setAttributeContextsForGroups($classContextAnnotation, $attributeMetadata);
8979
}
9080

9181
foreach ($classGroups as $group) {
92-
$attributesMetadata[$property->name]->addGroup($group);
82+
$attributeMetadata->addGroup($group);
9383
}
9484

9585
foreach ($this->loadAttributes($property) as $annotation) {
86+
$loaded = true;
87+
9688
if ($annotation instanceof Groups) {
9789
foreach ($annotation->getGroups() as $group) {
98-
$attributesMetadata[$property->name]->addGroup($group);
90+
$attributeMetadata->addGroup($group);
9991
}
100-
} elseif ($annotation instanceof MaxDepth) {
101-
$attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
102-
} elseif ($annotation instanceof SerializedName) {
103-
$attributesMetadata[$property->name]->setSerializedName($annotation->getSerializedName());
104-
} elseif ($annotation instanceof SerializedPath) {
105-
$attributesMetadata[$property->name]->setSerializedPath($annotation->getSerializedPath());
106-
} elseif ($annotation instanceof Ignore) {
107-
$attributesMetadata[$property->name]->setIgnore(true);
108-
} elseif ($annotation instanceof Context) {
109-
$this->setAttributeContextsForGroups($annotation, $attributesMetadata[$property->name]);
92+
93+
continue;
11094
}
11195

112-
$loaded = true;
96+
match (true) {
97+
$annotation instanceof MaxDepth => $attributeMetadata->setMaxDepth($annotation->getMaxDepth()),
98+
$annotation instanceof SerializedName => $attributeMetadata->setSerializedName($annotation->getSerializedName()),
99+
$annotation instanceof SerializedPath => $attributeMetadata->setSerializedPath($annotation->getSerializedPath()),
100+
$annotation instanceof Ignore => $attributeMetadata->setIgnore(true),
101+
$annotation instanceof Context => $this->setAttributeContextsForGroups($annotation, $attributeMetadata),
102+
default => null,
103+
};
113104
}
114105
}
115106
}
@@ -206,17 +197,17 @@ private function loadAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionPr
206197

207198
private function setAttributeContextsForGroups(Context $annotation, AttributeMetadataInterface $attributeMetadata): void
208199
{
209-
if ($annotation->getContext()) {
210-
$attributeMetadata->setNormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
211-
$attributeMetadata->setDenormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
212-
}
200+
$context = $annotation->getContext();
201+
$groups = $annotation->getGroups();
202+
$normalizationContext = $annotation->getNormalizationContext();
203+
$denormalizationContext = $annotation->getDenormalizationContext();
213204

214-
if ($annotation->getNormalizationContext()) {
215-
$attributeMetadata->setNormalizationContextForGroups($annotation->getNormalizationContext(), $annotation->getGroups());
205+
if ($normalizationContext || $context) {
206+
$attributeMetadata->setNormalizationContextForGroups($normalizationContext ?: $context, $groups);
216207
}
217208

218-
if ($annotation->getDenormalizationContext()) {
219-
$attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups());
209+
if ($denormalizationContext || $context) {
210+
$attributeMetadata->setDenormalizationContextForGroups($denormalizationContext ?: $context, $groups);
220211
}
221212
}
222213

0 commit comments

Comments
 (0)