Skip to content

[Serializer] Document how to create a normalizer #10505

Closed
@lyrixx

Description

@lyrixx

ATM, the documentation is a bit old and uncomplete. For exemple the doc tells to create the definition and then to tag it. So OLD 😂

For exemple, we could exemple how to enhance normalized data:

class TopicNormalizer implements NormalizerInterface
{
    private $router;
    private $normaliser;

    public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normaliser)
    {
        $this->router = $router;
        $this->normaliser = $normaliser;
    }

    public function normalize($topic, $format = null, array $context = array())
    {
        $data = $this->normaliser->normalize($topic, 'json', $context);
        $data['href']['self'] = $this
            ->router
            ->generate(
                'topic_show',
                ['id' => $topic->getId()],
                UrlGeneratorInterface::ABSOLUTE_URL
            )
        ;

        return $data;
    }

    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof Topic;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions