Skip to content

Commit

Permalink
feat: Do not serialize tag slug manually
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jan 16, 2024
1 parent c11d9aa commit 42747f1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
47 changes: 47 additions & 0 deletions lib/RoadizCoreBundle/src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Entity;

use ApiPlatform\Doctrine\Orm\Filter as BaseFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use ApiPlatform\Metadata\ApiFilter;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -61,6 +62,10 @@ class Tag extends AbstractDateTimedPositioned implements LeafInterface
#[SymfonySerializer\Groups(['tag', 'tag_base', 'color'])]
#[Serializer\Groups(['tag', 'tag_base', 'color'])]
#[Assert\Length(max: 7)]
#[ApiProperty(
description: 'Tag color in hexadecimal format.',
example: '#ff0000',
)]
protected string $color = '#000000';

/**
Expand Down Expand Up @@ -117,6 +122,10 @@ class Tag extends AbstractDateTimedPositioned implements LeafInterface
#[Assert\NotNull]
#[Assert\NotBlank]
#[Assert\Length(max: 250)]
#[ApiProperty(
description: 'Unique tag name (slug) used to build content URL or filter queries.',
example: 'this-is-a-tag-name',
)]
private string $tagName = '';

#[SymfonySerializer\Ignore]
Expand All @@ -127,24 +136,50 @@ class Tag extends AbstractDateTimedPositioned implements LeafInterface
#[ORM\Column(type: 'boolean', nullable: false, options: ['default' => true])]
#[SymfonySerializer\Groups(['tag', 'tag_base', 'node', 'nodes_sources'])]
#[Serializer\Groups(['tag', 'tag_base', 'node', 'nodes_sources'])]
#[ApiProperty(
description: 'Is this tag visible in website?',
example: 'true',
)]
private bool $visible = true;

#[ORM\Column(name: 'children_order', type: 'string', length: 60, options: ['default' => 'position'])]
#[SymfonySerializer\Ignore]
#[Serializer\Groups(["tag"])]
#[Assert\Length(max: 60)]
#[ApiProperty(
description: 'This tag children will be sorted by a given field',
example: 'position',
schema: [
'type' => 'string',
'enum' => ['position', 'tagName', 'createdAt', 'updatedAt', 'publishedAt'],
'example' => 'position'
],
)]
private string $childrenOrder = 'position';

#[ORM\Column(name: 'children_order_direction', type: 'string', length: 4, options: ['default' => 'ASC'])]
#[SymfonySerializer\Ignore]
#[Serializer\Groups(["tag"])]
#[Assert\Length(max: 4)]
#[ApiProperty(
description: 'This tag children will be sorted ascendant or descendant',
example: 'ASC',
schema: [
'type' => 'string',
'enum' => ['ASC', 'DESC'],
'example' => 'ASC'
],
)]
private string $childrenOrderDirection = 'ASC';

#[ApiFilter(BaseFilter\BooleanFilter::class)]
#[ORM\Column(type: 'boolean', nullable: false, options: ['default' => false])]
#[SymfonySerializer\Ignore]
#[Serializer\Groups(["tag"])]
#[ApiProperty(
description: 'Is this tag locked to prevent deletion and renaming?',
example: 'false',
)]
private bool $locked = false;

/**
Expand Down Expand Up @@ -468,4 +503,16 @@ public function setParent(?LeafInterface $parent = null): static

return $this;
}


#[ApiProperty(
description: 'Unique tag name (slug) used to build content URL or filter queries.',
example: 'this-is-a-tag-name',
)]
#[SymfonySerializer\SerializedName('slug')]
#[SymfonySerializer\Groups(['tag', 'tag_base'])]
public function getSlug(): string
{
return $this->getTagName();
}
}
29 changes: 15 additions & 14 deletions lib/RoadizCoreBundle/src/Serializer/Normalizer/TagNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ final class TagNormalizer extends AbstractPathNormalizer
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();

if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) {
$documentsContext = $context;
$documentsContext['groups'] = ['document_display'];
$translatedData = $object->getTranslatedTagsByTranslation($context['translation'])->first() ?: null;
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 (
$object instanceof Tag &&
is_array($data) &&
isset($context['translation']) &&
$context['translation'] instanceof TranslationInterface
) {
$documentsContext = $context;
$documentsContext['groups'] = ['document_display'];
$translatedData = $object->getTranslatedTagsByTranslation($context['translation'])->first() ?: null;
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());
}
}
return $data;
Expand Down

0 comments on commit 42747f1

Please sign in to comment.