1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
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\Core\JsonApi\Serializer; |
||
15 | |||
16 | use ApiPlatform\Core\Serializer\AbstractCollectionNormalizer; |
||
17 | use ApiPlatform\Core\Util\IriHelper; |
||
18 | use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
||
19 | |||
20 | /** |
||
21 | * Normalizes collections in the JSON API format. |
||
22 | * |
||
23 | * @author Kevin Dunglas <[email protected]> |
||
24 | * @author Hamza Amrouche <[email protected]> |
||
25 | * @author Baptiste Meyer <[email protected]> |
||
26 | */ |
||
27 | final class CollectionNormalizer extends AbstractCollectionNormalizer |
||
28 | { |
||
29 | public const FORMAT = 'jsonapi'; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | protected function getPaginationData($object, array $context = []): array |
||
35 | { |
||
36 | [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context); |
||
37 | $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName); |
||
38 | |||
39 | $data = [ |
||
40 | 'links' => [ |
||
41 | 'self' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null), |
||
42 | ], |
||
43 | ]; |
||
44 | |||
45 | if ($paginated) { |
||
46 | if (null !== $lastPage) { |
||
47 | $data['links']['first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.); |
||
48 | $data['links']['last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage); |
||
49 | } |
||
50 | |||
51 | if (1. !== $currentPage) { |
||
52 | $data['links']['prev'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.); |
||
53 | } |
||
54 | |||
55 | if (null !== $lastPage && $currentPage !== $lastPage || null === $lastPage && $pageTotalItems >= $itemsPerPage) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
56 | $data['links']['next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if (null !== $totalItems) { |
||
61 | $data['meta']['totalItems'] = $totalItems; |
||
62 | } |
||
63 | |||
64 | if ($paginator) { |
||
65 | $data['meta']['itemsPerPage'] = (int) $itemsPerPage; |
||
66 | $data['meta']['currentPage'] = (int) $currentPage; |
||
67 | } |
||
68 | |||
69 | return $data; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | * |
||
75 | * @throws UnexpectedValueException |
||
76 | */ |
||
77 | protected function getItemsData($object, string $format = null, array $context = []): array |
||
78 | { |
||
79 | $data = [ |
||
80 | 'data' => [], |
||
81 | ]; |
||
82 | |||
83 | foreach ($object as $obj) { |
||
84 | $item = $this->normalizer->normalize($obj, $format, $context); |
||
85 | if (!\is_array($item)) { |
||
86 | throw new UnexpectedValueException('Expected item to be an array'); |
||
87 | } |
||
88 | |||
89 | if (!isset($item['data'])) { |
||
90 | throw new UnexpectedValueException('The JSON API document must contain a "data" key.'); |
||
91 | } |
||
92 | |||
93 | $data['data'][] = $item['data']; |
||
94 | |||
95 | if (isset($item['included'])) { |
||
96 | $data['included'] = array_values(array_unique(array_merge($data['included'] ?? [], $item['included']), SORT_REGULAR)); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return $data; |
||
101 | } |
||
102 | } |
||
103 |