Skip to content

Commit e55c72b

Browse files
committed
Merge branch 'blaues0cke-custom-normalizer' into 2.0
2 parents 14dec92 + 74f6817 commit e55c72b

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

core/content-negotiation.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,107 @@ API Platform Core will automatically call the serializer with your defined forma
6868
as `format` parameter during the deserialization process. Then it will return the result to the client with the asked MIME
6969
type using its built-in responder.
7070

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\Serializer\CustomItemNormalizer
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\Serializer;
95+
96+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
97+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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 \InvalidArgumentException('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\Serializer;
143+
144+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
145+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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+
71172
Previous chapter: [The Event System](events.md)
72173

73174
Next chapter: [Using External JSON-LD Vocabularies](external-vocabularies.md)

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
1. [Enabling Several Formats](core/content-negotiation.md#enabling-several-formats)
5353
2. [Registering a Custom Serializer](core/content-negotiation.md#registering-a-custom-serializer)
5454
3. [Creating a Responder](core/content-negotiation.md#creating-a-responder)
55+
4. [Writing a Custom Normalizer](core/content-negotiation.md#writing-a-custom-normalizer)
5556
11. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
5657
12. [Extending JSON-LD context](core/extending-jsonld-context.md)
5758
13. [Data Providers](core/data-providers.md)

0 commit comments

Comments
 (0)