-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathGreetingInputDenormalizer.php
More file actions
48 lines (38 loc) · 1.48 KB
/
Copy pathGreetingInputDenormalizer.php
File metadata and controls
48 lines (38 loc) · 1.48 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
41
42
43
44
45
46
47
48
<?php declare(strict_types=1);
namespace App\Rest\Serializer;
use App\Rest\Values\Greeting;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class GreetingInputDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
if ('json' === $format) {
$data = $data[array_key_first($data)];
}
$data = array_change_key_case($data);
$salutation = $data['salutation'] ?? 'Hello';
$recipient = $data['recipient'] ?? 'World';
return new Greeting($salutation, $recipient);
}
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
if (!is_array($data)) {
return false;
}
if ('json' === $format) {
$data = $data[array_key_first($data)];
}
$data = array_change_key_case($data);
return in_array($type, $this->getSupportedTypes($format), true) &&
(array_key_exists('salutation', $data) || array_key_exists('recipient', $data));
}
public function getSupportedTypes(?string $format): array
{
return [
Greeting::class => true,
];
}
}