Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Paginate | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
limit | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
offset | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
buildPaginateSQL | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
resetLimit | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
resetOffset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Query\Builder; |
4 | |
5 | /** |
6 | * Paginate |
7 | * |
8 | * Allows pagination of results with a limit() and offset(). |
9 | * This works across database engines, abstracting away the differences. |
10 | * |
11 | * @package BO\Zmsdb\Query\Builder |
12 | * @author Alex Gisby<alex@solution10.com> |
13 | * @license MIT |
14 | */ |
15 | trait Paginate |
16 | { |
17 | /** |
18 | * @var int Limit or results per page |
19 | */ |
20 | protected $limit = null; |
21 | |
22 | /** |
23 | * @var int Offset |
24 | */ |
25 | protected $offset = 0; |
26 | |
27 | /** |
28 | * Set/Get the limit of the query |
29 | * |
30 | * @param int|null $limit Int to set, null to get |
31 | * @return $this|int |
32 | */ |
33 | public function limit($limit = null) |
34 | { |
35 | if ($limit === null) { |
36 | return $this->limit; |
37 | } |
38 | |
39 | $this->limit = (int)$limit; |
40 | return $this; |
41 | } |
42 | |
43 | /** |
44 | * Set/Get the offset of the query |
45 | * |
46 | * @param int|null $offset Int to set, null to get |
47 | * @return $this|int |
48 | */ |
49 | public function offset($offset = null) |
50 | { |
51 | if ($offset === null) { |
52 | return $this->offset; |
53 | } |
54 | |
55 | $this->offset = (int)$offset; |
56 | return $this; |
57 | } |
58 | |
59 | /** |
60 | * Builds the SQL for the pagination, based on the DB engine |
61 | * |
62 | * @return string |
63 | */ |
64 | public function buildPaginateSQL() |
65 | { |
66 | if ($this->limit === null) { |
67 | return ''; |
68 | } |
69 | |
70 | return 'LIMIT ' . (($this->offset != 0) ? $this->offset . ', ' . $this->limit : $this->limit); |
71 | } |
72 | |
73 | /** |
74 | * Resets the LIMIT portion of this query to none. |
75 | * |
76 | * @return $this |
77 | */ |
78 | public function resetLimit() |
79 | { |
80 | $this->limit = null; |
81 | return $this; |
82 | } |
83 | |
84 | /** |
85 | * Resets the OFFSET portion of this query to 0. |
86 | * |
87 | * @return $this |
88 | */ |
89 | public function resetOffset() |
90 | { |
91 | $this->offset = 0; |
92 | return $this; |
93 | } |
94 | } |