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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
    {
UNCOV
46
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
×
47

UNCOV
48
        if ($resourceMetadataCollectionFactory) {
×
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

UNCOV
52
        parent::__construct($resourceClassResolver, '');
×
53
    }
54

55
    /**
56
     * Gets the pagination data.
57
     */
58
    protected function getPaginationData(iterable $object, array $context = []): array
59
    {
UNCOV
60
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
×
UNCOV
61
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
×
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.
UNCOV
64
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
×
UNCOV
65
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
×
UNCOV
66
        $data['@type'] = $hydraPrefix.'Collection';
×
67

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

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

UNCOV
76
        return $data;
×
77
    }
78

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

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

UNCOV
96
        return $data;
×
97
    }
98

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

UNCOV
104
        return $context;
×
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