1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\paginator; |
4
|
|
|
|
5
|
|
|
class Paginator |
6
|
|
|
{ |
7
|
|
|
const DEFAULT_ITEMS_PER_PAGE = 25; |
8
|
|
|
|
9
|
|
|
protected $currentPage = 1; |
10
|
|
|
protected $itemsPerPage = 0; |
11
|
|
|
protected $total = 0; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return int |
15
|
|
|
*/ |
16
|
|
|
public function getTotal(): int |
17
|
|
|
{ |
18
|
|
|
return $this->total; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param int $total |
23
|
|
|
*/ |
24
|
|
|
public function setTotal($total) |
25
|
|
|
{ |
26
|
|
|
$this->total = (int)$total; |
27
|
|
|
$this->validate(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
|
|
public function getCurrentPage(): int |
34
|
|
|
{ |
35
|
|
|
return $this->currentPage; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $currentPage |
40
|
|
|
*/ |
41
|
|
|
public function setCurrentPage($currentPage) |
42
|
|
|
{ |
43
|
|
|
$this->currentPage = (int)$currentPage; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return int |
48
|
|
|
*/ |
49
|
|
|
public function getItemsPerPage(): int |
50
|
|
|
{ |
51
|
|
|
return $this->itemsPerPage; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $itemsPerPage |
56
|
|
|
*/ |
57
|
|
|
public function setItemsPerPage($itemsPerPage) |
58
|
|
|
{ |
59
|
|
|
$this->itemsPerPage = (int)$itemsPerPage; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getPageCount(): int |
67
|
|
|
{ |
68
|
|
|
if (!$this->getItemsPerPage()) { |
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
return (int)\ceil($this->getTotal() / $this->getItemsPerPage()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getCurrentPages(): array |
78
|
|
|
{ |
79
|
|
|
$result = []; |
80
|
|
|
$pageCurrent = $this->getCurrentPage(); |
81
|
|
|
if ($pageCount = $this->getPageCount()) { |
82
|
|
|
if ($pageCount <= 5) { |
83
|
|
|
for ($i = 1; $i <= $pageCount; $i++) { |
84
|
|
|
$result[] = $i; |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
if ($pageCurrent <= 3) { |
88
|
|
|
for ($i = 1; $i <= 5; $i++) { |
89
|
|
|
$result[] = $i; |
90
|
|
|
} |
91
|
|
|
} elseif ($pageCount - $pageCurrent <= 2) { |
92
|
|
|
for ($i = $pageCount - 4; $i <= $pageCount; $i++) { |
93
|
|
|
$result[] = $i; |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
for ($i = $pageCurrent - 2; $i <= $pageCurrent + 2; $i++) { |
97
|
|
|
$result[] = $i; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function getOffset(): int |
110
|
|
|
{ |
111
|
|
|
return $this->itemsPerPage * ($this->currentPage - 1); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function getLimit(): int |
118
|
|
|
{ |
119
|
|
|
return $this->itemsPerPage; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
|
protected function validate() |
126
|
|
|
{ |
127
|
|
|
if ($this->currentPage < 1) { |
128
|
|
|
$this->currentPage = 1; |
129
|
|
|
} |
130
|
|
|
if ($this->itemsPerPage < 0) { |
131
|
|
|
$this->itemsPerPage = 0; |
132
|
|
|
} |
133
|
|
|
if ($this->total < 0) { |
134
|
|
|
$this->total = 0; |
135
|
|
|
} |
136
|
|
|
if ($this->itemsPerPage * ($this->currentPage - 1) > $this->total) { |
137
|
|
|
$this->currentPage = 1; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|