Skip to content

Commit baadee0

Browse files
committed
minor #10515 [Serializer] Document Normalizers (lyrixx)
This PR was merged into the 2.8 branch. Discussion ---------- [Serializer] Document Normalizers refs #10505 Commits ------- aa4a747 [Serializer] Document Normalizers
2 parents 5ed8ec1 + aa4a747 commit baadee0

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

serializer/custom_normalizer.rst

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

serializer/normalizers.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. index::
2+
single: Serializer, Normalizers
3+
4+
Normalizers
5+
===========
6+
7+
Normalizer basically turn **objects** into **array** and vice versa.
8+
They implement
9+
:class:`Symfony\\Component\\Serializer\\Normalizers\\NormalizerInterface` for
10+
normalizing (object to array) and
11+
:class:`Symfony\\Component\\Serializer\\Normalizers\\DenormalizerInterface` for
12+
denormalizing (object to array).
13+
14+
You can add new normalizers to a Serializer instance by using its first constructor argument::
15+
16+
use Symfony\Component\Serializer\Serializer;
17+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
18+
19+
$normalizers = array(new ObjectNormalizer());
20+
$serializer = new Serializer($normalizers);
21+
22+
Built-in Normalizers
23+
--------------------
24+
25+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer` to normalizer PHP object using the PropertyAccessor component;
26+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\CustomNormalizer` to normalizer PHP object using object that implements ``:class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizableInterface``;
27+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer` to normalizer PHP object using getter and setter of the object;
28+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer` to normalizer PHP object using PHP reflection.

0 commit comments

Comments
 (0)