1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Relay\Connection; |
6
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
8
|
|
|
|
9
|
|
|
class Paginator |
10
|
|
|
{ |
11
|
|
|
public const MODE_REGULAR = false; |
12
|
|
|
public const MODE_PROMISE = true; |
13
|
|
|
|
14
|
|
|
/** @var callable */ |
15
|
|
|
private $fetcher; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
private $promise; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
private $totalCount; |
22
|
|
|
|
23
|
|
|
/** @var ConnectionBuilder */ |
24
|
|
|
private $connectionBuilder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param callable $fetcher |
28
|
|
|
* @param bool $promise |
29
|
|
|
*/ |
30
|
14 |
|
public function __construct(callable $fetcher, bool $promise = self::MODE_REGULAR, ConnectionBuilder $connectionBuilder = null) |
31
|
|
|
{ |
32
|
14 |
|
$this->fetcher = $fetcher; |
33
|
14 |
|
$this->promise = $promise; |
34
|
14 |
|
$this->connectionBuilder = $connectionBuilder ?: new ConnectionBuilder(); |
35
|
14 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Argument $args |
39
|
|
|
* @param int|callable $total |
40
|
|
|
* @param array $callableArgs |
41
|
|
|
* |
42
|
|
|
* @return ConnectionInterface|object A connection or a promise |
43
|
|
|
*/ |
44
|
7 |
|
public function backward($args, $total, array $callableArgs = []) |
45
|
|
|
{ |
46
|
7 |
|
$total = $this->computeTotalCount($total, $callableArgs); |
47
|
|
|
|
48
|
7 |
|
$args = $this->protectArgs($args); |
49
|
7 |
|
$limit = $args['last']; |
50
|
7 |
|
$offset = \max(0, $this->connectionBuilder->getOffsetWithDefault($args['before'], $total) - $limit); |
51
|
|
|
|
52
|
7 |
|
$entities = \call_user_func($this->fetcher, $offset, $limit); |
53
|
|
|
|
54
|
|
|
return $this->handleEntities($entities, function ($entities) use ($args, $offset, $total) { |
55
|
7 |
|
return $this->connectionBuilder->connectionFromArraySlice($entities, $args, [ |
56
|
7 |
|
'sliceStart' => $offset, |
57
|
7 |
|
'arrayLength' => $total, |
58
|
|
|
]); |
59
|
7 |
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Argument $args |
64
|
|
|
* |
65
|
|
|
* @return ConnectionInterface|object A connection or a promise |
66
|
|
|
*/ |
67
|
7 |
|
public function forward(Argument $args) |
68
|
|
|
{ |
69
|
7 |
|
$args = $this->protectArgs($args); |
70
|
7 |
|
$limit = $args['first']; |
71
|
7 |
|
$offset = $this->connectionBuilder->getOffsetWithDefault($args['after'], 0); |
72
|
|
|
|
73
|
|
|
// If we don't have a cursor or if it's not valid, then we must not use the slice method |
74
|
7 |
|
if (!\is_numeric($this->connectionBuilder->cursorToOffset($args['after'])) || !$args['after']) { |
75
|
4 |
|
$entities = \call_user_func($this->fetcher, $offset, $limit ? $limit + 1 : $limit); |
76
|
|
|
|
77
|
|
|
return $this->handleEntities($entities, function ($entities) use ($args) { |
78
|
3 |
|
return $this->connectionBuilder->connectionFromArray($entities, $args); |
79
|
4 |
|
}); |
80
|
|
|
} else { |
81
|
3 |
|
$entities = \call_user_func($this->fetcher, $offset, $limit ? $limit + 2 : $limit); |
82
|
|
|
|
83
|
|
|
return $this->handleEntities($entities, function ($entities) use ($args, $offset) { |
84
|
3 |
|
return $this->connectionBuilder->connectionFromArraySlice($entities, $args, [ |
85
|
3 |
|
'sliceStart' => $offset, |
86
|
3 |
|
'arrayLength' => $offset + \count($entities), |
87
|
|
|
]); |
88
|
3 |
|
}); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Argument $args |
94
|
|
|
* @param int|callable $total |
95
|
|
|
* @param array $callableArgs |
96
|
|
|
* |
97
|
|
|
* @return ConnectionInterface|object A connection or a promise |
98
|
|
|
*/ |
99
|
5 |
|
public function auto(Argument $args, $total, array $callableArgs = []) |
100
|
|
|
{ |
101
|
5 |
|
$args = $this->protectArgs($args); |
102
|
|
|
|
103
|
5 |
|
if ($args['last']) { |
104
|
3 |
|
$connection = $this->backward($args, $total, $callableArgs); |
105
|
|
|
} else { |
106
|
2 |
|
$connection = $this->forward($args); |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
if ($this->promise) { |
110
|
|
|
return $connection->then(function (ConnectionInterface $connection) use ($total, $callableArgs) { |
111
|
|
|
$connection->setTotalCount($this->computeTotalCount($total, $callableArgs)); |
112
|
|
|
|
113
|
|
|
return $connection; |
114
|
1 |
|
}); |
115
|
|
|
} else { |
116
|
4 |
|
$connection->setTotalCount($this->computeTotalCount($total, $callableArgs)); |
117
|
|
|
|
118
|
4 |
|
return $connection; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array|object $entities An array of entities to paginate or a promise |
124
|
|
|
* @param callable $callback |
125
|
|
|
* |
126
|
|
|
* @return ConnectionInterface|object A connection or a promise |
127
|
|
|
*/ |
128
|
14 |
|
private function handleEntities($entities, callable $callback) |
129
|
|
|
{ |
130
|
14 |
|
if ($this->promise) { |
131
|
1 |
|
return $entities->then($callback); |
132
|
|
|
} |
133
|
|
|
|
134
|
13 |
|
return \call_user_func($callback, $entities); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Argument|array $args |
139
|
|
|
* |
140
|
|
|
* @return Argument |
141
|
|
|
*/ |
142
|
14 |
|
private function protectArgs($args): Argument |
143
|
|
|
{ |
144
|
14 |
|
return $args instanceof Argument ? $args : new Argument($args); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int|callable $total |
149
|
|
|
* @param array $callableArgs |
150
|
|
|
* |
151
|
|
|
* @return int|mixed |
152
|
|
|
*/ |
153
|
8 |
|
private function computeTotalCount($total, array $callableArgs = []) |
154
|
|
|
{ |
155
|
8 |
|
if (null !== $this->totalCount) { |
156
|
3 |
|
return $this->totalCount; |
157
|
|
|
} |
158
|
|
|
|
159
|
8 |
|
$this->totalCount = \is_callable($total) ? \call_user_func_array($total, $callableArgs) : $total; |
160
|
|
|
|
161
|
8 |
|
return $this->totalCount; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|