Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5c4593

Browse files
committed
minor #18603 [Serializer] Custom normalizer update (alexandre-daubois)
This PR was merged into the 7.0 branch. Discussion ---------- [Serializer] Custom normalizer update Last PR to resolve #18507 Commits ------- 0fd8f8f [Serializer] Custom normalizer update
2 parents f7fd0e7 + 0fd8f8f commit a5c4593

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

serializer/custom_normalizer.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ Creating a New Normalizer
1212
Imagine you want add, modify, or remove some properties during the serialization
1313
process. For that you'll have to create your own normalizer. But it's usually
1414
preferable to let Symfony normalize the object, then hook into the normalization
15-
to customize the normalized data. To do that, leverage the ``ObjectNormalizer``::
15+
to customize the normalized data. To do that, leverage the
16+
``NormalizerAwareInterface`` and the ``NormalizerAwareTrait``. This will give
17+
you access to a ``$normalizer`` property which takes care of most of the
18+
normalization process::
1619

1720
// src/Serializer/TopicNormalizer.php
1821
namespace App\Serializer;
1922

2023
use App\Entity\Topic;
2124
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2225
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23-
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2426

25-
class TopicNormalizer implements NormalizerInterface
27+
class TopicNormalizer implements NormalizerInterface, NormalizerAwareInterface
2628
{
29+
use NormalizerAwareTrait;
30+
2731
public function __construct(
2832
private UrlGeneratorInterface $router,
29-
private ObjectNormalizer $normalizer,
3033
) {
3134
}
3235

0 commit comments

Comments
 (0)