1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
App::uses('Model', 'Model'); |
4
|
|
|
App::uses('LampagerArrayCursor', 'Lampager.Model'); |
5
|
|
|
App::uses('LampagerArrayProcessor', 'Lampager.Model'); |
6
|
|
|
App::uses('LampagerTransformer', 'Lampager.Model'); |
7
|
|
|
|
8
|
|
|
use Lampager\ArrayProcessor; |
9
|
|
|
use Lampager\PaginationResult; |
10
|
|
|
use Lampager\Paginator as BasePaginator; |
11
|
|
|
use Lampager\Query; |
12
|
|
|
use Lampager\Query\Order; |
13
|
|
|
|
14
|
|
|
class LampagerPaginator extends BasePaginator |
15
|
|
|
{ |
16
|
|
|
/** @var Model */ |
17
|
|
|
public $builder; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
public $options; |
21
|
|
|
|
22
|
36 |
|
/** @var ArrayProcessor */ |
23
|
|
|
public $processor; |
24
|
36 |
|
|
25
|
36 |
|
/** @var LampagerTransformer */ |
26
|
36 |
|
public $transformer; |
27
|
36 |
|
|
28
|
|
|
public function __construct(Model $builder, array $options) |
29
|
|
|
{ |
30
|
|
|
$this->builder = $builder; |
31
|
|
|
$this->fromArray($options); |
32
|
|
|
|
33
|
|
|
$this->processor = new LampagerArrayProcessor($builder); |
34
|
36 |
|
$this->transformer = new LampagerTransformer($builder, $options); |
35
|
|
|
} |
36
|
36 |
|
|
37
|
|
|
/** |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
|
|
public static function create(Model $builder, array $options) |
41
|
|
|
{ |
42
|
|
|
return new static($builder, $options); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
36 |
|
* Add cursor parameter name for ORDER BY statement. |
47
|
|
|
* |
48
|
36 |
|
* @param int|string $column |
49
|
2 |
|
* @param string $order |
50
|
|
|
* @return $this |
51
|
36 |
|
*/ |
52
|
2 |
|
public function orderBy($column, $order = Order::ASC) |
53
|
|
|
{ |
54
|
36 |
|
if (is_int($column)) { |
55
|
|
|
list($column, $order) = explode(' ', $order) + [1 => 'ASC']; |
56
|
|
|
} |
57
|
|
|
if (strpos($column, '.') === false) { |
58
|
|
|
$column = "{$this->builder->alias}.{$column}"; |
59
|
|
|
} |
60
|
|
|
return parent::orderBy($column, strtolower($order)); |
61
|
|
|
} |
62
|
|
|
|
63
|
36 |
|
/** |
64
|
|
|
* Define options from an associative array. |
65
|
|
|
* |
66
|
36 |
|
* @param (bool|int|string[])[] $options |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
36 |
|
public function fromArray(array $options) |
70
|
|
|
{ |
71
|
36 |
|
// Not supported in CakePHP 2 version |
72
|
36 |
|
unset($options['orders']); |
73
|
36 |
|
|
74
|
|
|
// Merge with existing options |
75
|
|
|
$this->options = array_replace_recursive($this->options ?: [], $options); |
76
|
|
|
|
77
|
36 |
|
if (isset($options['order'])) { |
78
|
|
|
foreach ($options['order'] as $column => $order) { |
79
|
|
|
$this->orderBy($column, $order); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return parent::fromArray($options); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Transform Query to CakePHP query. |
88
|
|
|
* |
89
|
|
|
* @param Query $query Query. |
90
|
|
|
* @return array Options for Model::find. |
91
|
|
|
*/ |
92
|
|
|
public function transform(Query $query) |
93
|
|
|
{ |
94
|
|
|
return $this->transformer->transform($query); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Build query from the cursor. |
99
|
|
|
* |
100
|
|
|
* @param int[]|string[] $cursor Cursor. |
101
|
|
|
* @return array Options for Model::find. |
102
|
|
|
*/ |
103
|
|
|
public function build(array $cursor = []) |
104
|
|
|
{ |
105
|
|
|
return $this->transform($this->configure(new LampagerArrayCursor($this->builder, $cursor))); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int[]|string[] $cursor Cursor. |
110
|
|
|
* @return PaginationResult Result. |
111
|
|
|
*/ |
112
|
|
|
public function paginate(array $cursor = []) |
113
|
|
|
{ |
114
|
|
|
$query = $this->configure(new LampagerArrayCursor($this->builder, $cursor)); |
115
|
|
|
return $this->processor->process($query, $this->builder->find('all', $this->transform($query))); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|