1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lampager\Cake\Datasource; |
6
|
|
|
|
7
|
|
|
use Cake\Datasource\Paging\NumericPaginator as CakePaginator; |
8
|
|
|
use Cake\Datasource\Paging\PaginatedInterface; |
9
|
|
|
use Cake\Datasource\QueryInterface; |
10
|
|
|
use Exception; |
11
|
|
|
use Lampager\Cake\ORM\Query; |
12
|
|
|
use Lampager\Cake\PaginationResult; |
13
|
|
|
use Lampager\Exceptions\InvalidArgumentException; |
14
|
|
|
use function Cake\Core\triggerWarning; |
15
|
|
|
|
16
|
|
|
class Paginator extends CakePaginator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
* @throws InvalidArgumentException if the \Lampager\Cake\ORM\Query is given |
21
|
|
|
* @return PaginationResult |
22
|
|
|
*/ |
23
|
|
|
public function paginate(mixed $target, array $params = [], array $settings = []): PaginatedInterface |
24
|
|
|
{ |
25
|
|
|
$query = null; |
26
|
|
|
if ($target instanceof QueryInterface) { |
27
|
|
|
$query = $target; |
28
|
|
|
$target = $query->getRepository(); |
29
|
|
|
if ($target === null) { |
30
|
|
|
throw new Exception('No repository set for query.'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($query instanceof Query) { |
35
|
|
|
throw new InvalidArgumentException(Query::class . ' cannot be paginated by ' . __METHOD__ . '()'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$alias = $target->getAlias(); |
39
|
|
|
$defaults = $this->getDefaults($alias, $settings); |
40
|
|
|
|
41
|
|
|
$validSettings = [ |
42
|
|
|
...array_keys($this->_defaultConfig), |
43
|
|
|
'order', |
44
|
|
|
'forward', |
45
|
|
|
'backward', |
46
|
|
|
'exclusive', |
47
|
|
|
'inclusive', |
48
|
|
|
'seekable', |
49
|
|
|
'unseekable', |
50
|
|
|
'cursor', |
51
|
|
|
]; |
52
|
|
|
$extraSettings = array_diff_key($defaults, array_flip($validSettings)); |
53
|
|
|
if ($extraSettings) { |
54
|
|
|
triggerWarning( |
55
|
|
|
'Passing query options as paginator settings is no longer supported.' |
56
|
|
|
. ' Use a custom finder through the `finder` config or pass a Query instance to paginate().' |
57
|
|
|
. ' Extra keys found are: `' . implode('`, `', array_keys($extraSettings)) . '`.' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$options = $this->mergeOptions($params, $defaults); |
62
|
|
|
$options = $this->validateSort($target, $options); |
63
|
|
|
$options = $this->checkLimit($options); |
64
|
|
|
|
65
|
|
|
$options += ['cursor' => [], 'scope' => null]; |
66
|
|
|
|
67
|
|
|
if ($query === null) { |
68
|
|
|
$args = []; |
69
|
|
|
$type = $options['finder'] ?? 'all'; |
70
|
|
|
if (is_array($type)) { |
71
|
|
|
$args = (array)current($type); |
72
|
|
|
$type = key($type); |
73
|
|
|
} |
74
|
|
|
$query = $target->find($type, ...$args); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$query = Query::fromQuery($query->applyOptions($options)); |
78
|
|
|
$query->fromArray($options); |
79
|
|
|
$query->cursor($options['cursor']); |
80
|
|
|
|
81
|
|
|
return $query->paginate(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|