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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

94.74
/src/Hydra/Serializer/CollectionNormalizer.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\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\JsonLd\ContextBuilderInterface;
20
use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Serializer\ContextTrait;
23
use ApiPlatform\State\Pagination\PaginatorInterface;
24
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
25
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
27
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
28
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
29
use Symfony\Component\Serializer\Serializer;
30

31
/**
32
 * This normalizer handles collections.
33
 *
34
 * @author Kevin Dunglas <dunglas@gmail.com>
35
 * @author Samuel ROZE <samuel.roze@gmail.com>
36
 */
37
final class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
38
{
39
    use ContextTrait;
40
    use JsonLdContextTrait;
41
    use NormalizerAwareTrait;
42

43
    public const FORMAT = 'jsonld';
44
    public const IRI_ONLY = 'iri_only';
45
    private array $defaultContext = [
46
        self::IRI_ONLY => false,
47
    ];
48

49
    public function __construct(private readonly ContextBuilderInterface $contextBuilder, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
50
    {
51
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
32✔
52

53
        if ($this->resourceMetadataCollectionFactory) {
32✔
54
            trigger_deprecation('api-platform/core', '3.0', sprintf('Injecting "%s" within "%s" is not needed anymore and this dependency will be removed in 4.0.', ResourceMetadataCollectionFactoryInterface::class, self::class));
×
55
        }
56
    }
57

58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
62
    {
63
        return self::FORMAT === $format && is_iterable($data);
14✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @param iterable $object
70
     */
71
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
72
    {
73
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
14✔
74
            return $this->normalizeRawCollection($object, $format, $context);
2✔
75
        }
76

77
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
12✔
78
        $context = $this->initContext($resourceClass, $context);
12✔
79
        $context['api_collection_sub_level'] = true;
12✔
80
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
12✔
81
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
12✔
82
        $data['@type'] = 'hydra:Collection';
12✔
83
        $data['hydra:member'] = [];
12✔
84
        $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
12✔
85

86
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
12✔
87
            $context['item_uri_template'] = $operation->getItemUriTemplate();
7✔
88
        }
89

90
        // We need to keep this operation for serialization groups for later
91
        if (isset($context['operation'])) {
12✔
92
            $context['root_operation'] = $context['operation'];
7✔
93
        }
94

95
        if (isset($context['operation_name'])) {
12✔
96
            $context['root_operation_name'] = $context['operation_name'];
10✔
97
        }
98

99
        // We need to unset the operation to ensure a proper IRI generation inside items
100
        unset($context['operation']);
12✔
101
        unset($context['operation_name'], $context['uri_variables']);
12✔
102

103
        foreach ($object as $obj) {
12✔
104
            if ($iriOnly) {
6✔
105
                $data['hydra:member'][] = $this->iriConverter->getIriFromResource($obj);
2✔
106
            } else {
107
                $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context);
4✔
108
            }
109
        }
110

111
        if ($object instanceof PaginatorInterface) {
12✔
112
            $data['hydra:totalItems'] = $object->getTotalItems();
7✔
113
        }
114

115
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
12✔
116
            $data['hydra:totalItems'] = \count($object);
3✔
117
        }
118

119
        return $data;
12✔
120
    }
121

122
    public function hasCacheableSupportsMethod(): bool
123
    {
124
        return true;
14✔
125
    }
126

127
    /**
128
     * Normalizes a raw collection (not API resources).
129
     */
130
    protected function normalizeRawCollection(iterable $object, string $format = null, array $context = []): array|\ArrayObject
131
    {
132
        if (\is_array($object) && !$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
2✔
133
            return new \ArrayObject();
×
134
        }
135

136
        $data = [];
2✔
137
        foreach ($object as $index => $obj) {
2✔
138
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
1✔
139
        }
140

141
        return $data;
2✔
142
    }
143
}
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