• 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

54.55
/src/Hal/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\Hal\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\IriConverterInterface;
23
use ApiPlatform\Metadata\Operation;
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 = 'jsonhal';
38

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

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
47
    {
48
        $entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
4✔
49

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

53
            foreach ($resourceMetadata as $resource) {
4✔
54
                foreach ($resource->getOperations() as $operation) {
4✔
55
                    /** @var Operation $operation */
56
                    if (!$operation instanceof CollectionOperationInterface) {
4✔
57
                        continue;
×
58
                    }
59

60
                    try {
61
                        $href = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
4✔
62
                        $entrypoint['_links'][lcfirst($operation->getShortName())]['href'] = $href;
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] : [];
219✔
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