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

api-platform / core / 9710726294

25 Jun 2024 02:01PM UTC coverage: 62.122% (-0.5%) from 62.637%
9710726294

push

github

web-flow
feat(laravel): laravel component (#5882)

* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan

33 of 77 new or added lines in 20 files covered. (42.86%)

140 existing lines in 15 files now uncovered.

10862 of 17485 relevant lines covered (62.12%)

59.64 hits per line

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

72.97
/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\JsonApi\Util\IriHelper;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
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
    {
35
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
352✔
36
    }
37

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

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

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

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

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

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

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

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

79
        return $data;
20✔
80
    }
81

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

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

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

103
            $data['data'][] = $item['data'];
20✔
104

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

110
        return $data;
20✔
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