Skip to content

Commit 3c2b8c6

Browse files
committed
Merge branch '2.8' into 3.4
* 2.8: [Serializer] Document Normalizers
2 parents 10671c8 + 9cd5b2b commit 3c2b8c6

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

serializer/custom_normalizer.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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
8+
normalizers to transform any data into an array. The component provides several
9+
:doc:`built-in normalizers </serializer/normalizers>` but you may need to create
10+
your own normalizer to transform an unsupported data structure.
11+
12+
Creating a New Normalizer
13+
-------------------------
14+
15+
Imagine you want add, modify, or remove some properties during the serialization
16+
process. For that you'll have to create your own normalizer. But it's usually
17+
preferable to let Symfony normalize the object, then hook into the normalization
18+
to customize the normalized data. To do that, leverage the ``ObjectNormalizer``::
19+
20+
namespace AppBundle\Serializer;
21+
22+
use AppBundle\Entity\Topic;
23+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
26+
27+
class TopicNormalizer implements NormalizerInterface
28+
{
29+
private $router;
30+
private $normalizer;
31+
32+
public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer)
33+
{
34+
$this->router = $router;
35+
$this->normalizer = $normalizer;
36+
}
37+
38+
public function normalize($topic, $format = null, array $context = array())
39+
{
40+
$data = $this->normalizer->normalize($topic, $format, $context);
41+
42+
// Here, add, edit, or delete some data:
43+
$data['href']['self'] = $this->router->generate('topic_show', array(
44+
'id' => $topic->getId(),
45+
), UrlGeneratorInterface::ABSOLUTE_URL);
46+
47+
return $data;
48+
}
49+
50+
public function supportsNormalization($data, $format = null)
51+
{
52+
return $data instanceof Topic;
53+
}
54+
}
55+
56+
Registering it in Your Application
57+
----------------------------------
58+
59+
In order to enable the normalizer in an application based on the entire Symfony
60+
framework, you must register it as a service and :doc:`tag it </service_container/tags>`
61+
with ``serializer.normalizer``.
62+
63+
.. configuration-block::
64+
65+
.. code-block:: yaml
66+
67+
# app/config/services.yml
68+
services:
69+
app.yaml_encoder:
70+
class: AppBundle\Serializer\TopicNormalizer
71+
tags:
72+
- { name: serializer.normalizer }
73+
74+
.. code-block:: xml
75+
76+
<!-- app/config/services.xml -->
77+
<?xml version="1.0" encoding="UTF-8" ?>
78+
<container xmlns="http://symfony.com/schema/dic/services"
79+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
80+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
81+
82+
<services>
83+
<service id="app.yaml_encoder" class="AppBundle\Serializer\TopicNormalizer">
84+
<tag name="serializer.normalizer" />
85+
</service>
86+
</services>
87+
</container>
88+
89+
.. code-block:: php
90+
91+
// app/config/services.php
92+
use AppBundle\Serializer\TopicNormalizer;
93+
94+
$container
95+
->register('app.yaml_encoder', TopicNormalizer::class)
96+
->addTag('serializer.normalizer')
97+
;

serializer/normalizers.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.. index::
2+
single: Serializer, Normalizers
3+
4+
Normalizers
5+
===========
6+
7+
Normalizers turn **objects** into **arrays** and vice versa. They implement
8+
:class:`Symfony\\Component\\Serializer\\Normalizers\\NormalizerInterface` for
9+
normalizing (object to array) and
10+
:class:`Symfony\\Component\\Serializer\\Normalizers\\DenormalizerInterface` for
11+
denormalizing (array to object).
12+
13+
Normalizers are enabled in the serializer passing them as its first argument::
14+
15+
use Symfony\Component\Serializer\Serializer;
16+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17+
18+
$normalizers = array(new ObjectNormalizer());
19+
$serializer = new Serializer($normalizers);
20+
21+
Built-in Normalizers
22+
--------------------
23+
24+
Symfony includes the following normalizers but you can also
25+
:doc:`create your own normalizer </serializer/custom_normalizer>`:
26+
27+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer` to
28+
normalize PHP object using the :doc:`PropertyAccessor component </components/property_access>`;
29+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\CustomNormalizer` to
30+
normalize PHP object using an object that implements
31+
``:class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizableInterface``;
32+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer` to
33+
normalize PHP object using the getter and setter methods of the object;
34+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer` to
35+
normalize PHP object using `PHP reflection`_.
36+
37+
.. _`PHP reflection`: https://php.net/manual/en/book.reflection.php

0 commit comments

Comments
 (0)