Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ec283d

Browse files
committed
[Serializer] Document Normalizers
1 parent3b241b1 commit0ec283d

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

‎serializer/custom_normalizer.rst‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
..index::
2+
single: Serializer; Custom encoders
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/normalizer>` 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 App\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 $normaliser;
33+
34+
public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normaliser)
35+
{
36+
$this->router = $router;
37+
$this->normaliser = $normaliser;
38+
}
39+
40+
public function normalize($topic, $format = null, array $context = array())
41+
{
42+
$data = $this->normaliser->normalize($topic, 'json', $context);
43+
$data['href']['self'] = $this
44+
->router
45+
->generate(
46+
'topic_show',
47+
['id' => $topic->getId()],
48+
UrlGeneratorInterface::ABSOLUTE_URL
49+
)
50+
;
51+
52+
return $data;
53+
}
54+
55+
public function supportsNormalization($data, $format = null)
56+
{
57+
return $data instanceof Topic;
58+
}
59+
}
60+
61+
Registering it in your app
62+
--------------------------
63+
64+
If you use the Symfony Framework. then you probably want to register this
65+
normalizer as a service in your app. Then, you only need to tag it with
66+
``serializer.normalizer`` to inject your custom normalizer into the Serializer.
67+
68+
..configuration-block::
69+
70+
..code-block::yaml
71+
72+
# app/config/services.yml
73+
services:
74+
app.yaml_encoder:
75+
class:AppBundle\Serializer\TopicNormalizer
76+
tags:
77+
-{ name: serializer.normalizer }
78+
79+
..code-block::xml
80+
81+
<!-- app/config/services.xml-->
82+
<?xml version="1.0" encoding="UTF-8" ?>
83+
<containerxmlns="http://symfony.com/schema/dic/services"
84+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
85+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
86+
87+
<services>
88+
<serviceid="app.yaml_encoder"class="AppBundle\Serializer\TopicNormalizer">
89+
<tagname="serializer.normalizer" />
90+
</service>
91+
</services>
92+
</container>
93+
94+
..code-block::php
95+
96+
// app/config/services.php
97+
use AppBundle\Serializer\TopicNormalizer;
98+
99+
$container
100+
->register('app.yaml_encoder', TopicNormalizer::class)
101+
->addTag('serializer.normalizer')
102+
;
103+
104+
.. _tracker:https://github.com/symfony/symfony/issues

‎serializer/encoders.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Encoders
55
========
66

7+
FIXME
8+
79
Encoders basically turn **arrays** into **formats** and vice versa.
810
They implement
911
:class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for

‎serializer/normalizers.rst‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.
29+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp