-
-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathCollectionNormalizer.php
90 lines (74 loc) · 3 KB
/
CollectionNormalizer.php
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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Core\Hal\Serializer;
use ApiPlatform\Core\Serializer\AbstractCollectionNormalizer;
use ApiPlatform\Core\Util\IriHelper;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Normalizes collections in the HAL format.
*
* @author Kevin Dunglas <[email protected]>
* @author Hamza Amrouche <[email protected]>
*/
final class CollectionNormalizer extends AbstractCollectionNormalizer
{
public const FORMAT = 'jsonhal';
/**
* {@inheritdoc}
*/
protected function getPaginationData($object, array $context = []): array
{
[$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context);
$parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
$data = [
'_links' => [
'self' => ['href' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null)],
],
];
if ($paginated) {
if (null !== $lastPage) {
$data['_links']['first']['href'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.);
$data['_links']['last']['href'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
}
if (1. !== $currentPage) {
$data['_links']['prev']['href'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.);
}
if ((null !== $lastPage && $currentPage !== $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) {
$data['_links']['next']['href'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.);
}
}
if (null !== $totalItems) {
$data['totalItems'] = $totalItems;
}
if ($paginator) {
$data['itemsPerPage'] = (int) $itemsPerPage;
}
return $data;
}
/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
protected function getItemsData($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'];
}
return $data;
}
}