-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathCollectionNormalizer.php
More file actions
100 lines (82 loc) · 3.79 KB
/
CollectionNormalizer.php
File metadata and controls
100 lines (82 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Hal\Serializer;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\IriHelper;
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Normalizes collections in the HAL format.
*
* @author Kevin Dunglas <dunglas@gmail.com>
* @author Hamza Amrouche <hamza@les-tilleuls.coop>
*/
final class CollectionNormalizer extends AbstractCollectionNormalizer
{
public const FORMAT = 'jsonhal';
public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
{
parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
}
/**
* {@inheritdoc}
*/
protected function getPaginationData(iterable $object, array $context = []): array
{
[$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context);
$parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);
$operation = $context['operation'] ?? $this->getOperation($context);
$urlGenerationStrategy = $operation->getUrlGenerationStrategy();
$data = [
'_links' => [
'self' => ['href' => IriHelper::createIri($parsed['uri'] ?? $parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $urlGenerationStrategy)],
],
];
if ($paginated) {
if (null !== $lastPage) {
$data['_links']['first']['href'] = IriHelper::createIri($parsed['uri'] ?? $parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1., $urlGenerationStrategy);
$data['_links']['last']['href'] = IriHelper::createIri($parsed['uri'] ?? $parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage, $urlGenerationStrategy);
}
if (1. !== $currentPage) {
$data['_links']['prev']['href'] = IriHelper::createIri($parsed['uri'] ?? $parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1., $urlGenerationStrategy);
}
if ((null !== $lastPage && $currentPage !== $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) {
$data['_links']['next']['href'] = IriHelper::createIri($parsed['uri'] ?? $parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1., $urlGenerationStrategy);
}
}
if (null !== $totalItems) {
$data['totalItems'] = $totalItems;
}
if ($paginator) {
$data['itemsPerPage'] = (int) $itemsPerPage;
}
return $data;
}
/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array
{
$data = [];
foreach ($object as $obj) {
$item = $this->normalizer->normalize($obj, $format, $context);
if (!\is_array($item)) {
throw new UnexpectedValueException('Expected item to be an array');
}
$data['_embedded']['item'][] = $item;
$data['_links']['item'][] = $item['_links']['self'] ?? null;
}
return $data;
}
}