• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

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

59.09
/src/JsonApi/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\JsonApi\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\Metadata\CollectionOperationInterface;
22
use ApiPlatform\Metadata\HttpOperation;
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 Amrouche Hamza <hamza.simperfit@gmail.com>
34
 * @author Kévin Dunglas <dunglas@gmail.com>
35
 */
36
final class EntrypointNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
37
{
38
    public const FORMAT = 'jsonapi';
39

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

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
48
    {
49
        $entrypoint = ['links' => ['self' => $this->urlGenerator->generate('api_entrypoint', [], UrlGeneratorInterface::ABS_URL)]];
4✔
50

51
        foreach ($object->getResourceNameCollection() as $resourceClass) {
4✔
52
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
4✔
53

54
            foreach ($resourceMetadata as $resource) {
4✔
55
                foreach ($resource->getOperations() as $operation) {
4✔
56
                    if (!$operation instanceof CollectionOperationInterface || ($operation instanceof HttpOperation && $operation->getUriVariables())) {
4✔
57
                        continue;
4✔
58
                    }
59

60
                    try {
61
                        $iri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_URL, $operation); // @phpstan-ignore-line phpstan issue as type is CollectionOperationInterface & Operation
4✔
62
                        $entrypoint['links'][lcfirst($resource->getShortName())] = $iri;
4✔
63
                    } catch (InvalidArgumentException) {
×
64
                        // Ignore resources without GET operations
65
                    }
66
                }
67
            }
68
        }
69

70
        return $entrypoint;
4✔
71
    }
72

73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
77
    {
78
        return self::FORMAT === $format && ($data instanceof Entrypoint || $data instanceof DocumentationEntrypoint);
4✔
79
    }
80

81
    public function getSupportedTypes($format): array
82
    {
83
        return self::FORMAT === $format ? [Entrypoint::class => true, DocumentationEntrypoint::class => true] : [];
239✔
84
    }
85

86
    public function hasCacheableSupportsMethod(): bool
87
    {
88
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
89
            trigger_deprecation(
×
90
                'api-platform/core',
×
91
                '3.1',
×
92
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
93
                __METHOD__
×
94
            );
×
95
        }
96

97
        return true;
×
98
    }
99
}
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