1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Http; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Container for pagination information (i.e. items per page, item offset, field sort order) |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class Pagination |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var int The max number of records to return |
33
|
|
|
*/ |
34
|
|
|
protected $max; |
35
|
|
|
/** |
36
|
|
|
* @var int The offset in the records |
37
|
|
|
*/ |
38
|
|
|
protected $offset; |
39
|
|
|
/** |
40
|
|
|
* @var array<string,bool> Associative array of field names to boolean values |
41
|
|
|
*/ |
42
|
|
|
protected $order = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a new Pagination object. |
46
|
|
|
* |
47
|
|
|
* ```php |
48
|
|
|
* // equivalent to ORDER BY foo ASC, bar DESC LIMIT 20 OFFSET 10 |
49
|
|
|
* $pagination = new \Caridea\Http\Pagination(20, 10, ['foo' => true, 'bar' => false]); |
50
|
|
|
* ``` |
51
|
|
|
* |
52
|
|
|
* @param int $max The max number of records to return. If zero or any negative number is provided, this defaults to `PHP_INT_MAX` |
53
|
|
|
* @param int $offset The offset in the records. If a negative number is provided, this defaults to `0` |
54
|
|
|
* @param array<string,bool> $order Associative array of field names to boolean values (`true` = ascending, `false` = descending). Any non-boolean value (`1` included) will evaluate as `false`. |
55
|
|
|
*/ |
56
|
2 |
|
public function __construct(int $max, int $offset, $order = []) |
57
|
|
|
{ |
58
|
2 |
|
$this->max = $this->normalize($max, PHP_INT_MAX); |
59
|
2 |
|
$this->offset = $this->normalize($offset, 0); |
60
|
2 |
|
foreach ($order as $k => $v) { |
61
|
2 |
|
if (strlen(trim($k)) !== 0) { |
62
|
2 |
|
$this->order[$k] = $v === true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
private function normalize(int $value, int $default): int |
68
|
|
|
{ |
69
|
2 |
|
return $value < 1 ? $default : $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the max number of records to return. |
74
|
|
|
* |
75
|
|
|
* @return int The max number of records to return |
76
|
|
|
*/ |
77
|
2 |
|
public function getMax(): int |
78
|
|
|
{ |
79
|
2 |
|
return $this->max; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the offset in the records. |
84
|
|
|
* |
85
|
|
|
* @return int The offset in the records |
86
|
|
|
*/ |
87
|
2 |
|
public function getOffset(): int |
88
|
|
|
{ |
89
|
2 |
|
return $this->offset; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the field ordering. |
94
|
|
|
* |
95
|
|
|
* Array keys are field names, values are `true` for ascending, `false` for descending. |
96
|
|
|
* |
97
|
|
|
* @return array<string,bool> The field order |
98
|
|
|
*/ |
99
|
2 |
|
public function getOrder(): array |
100
|
|
|
{ |
101
|
2 |
|
return $this->order; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|