/
IriTransformerNormalizer.php
73 lines (59 loc) · 2.3 KB
/
IriTransformerNormalizer.php
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
<?php
declare(strict_types=1);
namespace App\Serializer;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\JsonLd\Serializer\ItemNormalizer;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class IriTransformerNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public const CONTEXT_KEY = 'iris_transform';
public function __construct(
private readonly IriConverterInterface $iriConverter,
private readonly OperationMetadataFactoryInterface $operationMetadataFactory
) {
}
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
/** @var array $data */
$data = $this->normalizer->normalize($object, $format, $context + [self::class => true]);
$value = $context[self::CONTEXT_KEY];
if (!\is_array($value)) {
$value = [$value];
}
foreach ($value as $property => $uriTemplate) {
if (!isset($data[$property]) || !(\is_string($data[$property]) || isset($data[$property]['@id']))) {
continue;
}
$iri = $this->iriConverter->getIriFromResource(
$object->{$property},
UrlGeneratorInterface::ABS_PATH,
$this->operationMetadataFactory->create($uriTemplate)
);
if (\is_string($data[$property])) {
$data[$property] = $iri;
} elseif (isset($data[$property]['@id'])) {
$data[$property]['@id'] = $iri;
}
}
return $data;
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return \is_object($data)
&& !is_iterable($data)
&& isset($context[self::CONTEXT_KEY])
&& ItemNormalizer::FORMAT === $format
&& !isset($context[self::class]);
}
public function getSupportedTypes(?string $format): array
{
return [
'*' => false,
];
}
}