• 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

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

33
    public function __construct(ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
34
    {
35
        parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
271✔
36
    }
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function getPaginationData(iterable $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' => ['href' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $urlGenerationStrategy)],
20✔
52
            ],
20✔
53
        ];
20✔
54

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

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

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

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

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

78
        return $data;
20✔
79
    }
80

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

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

99
        return $data;
20✔
100
    }
101
}
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