• 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

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

28
/**
29
 * This normalizer handles collections.
30
 *
31
 * @author Kevin Dunglas <dunglas@gmail.com>
32
 * @author Samuel ROZE <samuel.roze@gmail.com>
33
 */
34
final class CollectionNormalizer extends AbstractCollectionNormalizer
35
{
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, LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver, private readonly LegacyIriConverterInterface|IriConverterInterface $iriConverter, readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
45
    {
46
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
283✔
47

48
        if ($resourceMetadataCollectionFactory) {
283✔
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, '');
283✔
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']);
76✔
61
        // This adds "jsonld_has_context" by reference, we moved the code to this class.
62
        // 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.
63
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
76✔
64
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
76✔
65
        $data['@type'] = 'hydra:Collection';
76✔
66

67
        if ($object instanceof PaginatorInterface) {
76✔
68
            $data['hydra:totalItems'] = $object->getTotalItems();
44✔
69
        }
70

71
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
76✔
72
            $data['hydra:totalItems'] = \count($object);
24✔
73
        }
74

75
        return $data;
76✔
76
    }
77

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

87
        foreach ($object as $obj) {
76✔
88
            if ($iriOnly) {
52✔
89
                $data['hydra:member'][] = $this->iriConverter->getIriFromResource($obj);
8✔
90
            } else {
91
                $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context + ['jsonld_has_context' => true]);
44✔
92
            }
93
        }
94

95
        return $data;
76✔
96
    }
97

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

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