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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 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\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
20
use ApiPlatform\Util\IriHelper;
21
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
22

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

34
    public function __construct(ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
35
    {
36
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
2,499✔
37
    }
38

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

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

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

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

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

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

71
        if (null !== $totalItems) {
75✔
72
            $data['meta']['totalItems'] = $totalItems;
75✔
73
        }
74

75
        if ($paginator) {
75✔
76
            $data['meta']['itemsPerPage'] = (int) $itemsPerPage;
69✔
77
            $data['meta']['currentPage'] = (int) $currentPage;
69✔
78
        }
79

80
        return $data;
75✔
81
    }
82

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

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

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

104
            $data['data'][] = $item['data'];
66✔
105

106
            if (isset($item['included'])) {
66✔
107
                $data['included'] = array_values(array_unique(array_merge($data['included'] ?? [], $item['included']), \SORT_REGULAR));
12✔
108
            }
109
        }
110

111
        return $data;
75✔
112
    }
113
}
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

© 2026 Coveralls, Inc