From 68905012e4200983a2a054d5ac71f4e0f05949cf Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 27 Feb 2024 12:14:57 +0100 Subject: [PATCH] fix: Lighten Tag and NodesSources serialization with `tag_documents` and `tag_color` serialization groups --- lib/RoadizCoreBundle/src/Entity/Tag.php | 8 ++++---- .../src/Serializer/Normalizer/TagNormalizer.php | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/RoadizCoreBundle/src/Entity/Tag.php b/lib/RoadizCoreBundle/src/Entity/Tag.php index 3c357a1b..ebbf8da5 100644 --- a/lib/RoadizCoreBundle/src/Entity/Tag.php +++ b/lib/RoadizCoreBundle/src/Entity/Tag.php @@ -58,8 +58,8 @@ class Tag extends AbstractDateTimedPositioned implements LeafInterface nullable: false, options: ['default' => '#000000'] )] - #[SymfonySerializer\Groups(['tag', 'tag_base', 'color'])] - #[Serializer\Groups(['tag', 'tag_base', 'color'])] + #[SymfonySerializer\Groups(['tag', 'tag_color', 'color'])] + #[Serializer\Groups(['tag', 'tag_color', 'color'])] #[Assert\Length(max: 7)] protected string $color = '#000000'; @@ -131,13 +131,13 @@ class Tag extends AbstractDateTimedPositioned implements LeafInterface #[ORM\Column(name: 'children_order', type: 'string', length: 60, options: ['default' => 'position'])] #[SymfonySerializer\Ignore] - #[Serializer\Groups(["tag"])] + #[Serializer\Groups(["tag", "tag_children_order"])] #[Assert\Length(max: 60)] private string $childrenOrder = 'position'; #[ORM\Column(name: 'children_order_direction', type: 'string', length: 4, options: ['default' => 'ASC'])] #[SymfonySerializer\Ignore] - #[Serializer\Groups(["tag"])] + #[Serializer\Groups(["tag", "tag_children_order"])] #[Assert\Length(max: 4)] private string $childrenOrderDirection = 'ASC'; diff --git a/lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php b/lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php index dfb2af53..aa6aa71a 100644 --- a/lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php +++ b/lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php @@ -26,6 +26,8 @@ public function normalize($object, $format = null, array $context = []) $data = $this->decorated->normalize($object, $format, $context); if ($object instanceof Tag && is_array($data)) { $data['slug'] = $object->getTagName(); + /** @var array $serializationGroups */ + $serializationGroups = isset($context['groups']) && is_array($context['groups']) ? $context['groups'] : []; if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) { $documentsContext = $context; @@ -34,9 +36,12 @@ public function normalize($object, $format = null, array $context = []) if ($translatedData instanceof TagTranslation) { $data['name'] = $translatedData->getName(); $data['description'] = $translatedData->getDescription(); - $data['documents'] = array_map(function (DocumentInterface $document) use ($format, $documentsContext) { - return $this->decorated->normalize($document, $format, $documentsContext); - }, $translatedData->getDocuments()); + + if (\in_array('tag_documents', $serializationGroups, true)) { + $data['documents'] = array_map(function (DocumentInterface $document) use ($format, $documentsContext) { + return $this->decorated->normalize($document, $format, $documentsContext); + }, $translatedData->getDocuments()); + } } } }