1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
// @codeCoverageIgnoreStart |
4
|
|
|
App::uses('Model', 'Model'); |
5
|
|
|
App::uses('LampagerTransformer', 'Lampager.Model'); |
6
|
|
|
// @codeCoverageIgnoreEnd |
7
|
|
|
|
8
|
|
|
use Lampager\Paginator as BasePaginator; |
9
|
|
|
use Lampager\Query\Order; |
10
|
|
|
|
11
|
|
|
class LampagerPaginator extends BasePaginator |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** @var Model */ |
14
|
|
|
public $builder; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
public $query; |
18
|
|
|
|
19
|
|
|
/** @var LampagerTransformer */ |
20
|
|
|
public $transformer; |
21
|
|
|
|
22
|
20 |
|
public function __construct(Model $builder, array $query) |
23
|
|
|
{ |
24
|
20 |
|
$this->builder = $builder; |
25
|
20 |
|
$this->fromArray($query); |
26
|
20 |
|
$this->transformer = new LampagerTransformer($this); |
27
|
20 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Model $builder Model. |
31
|
|
|
* @param array $query Query. |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
20 |
|
public static function create(Model $builder, array $query) |
35
|
|
|
{ |
36
|
20 |
|
return new static($builder, $query); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Add cursor parameter name for ORDER BY statement. |
41
|
|
|
* |
42
|
|
|
* @param string|int $column |
43
|
|
|
* @param string $order |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
20 |
|
public function orderBy($column, $order = Order::ASC) |
47
|
|
|
{ |
48
|
20 |
|
if (is_int($column)) { |
49
|
2 |
|
list($column, $order) = explode(' ', $order) + [1 => 'ASC']; |
50
|
|
|
} |
51
|
20 |
|
if (strpos($column, '.') === false) { |
52
|
2 |
|
$column = "{$this->builder->alias}.{$column}"; |
53
|
|
|
} |
54
|
20 |
|
return parent::orderBy($column, strtolower($order)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Define options from an associative array. |
59
|
|
|
* |
60
|
|
|
* @param (bool|int|string[])[] $options |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
20 |
|
public function fromArray(array $options) |
64
|
|
|
{ |
65
|
|
|
// Not supported in CakePHP 2 version |
66
|
20 |
|
unset($options['orders']); |
67
|
|
|
|
68
|
|
|
// Merge with existing query |
69
|
20 |
|
$this->query = array_replace_recursive($this->query ?: [], $options); |
70
|
|
|
|
71
|
20 |
|
if (isset($options['order'])) { |
72
|
20 |
|
foreach ($options['order'] as $column => $order) { |
|
|
|
|
73
|
20 |
|
$this->orderBy($column, $order); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
20 |
|
return parent::fromArray($options); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.