• 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

97.3
/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);
283✔
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);
32✔
45
        $parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);
32✔
46

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

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

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

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

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

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

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

80
        return $data;
32✔
81
    }
82

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

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

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

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

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

111
        return $data;
28✔
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

© 2025 Coveralls, Inc