-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCursorPaginationNormalizer.php
More file actions
41 lines (35 loc) · 1.18 KB
/
Copy pathCursorPaginationNormalizer.php
File metadata and controls
41 lines (35 loc) · 1.18 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
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Common\Serializer;
use PhpList\RestBundle\Common\Dto\CursorPaginationResult;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class CursorPaginationNormalizer implements NormalizerInterface
{
/**
* @param CursorPaginationResult $object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function normalize($object, string $format = null, array $context = []): array
{
$items = $object->items;
$limit = $object->limit;
$total = $object->total;
$hasNext = !empty($items) && isset($items[array_key_last($items)]['id']);
return [
'items' => $items,
'pagination' => [
'total' => $total,
'limit' => $limit,
'has_more' => count($items) === $limit,
'next_cursor' => $hasNext ? $items[array_key_last($items)]['id'] : null,
],
];
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof CursorPaginationResult;
}
}