Skip to content

Commit

Permalink
fix: Lighten Tag and NodesSources serialization with tag_documents
Browse files Browse the repository at this point in the history
…and `tag_color` serialization groups
  • Loading branch information
ambroisemaupate committed Feb 27, 2024
1 parent 9a50505 commit 6890501
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/RoadizCoreBundle/src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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';

Expand Down
11 changes: 8 additions & 3 deletions lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $serializationGroups */
$serializationGroups = isset($context['groups']) && is_array($context['groups']) ? $context['groups'] : [];

if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) {
$documentsContext = $context;
Expand All @@ -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());
}
}
}
}
Expand Down

0 comments on commit 6890501

Please sign in to comment.