|
| 1 | +.. index:: |
| 2 | + single: Serializer; Custom normalizers |
| 3 | + |
| 4 | +How to Create your Custom Normalizer |
| 5 | +==================================== |
| 6 | + |
| 7 | +The :doc:`Serializer Component </components/serializer>` uses Normalizers |
| 8 | +to transform any data to an array. |
| 9 | + |
| 10 | +The Component provides several built-in normalizer that are described |
| 11 | +:doc:`in their own section </serializer/normalizers>` but you may want |
| 12 | +to use another structure that's not supported. |
| 13 | + |
| 14 | +Creating a new normalizer |
| 15 | +------------------------- |
| 16 | + |
| 17 | +Imagine you want add, modify, or remove some properties during the serialization |
| 18 | +process. For that you'll have to create your own normalizer. But it's usually |
| 19 | +preferable to let Symfony normalize the object, then hook into the normalization |
| 20 | +to customize the normalized data. To do that, we leverage the ObjectNormalizer:: |
| 21 | + |
| 22 | + namespace AppBundle\Serializer; |
| 23 | + |
| 24 | + use AppBundle\Entity\Topic; |
| 25 | + use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 26 | + use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 27 | + use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 28 | + |
| 29 | + class TopicNormalizer implements NormalizerInterface |
| 30 | + { |
| 31 | + private $router; |
| 32 | + private $normalizer; |
| 33 | + |
| 34 | + public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer) |
| 35 | + { |
| 36 | + $this->router = $router; |
| 37 | + $this->normalizer = $normalizer; |
| 38 | + } |
| 39 | + |
| 40 | + public function normalize($topic, $format = null, array $context = array()) |
| 41 | + { |
| 42 | + $data = $this->normalizer->normalize($topic, $format, $context); |
| 43 | + |
| 44 | + // Here, add, edit, or delete some data: |
| 45 | + $data['href']['self'] = $this |
| 46 | + ->router |
| 47 | + ->generate( |
| 48 | + 'topic_show', |
| 49 | + ['id' => $topic->getId()], |
| 50 | + UrlGeneratorInterface::ABSOLUTE_URL |
| 51 | + ) |
| 52 | + ; |
| 53 | + |
| 54 | + return $data; |
| 55 | + } |
| 56 | + |
| 57 | + public function supportsNormalization($data, $format = null) |
| 58 | + { |
| 59 | + return $data instanceof Topic; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +Registering it in your app |
| 64 | +-------------------------- |
| 65 | + |
| 66 | +If you use the Symfony Framework. then you probably want to register this |
| 67 | +normalizer as a service in your app. Then, you only need to tag it with |
| 68 | +``serializer.normalizer`` to inject your custom normalizer into the Serializer. |
| 69 | + |
| 70 | +.. configuration-block:: |
| 71 | + |
| 72 | + .. code-block:: yaml |
| 73 | +
|
| 74 | + # app/config/services.yml |
| 75 | + services: |
| 76 | + app.yaml_encoder: |
| 77 | + class: AppBundle\Serializer\TopicNormalizer |
| 78 | + tags: |
| 79 | + - { name: serializer.normalizer } |
| 80 | +
|
| 81 | + .. code-block:: xml |
| 82 | +
|
| 83 | + <!-- app/config/services.xml --> |
| 84 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 85 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 86 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 87 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 88 | +
|
| 89 | + <services> |
| 90 | + <service id="app.yaml_encoder" class="AppBundle\Serializer\TopicNormalizer"> |
| 91 | + <tag name="serializer.normalizer" /> |
| 92 | + </service> |
| 93 | + </services> |
| 94 | + </container> |
| 95 | +
|
| 96 | + .. code-block:: php |
| 97 | +
|
| 98 | + // app/config/services.php |
| 99 | + use AppBundle\Serializer\TopicNormalizer; |
| 100 | +
|
| 101 | + $container |
| 102 | + ->register('app.yaml_encoder', TopicNormalizer::class) |
| 103 | + ->addTag('serializer.normalizer') |
| 104 | + ; |
| 105 | +
|
| 106 | +.. _tracker: https://github.com/symfony/symfony/issues |
0 commit comments