From 0fd8f8fa532a3bafed52504670f4ad339b34a712 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 24 Jul 2023 14:21:18 +0200 Subject: [PATCH] [Serializer] Custom normalizer update --- serializer/custom_normalizer.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/serializer/custom_normalizer.rst b/serializer/custom_normalizer.rst index 25528fff54e..58e6ef2d075 100644 --- a/serializer/custom_normalizer.rst +++ b/serializer/custom_normalizer.rst @@ -12,7 +12,10 @@ Creating a New Normalizer Imagine you want add, modify, or remove some properties during the serialization process. For that you'll have to create your own normalizer. But it's usually preferable to let Symfony normalize the object, then hook into the normalization -to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:: +to customize the normalized data. To do that, leverage the +``NormalizerAwareInterface`` and the ``NormalizerAwareTrait``. This will give +you access to a ``$normalizer`` property which takes care of most of the +normalization process:: // src/Serializer/TopicNormalizer.php namespace App\Serializer; @@ -20,13 +23,13 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``: use App\Entity\Topic; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; - use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; - class TopicNormalizer implements NormalizerInterface + class TopicNormalizer implements NormalizerInterface, NormalizerAwareInterface { + use NormalizerAwareTrait; + public function __construct( private UrlGeneratorInterface $router, - private ObjectNormalizer $normalizer, ) { }