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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Hal/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\Hal\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 HAL format.
24
 *
25
 * @author Kevin Dunglas <dunglas@gmail.com>
26
 * @author Hamza Amrouche <hamza@les-tilleuls.coop>
27
 */
28
final class CollectionNormalizer extends AbstractCollectionNormalizer
29
{
30
    public const FORMAT = 'jsonhal';
31

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

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

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

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

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

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

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

UNCOV
69
        if (null !== $totalItems) {
×
UNCOV
70
            $data['totalItems'] = $totalItems;
×
71
        }
72

UNCOV
73
        if ($paginator) {
×
UNCOV
74
            $data['itemsPerPage'] = (int) $itemsPerPage;
×
75
        }
76

UNCOV
77
        return $data;
×
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @throws UnexpectedValueException
84
     */
85
    protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array
86
    {
UNCOV
87
        $data = [];
×
88

UNCOV
89
        foreach ($object as $obj) {
×
UNCOV
90
            $item = $this->normalizer->normalize($obj, $format, $context);
×
UNCOV
91
            if (!\is_array($item)) {
×
92
                throw new UnexpectedValueException('Expected item to be an array');
×
93
            }
UNCOV
94
            $data['_embedded']['item'][] = $item;
×
UNCOV
95
            $data['_links']['item'][] = $item['_links']['self'] ?? null;
×
96
        }
97

UNCOV
98
        return $data;
×
99
    }
100
}
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