Skip to content

Commit ffbb287

Browse files
committed
Allow string cursor field
1 parent b16a5d5 commit ffbb287

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

packages/query/src/Data/QueryPaginate.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,40 @@
66

77
use Windwalker\Query\Query;
88

9-
readonly class QueryPaginate
9+
class QueryPaginate
1010
{
11+
public \Closure $nextHandler {
12+
get => $this->getNextHandler();
13+
}
14+
15+
protected string|\Closure $cursorField;
16+
1117
public function __construct(
1218
public int $length,
1319
/**
14-
* @var \Closure(Query $query): ?Query
20+
* @var string|\Closure(Query $query): ?Query
1521
*/
16-
public \Closure $nextHandler,
22+
\Closure|string $cursorField
1723
) {
24+
$this->cursorField = $cursorField;
25+
}
26+
27+
protected function getNextHandler(): \Closure
28+
{
29+
$field = $this->cursorField;
30+
31+
if (is_string($field)) {
32+
$field = static function (Query $query, object|array $item) use ($field) {
33+
if (is_array($item)) {
34+
$cursor = $item[$field];
35+
} else {
36+
$cursor = $item->{$field};
37+
}
38+
39+
return $query->where($field, '>', $cursor);
40+
};
41+
}
42+
43+
return $field;
1844
}
1945
}

0 commit comments

Comments
 (0)