1 |
<?php
|
|
2 |
|
|
3 |
namespace Nuwave\Lighthouse\Pagination; |
|
4 |
|
|
5 |
use Illuminate\Support\Arr; |
|
6 |
|
|
7 |
/**
|
|
8 |
* Encode and decode pagination cursors.
|
|
9 |
*
|
|
10 |
* Currently, the underlying pagination Query uses offset based navigation, so
|
|
11 |
* this basically just encodes an offset. This is enough to satisfy the constraints
|
|
12 |
* that Relay has, but not a clean permanent solution.
|
|
13 |
*
|
|
14 |
* TODO Implement actual cursor pagination https://github.com/nuwave/lighthouse/issues/311
|
|
15 |
*/
|
|
16 |
class Cursor |
|
17 |
{
|
|
18 |
/**
|
|
19 |
* Decode cursor from query arguments.
|
|
20 |
*
|
|
21 |
* If no 'after' argument is provided or the contents are not a valid base64 string,
|
|
22 |
* this will return 0. That will effectively reset pagination, so the user gets the
|
|
23 |
* first slice.
|
|
24 |
*
|
|
25 |
* @param array<string, mixed> $args
|
|
26 |
*/
|
|
27 | 1 |
public static function decode(array $args): int |
28 |
{
|
|
29 | 1 |
if ($cursor = Arr::get($args, 'after')) { |
30 |
return (int) \Safe\base64_decode($cursor); |
|
31 |
}
|
|
32 |
|
|
33 | 1 |
return 0; |
34 |
}
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Encode the given offset to make the implementation opaque.
|
|
38 |
*/
|
|
39 | 1 |
public static function encode(int $offset): string |
40 |
{
|
|
41 | 1 |
return base64_encode((string) $offset); |
42 |
}
|
|
43 |
}
|
Read our documentation on viewing source code .