Skip to content

Commit 51fce9a

Browse files
committed
Add cursor pagination
1 parent 7c68c3b commit 51fce9a

File tree

5 files changed

+120
-14
lines changed

5 files changed

+120
-14
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
4+
namespace Zfegg\ApiSerializerExt\Basic;
5+
6+
7+
use UnexpectedValueException;
8+
use Zfegg\ApiSerializerExt\Paginator\CursorPaginatorInterface;
9+
use Zfegg\ApiSerializerExt\Serializer\AbstractCollectionNormalizer;
10+
11+
class CursorPaginationNormalizer extends AbstractCollectionNormalizer
12+
{
13+
public const FORMAT = 'json';
14+
15+
public function supportsNormalization($data, $format = null, array $context = [])
16+
{
17+
return $data instanceof CursorPaginatorInterface && parent::supportsNormalization($data, $format, $context);
18+
}
19+
20+
protected function getPaginationData($object, array $context = []): array
21+
{
22+
if (!$object instanceof CursorPaginatorInterface) {
23+
return [];
24+
}
25+
26+
$data = [];
27+
$data['page_size'] = $object->getItemsPerPage();
28+
$data['cursor'] = $object->getCursor();
29+
$data['prev_cursor'] = $object->getPrevCursor();
30+
$data['next_cursor'] = $object->getNextCursor();
31+
32+
if ($object instanceof \Countable) {
33+
$data['total'] = \count($object);
34+
}
35+
36+
return $data;
37+
}
38+
39+
protected function getItemsData($object, string $format = null, array $context = []): array
40+
{
41+
$data = [
42+
'data' => [],
43+
];
44+
45+
foreach ($object as $obj) {
46+
$item = $this->normalizer->normalize($obj, $format, $context);
47+
if (!\is_array($item)) {
48+
throw new UnexpectedValueException('Expected item to be an array');
49+
}
50+
51+
$data['data'][] = $item;
52+
}
53+
54+
return $data;
55+
}
56+
}

src/Paginator/CursorPaginatorInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66

77
interface CursorPaginatorInterface extends PaginatorInterface
88
{
9+
public function getCursor(): ?int;
910

10-
}
11+
public function getItemsPerPage(): int;
12+
13+
public function getPrevCursor(): ?int;
14+
15+
public function getNextCursor(): ?int;
16+
}

src/Paginator/CursorPropertyTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
4+
namespace Zfegg\ApiSerializerExt\Paginator;
5+
6+
7+
trait CursorPropertyTrait
8+
{
9+
use ItemsPerPageTrait;
10+
11+
private ?int $cursor = null;
12+
13+
private ?int $prevCursor = null;
14+
15+
private ?int $nextCursor = null;
16+
17+
public function getCursor(): ?int
18+
{
19+
return $this->cursor;
20+
}
21+
22+
public function setCursor(?int $cursor)
23+
{
24+
$this->cursor = $cursor;
25+
}
26+
27+
public function getPrevCursor(): ?int
28+
{
29+
return $this->prevCursor;
30+
}
31+
32+
public function getNextCursor(): ?int
33+
{
34+
return $this->nextCursor;
35+
}
36+
}

src/Paginator/ItemsPerPageTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Zfegg\ApiSerializerExt\Paginator;
4+
5+
6+
trait ItemsPerPageTrait
7+
{
8+
private int $itemsPerPage = 30;
9+
10+
public function getItemsPerPage(): int
11+
{
12+
return $this->itemsPerPage;
13+
}
14+
15+
public function setItemsPerPage(int $itemsPerPage): void
16+
{
17+
$this->itemsPerPage = max($itemsPerPage, 1);
18+
}
19+
}
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22

3-
43
namespace Zfegg\ApiSerializerExt\Paginator;
54

65

76
trait PaginatorPropertyTrait
87
{
9-
private int $currentPage = 1;
10-
11-
private int $itemsPerPage = 30;
8+
use ItemsPerPageTrait;
129

10+
private int $currentPage = 1;
1311

1412
public function getCurrentPage(): int
1513
{
@@ -21,13 +19,4 @@ public function setCurrentPage(int $currentPage): void
2119
$this->currentPage = $currentPage;
2220
}
2321

24-
public function getItemsPerPage(): int
25-
{
26-
return $this->itemsPerPage;
27-
}
28-
29-
public function setItemsPerPage(int $itemsPerPage): void
30-
{
31-
$this->itemsPerPage = $itemsPerPage;
32-
}
3322
}

0 commit comments

Comments
 (0)