Repository navigation
Expand file tree
/
Copy pathCursor.php
More file actions
111 lines (94 loc) · 2.95 KB
/
Copy pathCursor.php
File metadata and controls
111 lines (94 loc) · 2.95 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Paginator;
use Hyperf\Contract\Arrayable;
use JsonException;
use UnexpectedValueException;
use function Hyperf\Collection\collect;
class Cursor implements Arrayable
{
/**
* Create a new cursor instance.
* @param array $parameters the parameters associated with the cursor
* @param bool $pointsToNextItems determine whether the cursor points to the next or previous set of items
*/
public function __construct(
protected array $parameters,
protected bool $pointsToNextItems = true
) {
}
/**
* Get the given parameter from the cursor.
*/
public function parameter(string $parameterName): ?string
{
if (! array_key_exists($parameterName, $this->parameters)) {
throw new UnexpectedValueException("Unable to find parameter [{$parameterName}] in pagination item.");
}
return (string) $this->parameters[$parameterName];
}
/**
* Get the given parameters from the cursor.
*/
public function parameters(array $parameterNames): array
{
return collect($parameterNames)->map(function ($parameterName) {
return $this->parameter($parameterName);
})->toArray();
}
/**
* Determine whether the cursor points to the next set of items.
*/
public function pointsToNextItems(): bool
{
return $this->pointsToNextItems;
}
/**
* Determine whether the cursor points to the previous set of items.
*/
public function pointsToPreviousItems(): bool
{
return ! $this->pointsToNextItems;
}
/**
* Get the array representation of the cursor.
*/
public function toArray(): array
{
return array_merge($this->parameters, [
'_pointsToNextItems' => $this->pointsToNextItems,
]);
}
/**
* Get the encoded string representation of the cursor to construct a URL.
*/
public function encode(): string
{
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(json_encode($this->toArray(), JSON_THROW_ON_ERROR)));
}
/**
* Get a cursor instance from the encoded string representation.
*/
public static function fromEncoded(?string $encodedString): ?static
{
if (! is_string($encodedString)) {
return null;
}
try {
$parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedString)), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return null;
}
$pointsToNextItems = $parameters['_pointsToNextItems'];
unset($parameters['_pointsToNextItems']);
return new static($parameters, $pointsToNextItems);
}
}