forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectNormalizer.php
More file actions
98 lines (83 loc) · 2.91 KB
/
ObjectNormalizer.php
File metadata and controls
98 lines (83 loc) · 2.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Hal\Serializer;
use ApiPlatform\Api\IriConverterInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Decorates the output with JSON HAL metadata when appropriate, but otherwise
* just passes through to the decorated normalizer.
*/
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
public const FORMAT = 'jsonhal';
public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter)
{
}
/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}
/**
* {@inheritdoc}
*/
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (isset($context['api_resource'])) {
$originalResource = $context['api_resource'];
unset($context['api_resource']);
}
$data = $this->decorated->normalize($object, $format, $context);
if (!\is_array($data)) {
return $data;
}
if (!isset($originalResource)) {
return $data;
}
$metadata = [
'_links' => [
'self' => [
'href' => $this->iriConverter->getIriFromResource($originalResource),
],
],
];
return $metadata + $data;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
// prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
return self::FORMAT === $format;
}
/**
* {@inheritdoc}
*
* @throws LogicException
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
}
}