generated from Sammyjo20/package-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CursorPaginator.php
44 lines (35 loc) · 1.08 KB
/
CursorPaginator.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
<?php
declare(strict_types=1);
namespace Saloon\PaginationPlugin;
use LogicException;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\PaginationPlugin\Traits\HasAsyncPagination;
abstract class CursorPaginator extends Paginator
{
use HasAsyncPagination;
/**
* Apply the pagination to the request
*/
protected function applyPagination(Request $request): Request
{
if ($this->currentResponse instanceof Response) {
$request->query()->add('cursor', $this->getNextCursor($this->currentResponse));
}
if (isset($this->perPageLimit)) {
$request->query()->add('per_page', $this->perPageLimit);
}
return $request;
}
/**
* Get the next cursor
*/
abstract protected function getNextCursor(Response $response): int|string;
/**
* Get the total number of pages
*/
protected function getTotalPages(Response $response): int
{
throw new LogicException('Please implement the `getTotalPages` method on this paginator when using asynchronous pagination.');
}
}