This commit is contained in:
Kilian Hofmann
2021-06-01 19:55:55 +02:00
parent 052cbe3038
commit d8c489c714
328 changed files with 36645 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace Nuwave\Lighthouse\Pagination;
use Illuminate\Support\Arr;
/**
* Encode and decode pagination cursors.
*
* Currently, the underlying pagination Query uses offset based navigation, so
* this basically just encodes an offset. This is enough to satisfy the constraints
* that Relay has, but not a clean permanent solution.
*
* TODO Implement actual cursor pagination https://github.com/nuwave/lighthouse/issues/311
*/
class Cursor
{
/**
* Decode cursor from query arguments.
*
* If no 'after' argument is provided or the contents are not a valid base64 string,
* this will return 0. That will effectively reset pagination, so the user gets the
* first slice.
*
* @param array $args
* @return int
*/
public static function decode(array $args): int
{
if ($cursor = Arr::get($args, 'after')) {
return (int) base64_decode($cursor);
}
return 0;
}
/**
* Encode the given offset to make the implementation opaque.
*
* @param int $offset
* @return string
*/
public static function encode(int $offset): string
{
return base64_encode($offset);
}
}