@@ -68,6 +68,107 @@ API Platform Core will automatically call the serializer with your defined forma
68
68
as `format` parameter during the deserialization process. Then it will return the result to the client with the asked MIME
69
69
type using its built-in responder.
70
70
71
+
72
+ # # Writing a Custom Normalizer
73
+
74
+ Using composition is the recommended way to implement a custom normalizer. You can use the following template to start with your
75
+ own implementation of `CustomItemNormalizer` :
76
+
77
+
78
+ ` ` ` yaml
79
+ # app/config/services.yml
80
+
81
+ services:
82
+ app.custom_item_normalizer:
83
+ public: false
84
+ class: AppBundle\S erializer\C ustomItemNormalizer
85
+ arguments: [ '@api_platform.serializer.normalizer.item' ]
86
+ tags: [ { name: serializer.normalizer } ]
87
+ ` ` `
88
+
89
+ ` ` ` php
90
+ <?php
91
+
92
+ // src/AppBundle/Serializer/CustomItemNormalizer.php
93
+
94
+ namespace AppBundle\S erializer;
95
+
96
+ use Symfony\C omponent\S erializer\N ormalizer\D enormalizerInterface;
97
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerInterface;
98
+
99
+ class CustomItemNormalizer implements NormalizerInterface, DenormalizerInterface
100
+ {
101
+ private $normalizer;
102
+
103
+ public function __construct(NormalizerInterface $normalizer)
104
+ {
105
+ if (!$normalizer instanceof DenormalizerInterface) {
106
+ throw new \I nvalidArgumentException('The normalizer must implement the DenormalizerInterface');
107
+ }
108
+
109
+ $this->normalizer = $normalizer;
110
+ }
111
+
112
+ public function denormalize($data, $class, $format = null, array $context = [])
113
+ {
114
+ return $this->normalizer->denormalize($data, $class, $format, $context);
115
+ }
116
+
117
+ public function supportsDenormalization($data, $type, $format = null)
118
+ {
119
+ return $this->normalizer->supportsDenormalization($data, $type, $format);
120
+ }
121
+
122
+ public function normalize($object, $format = null, array $context = [])
123
+ {
124
+ return $this->normalizer->normalize($object, $format, $context);
125
+ }
126
+
127
+ public function supportsNormalization($data, $format = null)
128
+ {
129
+ return $this->normalizer->supportsNormalization($data, $format);
130
+ }
131
+ }
132
+ ` ` `
133
+
134
+ For example if you want to make the `csv` format to work for even complex entities with a lot of hierarchy, you have
135
+ to flatten or remove too complex relations :
136
+
137
+ ` ` ` php
138
+ <?php
139
+
140
+ // src/AppBundle/Serializer/CustomItemNormalizer.php
141
+
142
+ namespace AppBundle\S erializer;
143
+
144
+ use Symfony\C omponent\S erializer\N ormalizer\D enormalizerInterface;
145
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerInterface;
146
+
147
+ class CustomItemNormalizer implements NormalizerInterface, DenormalizerInterface
148
+ {
149
+ // ...
150
+
151
+ public function normalize($object, $format = null, array $context = [])
152
+ {
153
+ $result = $this->normalizer->normalize($object, $format, $context);
154
+
155
+ if ('csv' !== $format || !is_array($result)) {
156
+ return $result;
157
+ }
158
+
159
+ foreach ($result as $key => $value) {
160
+ if (is_array($value) && array_keys(array_keys($value)) === array_keys($value)) {
161
+ unset($result[$key]);
162
+ }
163
+ }
164
+
165
+ return $result;
166
+ }
167
+
168
+ // ...
169
+ }
170
+ ` ` `
171
+
71
172
Previous chapter : [The Event System](events.md)
72
173
73
174
Next chapter : [Using External JSON-LD Vocabularies](external-vocabularies.md)
0 commit comments