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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    {
UNCOV
34
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
953✔
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);
26✔
UNCOV
43
        $parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);
26✔
44

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

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

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

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

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

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

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

UNCOV
77
        return $data;
26✔
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 = [];
26✔
88

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

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