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

api-platform / core / 14855288206

06 May 2025 08:38AM UTC coverage: 8.457% (-0.002%) from 8.459%
14855288206

push

github

soyuka
fix: type info deprecations to baseline

bc layer covered on main

13397 of 158416 relevant lines covered (8.46%)

22.88 hits per line

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

96.0
/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\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
18
use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use ApiPlatform\Metadata\UrlGeneratorInterface;
23
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
24
use ApiPlatform\State\Pagination\PaginatorInterface;
25
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
26

27
/**
28
 * This normalizer handles collections.
29
 *
30
 * @author Kevin Dunglas <dunglas@gmail.com>
31
 * @author Samuel ROZE <samuel.roze@gmail.com>
32
 */
33
final class CollectionNormalizer extends AbstractCollectionNormalizer
34
{
35
    use HydraPrefixTrait;
36
    use JsonLdContextTrait;
37

38
    public const FORMAT = 'jsonld';
39
    public const IRI_ONLY = 'iri_only';
40
    private array $defaultContext = [
41
        self::IRI_ONLY => false,
42
    ];
43

44
    public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
45
    {
46
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
2,020✔
47

48
        if ($resourceMetadataCollectionFactory) {
2,020✔
49
            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));
×
50
        }
51

52
        parent::__construct($resourceClassResolver, '');
2,020✔
53
    }
54

55
    /**
56
     * Gets the pagination data.
57
     */
58
    protected function getPaginationData(iterable $object, array $context = []): array
59
    {
60
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
594✔
61
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
594✔
62
        // This adds "jsonld_has_context" by reference, we moved the code to this class.
63
        // To follow a note I wrote in the ItemNormalizer, we need to change the JSON-LD context generation as it is more complicated then it should.
64
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
594✔
65
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
594✔
66
        $data['@type'] = $hydraPrefix.'Collection';
594✔
67

68
        if ($object instanceof PaginatorInterface) {
594✔
69
            $data[$hydraPrefix.'totalItems'] = $object->getTotalItems();
527✔
70
        }
71

72
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
594✔
73
            $data[$hydraPrefix.'totalItems'] = \count($object);
58✔
74
        }
75

76
        return $data;
594✔
77
    }
78

79
    /**
80
     * Gets items data.
81
     */
82
    protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array
83
    {
84
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
594✔
85
        $data = [$hydraPrefix.'member' => []];
594✔
86
        $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
594✔
87

88
        foreach ($object as $obj) {
594✔
89
            if ($iriOnly) {
547✔
90
                $data[$hydraPrefix.'member'][] = $this->iriConverter->getIriFromResource($obj);
2✔
91
            } else {
92
                $data[$hydraPrefix.'member'][] = $this->normalizer->normalize($obj, $format, $context + ['jsonld_has_context' => true]);
545✔
93
            }
94
        }
95

96
        return $data;
594✔
97
    }
98

99
    protected function initContext(string $resourceClass, array $context): array
100
    {
101
        $context = parent::initContext($resourceClass, $context);
594✔
102
        $context['api_collection_sub_level'] = true;
594✔
103

104
        return $context;
594✔
105
    }
106
}
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