• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.41
/src/Hydra/Serializer/EntrypointNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Hydra\Serializer;
15

16
use ApiPlatform\Api\Entrypoint;
17
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface as LegacyUrlGeneratorInterface;
19
use ApiPlatform\Documentation\Entrypoint as DocumentationEntrypoint;
20
use ApiPlatform\Exception\InvalidArgumentException;
21
use ApiPlatform\Exception\OperationNotFoundException;
22
use ApiPlatform\Metadata\CollectionOperationInterface;
23
use ApiPlatform\Metadata\IriConverterInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\UrlGeneratorInterface;
26
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28
use Symfony\Component\Serializer\Serializer;
29

30
/**
31
 * Normalizes the API entrypoint.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
final class EntrypointNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
36
{
37
    public const FORMAT = 'jsonld';
38

39
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter, private readonly UrlGeneratorInterface|LegacyUrlGeneratorInterface $urlGenerator)
40
    {
41
    }
271✔
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
47
    {
48
        $entrypoint = [
28✔
49
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']),
28✔
50
            '@id' => $this->urlGenerator->generate('api_entrypoint'),
28✔
51
            '@type' => 'Entrypoint',
28✔
52
        ];
28✔
53

54
        foreach ($object->getResourceNameCollection() as $resourceClass) {
28✔
55
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
28✔
56

57
            foreach ($resourceMetadata as $resource) {
28✔
58
                if ($resource->getExtraProperties()['is_alternate_resource_metadata'] ?? false) {
28✔
59
                    continue;
20✔
60
                }
61

62
                foreach ($resource->getOperations() as $operation) {
28✔
63
                    $key = lcfirst($resource->getShortName());
28✔
64
                    if (!$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
28✔
65
                        continue;
20✔
66
                    }
67

68
                    try {
69
                        $entrypoint[$key] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $operation); // @phpstan-ignore-line phpstan issue as type is CollectionOperationInterface & Operation
28✔
70
                    } catch (InvalidArgumentException|OperationNotFoundException) {
20✔
71
                        // Ignore resources without GET operations
72
                    }
73
                }
74
            }
75
        }
76

77
        ksort($entrypoint);
28✔
78

79
        return $entrypoint;
28✔
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
86
    {
87
        return self::FORMAT === $format && ($data instanceof Entrypoint || $data instanceof DocumentationEntrypoint);
24✔
88
    }
89

90
    public function getSupportedTypes($format): array
91
    {
92
        return self::FORMAT === $format ? [Entrypoint::class => true, DocumentationEntrypoint::class => true] : [];
239✔
93
    }
94

95
    public function hasCacheableSupportsMethod(): bool
96
    {
97
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
98
            trigger_deprecation(
×
99
                'api-platform/core',
×
100
                '3.1',
×
101
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
102
                __METHOD__
×
103
            );
×
104
        }
105

106
        return true;
×
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc