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

api-platform / core / 10508505187

22 Aug 2024 12:55PM UTC coverage: 7.704%. Remained the same
10508505187

push

github

soyuka
Merge 3.4

0 of 4 new or added lines in 4 files covered. (0.0%)

8840 existing lines in 284 files now uncovered.

12477 of 161949 relevant lines covered (7.7%)

22.99 hits per line

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

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

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

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

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

UNCOV
46
        if ($resourceMetadataCollectionFactory) {
2,255✔
47
            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));
×
48
        }
49

UNCOV
50
        parent::__construct($resourceClassResolver, '');
2,255✔
51
    }
52

53
    /**
54
     * Gets the pagination data.
55
     */
56
    protected function getPaginationData(iterable $object, array $context = []): array
57
    {
UNCOV
58
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
548✔
59
        // This adds "jsonld_has_context" by reference, we moved the code to this class.
60
        // 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.
UNCOV
61
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
548✔
UNCOV
62
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
548✔
UNCOV
63
        $data['@type'] = 'hydra:Collection';
548✔
64

UNCOV
65
        if ($object instanceof PaginatorInterface) {
548✔
UNCOV
66
            $data['hydra:totalItems'] = $object->getTotalItems();
494✔
67
        }
68

UNCOV
69
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
548✔
UNCOV
70
            $data['hydra:totalItems'] = \count($object);
46✔
71
        }
72

UNCOV
73
        return $data;
548✔
74
    }
75

76
    /**
77
     * Gets items data.
78
     */
79
    protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array
80
    {
UNCOV
81
        $data = [];
548✔
UNCOV
82
        $data['hydra:member'] = [];
548✔
UNCOV
83
        $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
548✔
84

UNCOV
85
        foreach ($object as $obj) {
548✔
UNCOV
86
            if ($iriOnly) {
496✔
UNCOV
87
                $data['hydra:member'][] = $this->iriConverter->getIriFromResource($obj);
3✔
88
            } else {
UNCOV
89
                $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context + ['jsonld_has_context' => true]);
493✔
90
            }
91
        }
92

UNCOV
93
        return $data;
548✔
94
    }
95

96
    protected function initContext(string $resourceClass, array $context): array
97
    {
UNCOV
98
        $context = parent::initContext($resourceClass, $context);
548✔
UNCOV
99
        $context['api_collection_sub_level'] = true;
548✔
100

UNCOV
101
        return $context;
548✔
102
    }
103
}
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