1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tleckie\Paginator; |
4
|
|
|
|
5
|
|
|
use function ceil; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Paginator |
9
|
|
|
* |
10
|
|
|
* @package Tleckie\Paginator |
11
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Paginator implements PaginatorInterface |
14
|
|
|
{ |
15
|
|
|
/** @var int */ |
16
|
|
|
protected int $totalRecords; |
17
|
|
|
|
18
|
|
|
/** @var int */ |
19
|
|
|
protected int $itemsPerPage; |
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
protected int $currentPage; |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
private int $firstPage = 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Paginator constructor. |
29
|
|
|
* |
30
|
|
|
* @param int $totalRecords |
31
|
|
|
* @param int $itemsPerPage |
32
|
|
|
* @param int $currentPage |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
int $totalRecords, |
36
|
|
|
int $itemsPerPage, |
37
|
|
|
int $currentPage |
38
|
|
|
) { |
39
|
|
|
$this->setTotalRecords($totalRecords); |
40
|
|
|
$this->setItemsPerPage($itemsPerPage); |
41
|
|
|
$this->setCurrentPage($currentPage); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function getTotalRecords(): int |
48
|
|
|
{ |
49
|
|
|
return $this->totalRecords; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function setTotalRecords(int $totalRecords): PaginatorInterface |
56
|
|
|
{ |
57
|
|
|
$this->totalRecords = $totalRecords; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function getItemsPerPage(): int |
66
|
|
|
{ |
67
|
|
|
return $this->itemsPerPage; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function setItemsPerPage(int $itemsPerPage): PaginatorInterface |
74
|
|
|
{ |
75
|
|
|
$this->itemsPerPage = $itemsPerPage; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function getCurrentPage(): int |
84
|
|
|
{ |
85
|
|
|
return $this->currentPage; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function setCurrentPage(int $currentPage): PaginatorInterface |
92
|
|
|
{ |
93
|
|
|
$this->currentPage = !$currentPage ? $this->firstPage : $currentPage; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function getFirstPage(): int |
102
|
|
|
{ |
103
|
|
|
return $this->firstPage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function getNextPage(): int |
110
|
|
|
{ |
111
|
|
|
if ($this->hasNext()) { |
112
|
|
|
return $this->currentPage + $this->firstPage; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->currentPage; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function hasNext(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->currentPage < $this->getLastPage(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
|
|
public function getLastPage(): int |
130
|
|
|
{ |
131
|
|
|
return $this->getTotalPages(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
|
|
public function getTotalPages(): int |
138
|
|
|
{ |
139
|
|
|
return ceil($this->totalRecords / $this->itemsPerPage); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
public function getPrevPage(): int |
146
|
|
|
{ |
147
|
|
|
if ($this->hasPrev()) { |
148
|
|
|
return $this->currentPage - $this->firstPage; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->currentPage; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function hasPrev(): bool |
158
|
|
|
{ |
159
|
|
|
return $this->currentPage > $this->firstPage; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function getOffset(): int |
166
|
|
|
{ |
167
|
|
|
return ($this->currentPage - $this->firstPage) * $this->itemsPerPage; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|