-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathPaginator.php
More file actions
135 lines (114 loc) · 4.44 KB
/
Copy pathPaginator.php
File metadata and controls
135 lines (114 loc) · 4.44 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Overblog\GraphQLBundle\Relay\Connection;
use GraphQL\Executor\Promise\Promise;
use Overblog\GraphQLBundle\Definition\ArgumentInterface;
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
use function call_user_func;
use function call_user_func_array;
use function count;
use function is_callable;
use function is_numeric;
use function max;
class Paginator
{
public const MODE_REGULAR = false;
public const MODE_PROMISE = true;
private bool $promise;
private ConnectionBuilder $connectionBuilder;
/** @var callable */
private $fetcher;
public function __construct(callable $fetcher, bool $promise = self::MODE_REGULAR, ConnectionBuilder $connectionBuilder = null)
{
$this->fetcher = $fetcher;
$this->promise = $promise;
$this->connectionBuilder = $connectionBuilder ?? new ConnectionBuilder();
}
/**
* @param int|callable $total
*
* @return Connection|Promise A connection or a promise
*/
public function backward(ArgumentInterface $args, $total, array $callableArgs = [])
{
$total = $this->computeTotalCount($total, $callableArgs);
$limit = $args['last'] ?? null;
$before = $args['before'] ?? null;
$offset = max(0, $this->connectionBuilder->getOffsetWithDefault($before, $total) - $limit);
$entities = call_user_func($this->fetcher, $offset, $limit);
return $this->handleEntities($entities, function ($entities) use ($args, $offset, $total) {
return $this->connectionBuilder->connectionFromArraySlice($entities, $args, [
'sliceStart' => $offset,
'arrayLength' => $total,
]);
});
}
/**
* @return Connection|Promise A connection or a promise
*/
public function forward(ArgumentInterface $args)
{
$limit = $args['first'] ?? null;
$after = $args['after'] ?? null;
$offset = $this->connectionBuilder->getOffsetWithDefault($after, 0);
// If we don't have a cursor or if it's not valid, then we must not use the slice method
if (!is_numeric($this->connectionBuilder->cursorToOffset($after)) || !$after) {
$entities = call_user_func($this->fetcher, $offset, $limit ? $limit + 1 : $limit);
return $this->handleEntities($entities, fn ($entities) => $this->connectionBuilder->connectionFromArray($entities, $args));
} else {
$entities = call_user_func($this->fetcher, $offset, $limit ? $limit + 2 : $limit);
return $this->handleEntities($entities, function ($entities) use ($args, $offset) {
return $this->connectionBuilder->connectionFromArraySlice($entities, $args, [
'sliceStart' => $offset,
'arrayLength' => $offset + count($entities),
]);
});
}
}
/**
* @param int|callable $total
*
* @return Connection|Promise A connection or a promise
*/
public function auto(ArgumentInterface $args, $total, array $callableArgs = [])
{
if (isset($args['last'])) {
$connection = $this->backward($args, $total, $callableArgs);
} else {
$connection = $this->forward($args);
}
if ($this->promise) {
/** @var Promise $connection */
return $connection->then(function (ConnectionInterface $connection) use ($total, $callableArgs) {
$connection->setTotalCount($this->computeTotalCount($total, $callableArgs));
return $connection;
});
} else {
/** @var Connection $connection */
$connection->setTotalCount($this->computeTotalCount($total, $callableArgs));
return $connection;
}
}
/**
* @param array<int, string>|Promise $entities An array of entities to paginate or a promise
*
* @return Connection|Promise A connection or a promise
*/
private function handleEntities($entities, callable $callback)
{
if ($this->promise) {
/** @var Promise $entities */
return $entities->then($callback);
}
return $callback($entities);
}
/**
* @param int|callable $total
*
* @return int
*/
private function computeTotalCount($total, array $callableArgs = []): int
{
return is_callable($total) ? call_user_func_array($total, $callableArgs) : $total;
}
}