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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

100.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\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 HydraPrefixTrait;
35
    use JsonLdContextTrait;
36

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

43
    public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, array $defaultContext = [])
44
    {
UNCOV
45
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
950✔
46

UNCOV
47
        parent::__construct($resourceClassResolver, '');
950✔
48
    }
49

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

UNCOV
63
        if ($object instanceof PaginatorInterface) {
286✔
UNCOV
64
            $data[$hydraPrefix.'totalItems'] = $object->getTotalItems();
257✔
65
        }
66

UNCOV
67
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
286✔
UNCOV
68
            $data[$hydraPrefix.'totalItems'] = \count($object);
27✔
69
        }
70

UNCOV
71
        return $data;
286✔
72
    }
73

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

UNCOV
83
        foreach ($object as $obj) {
286✔
UNCOV
84
            if ($iriOnly) {
263✔
85
                $data[$hydraPrefix.'member'][] = $this->iriConverter->getIriFromResource($obj);
1✔
86
            } else {
UNCOV
87
                $data[$hydraPrefix.'member'][] = $this->normalizer->normalize($obj, $format, $context + ['jsonld_has_context' => true]);
262✔
88
            }
89
        }
90

UNCOV
91
        return $data;
286✔
92
    }
93

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

UNCOV
99
        return $context;
286✔
100
    }
101
}
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