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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

96.77
/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
    {
34
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
660✔
35
    }
36

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

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

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

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

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

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

69
        if (null !== $totalItems) {
14✔
70
            $data['totalItems'] = $totalItems;
10✔
71
        }
72

73
        if ($paginator) {
14✔
74
            $data['itemsPerPage'] = (int) $itemsPerPage;
8✔
75
        }
76

77
        return $data;
14✔
78
    }
79

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

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

98
        return $data;
14✔
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