• 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

94.59
/src/JsonApi/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\JsonApi\Serializer;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\Metadata\ResourceClassResolverInterface;
18
use ApiPlatform\Metadata\Util\IriHelper;
19
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
20
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
21

22
/**
23
 * Normalizes collections in the JSON API format.
24
 *
25
 * @author Kevin Dunglas <dunglas@gmail.com>
26
 * @author Hamza Amrouche <hamza@les-tilleuls.coop>
27
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
28
 */
29
final class CollectionNormalizer extends AbstractCollectionNormalizer
30
{
31
    public const FORMAT = 'jsonapi';
32

33
    public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
34
    {
UNCOV
35
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
950✔
36
    }
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function getPaginationData($object, array $context = []): array
42
    {
UNCOV
43
        [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context);
30✔
UNCOV
44
        $parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);
30✔
45

UNCOV
46
        $operation = $context['operation'] ?? $this->getOperation($context);
30✔
UNCOV
47
        $urlGenerationStrategy = $operation->getUrlGenerationStrategy();
30✔
48

UNCOV
49
        $data = [
30✔
UNCOV
50
            'links' => [
30✔
UNCOV
51
                'self' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $urlGenerationStrategy),
30✔
UNCOV
52
            ],
30✔
UNCOV
53
        ];
30✔
54

UNCOV
55
        if ($paginated) {
30✔
56
            if (null !== $lastPage) {
6✔
57
                $data['links']['first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1., $urlGenerationStrategy);
6✔
58
                $data['links']['last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage, $urlGenerationStrategy);
6✔
59
            }
60

61
            if (1. !== $currentPage) {
6✔
62
                $data['links']['prev'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1., $urlGenerationStrategy);
1✔
63
            }
64

65
            if (null !== $lastPage && $currentPage !== $lastPage || null === $lastPage && $pageTotalItems >= $itemsPerPage) {
6✔
66
                $data['links']['next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1., $urlGenerationStrategy);
5✔
67
            }
68
        }
69

UNCOV
70
        if (null !== $totalItems) {
30✔
UNCOV
71
            $data['meta']['totalItems'] = $totalItems;
29✔
72
        }
73

UNCOV
74
        if ($paginator) {
30✔
UNCOV
75
            $data['meta']['itemsPerPage'] = (int) $itemsPerPage;
25✔
UNCOV
76
            $data['meta']['currentPage'] = (int) $currentPage;
25✔
77
        }
78

UNCOV
79
        return $data;
30✔
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @throws UnexpectedValueException
86
     */
87
    protected function getItemsData($object, ?string $format = null, array $context = []): array
88
    {
UNCOV
89
        $data = [
30✔
UNCOV
90
            'data' => [],
30✔
UNCOV
91
        ];
30✔
92

UNCOV
93
        foreach ($object as $obj) {
30✔
UNCOV
94
            $item = $this->normalizer->normalize($obj, $format, $context);
27✔
UNCOV
95
            if (!\is_array($item)) {
27✔
96
                throw new UnexpectedValueException('Expected item to be an array');
×
97
            }
98

UNCOV
99
            if (!isset($item['data'])) {
27✔
100
                throw new UnexpectedValueException('The JSON API document must contain a "data" key.');
×
101
            }
102

UNCOV
103
            $data['data'][] = $item['data'];
27✔
104

UNCOV
105
            if (isset($item['included'])) {
27✔
106
                $data['included'] = array_values(array_unique(array_merge($data['included'] ?? [], $item['included']), \SORT_REGULAR));
4✔
107
            }
108
        }
109

UNCOV
110
        return $data;
30✔
111
    }
112
}
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