-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathGreetingNormalizer.php
More file actions
40 lines (33 loc) · 1.24 KB
/
Copy pathGreetingNormalizer.php
File metadata and controls
40 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php declare(strict_types=1);
namespace App\Rest\Serializer;
use App\Rest\Values\Greeting;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class GreetingNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Greeting;
}
/** @param \App\Rest\Values\Greeting $object */
public function normalize(mixed $object, ?string $format = null, array $context = []): array|\ArrayObject|bool|float|int|null|string
{
$data = [
'Salutation' => $object->salutation,
'Recipient' => $object->recipient,
'Sentence' => "{$object->salutation} {$object->recipient}",
];
if ('json' === $format) {
$data = ['Greeting' => $data];
}
return $this->normalizer->normalize($data, $format, $context);
}
public function getSupportedTypes(?string $format): array
{
return [
Greeting::class => true,
];
}
}