-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntrypointNormalizer.php
More file actions
83 lines (70 loc) · 2.83 KB
/
Copy pathEntrypointNormalizer.php
File metadata and controls
83 lines (70 loc) · 2.83 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
<?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\JsonApi\Serializer;
use ApiPlatform\Documentation\Entrypoint;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Normalizes the API entrypoint.
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class EntrypointNormalizer implements NormalizerInterface
{
public const FORMAT = 'jsonapi';
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly IriConverterInterface $iriConverter, private readonly UrlGeneratorInterface $urlGenerator)
{
}
/**
* {@inheritdoc}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
$entrypoint = ['links' => ['self' => $this->urlGenerator->generate('api_entrypoint', [], UrlGeneratorInterface::ABS_URL)]];
foreach ($data->getResourceNameCollection() as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
foreach ($resourceMetadata as $resource) {
foreach ($resource->getOperations() as $operation) {
if (!$operation instanceof CollectionOperationInterface || ($operation instanceof HttpOperation && $operation->getUriVariables())) {
continue;
}
try {
$iri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_URL, $operation);
$entrypoint['links'][lcfirst($resource->getShortName())] = $iri;
} catch (InvalidArgumentException) {
// Ignore resources without GET operations
}
}
}
}
return $entrypoint;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return self::FORMAT === $format && $data instanceof Entrypoint;
}
/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array
{
return self::FORMAT === $format ? [Entrypoint::class => true] : [];
}
}