1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Pager |
8
|
|
|
{ |
9
|
|
|
protected $page = 1; |
10
|
|
|
|
11
|
|
|
protected $limit = null; |
12
|
|
|
|
13
|
|
|
protected $totalCount; |
14
|
|
|
|
15
|
|
|
protected $maxPage; |
16
|
|
|
|
17
|
|
|
protected $pages = []; |
18
|
|
|
|
19
|
|
|
protected $isEnabled; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
public function isEnabled() |
23
|
|
|
{ |
24
|
|
|
return $this->isEnabled; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function enable() |
28
|
|
|
{ |
29
|
|
|
$this->isEnabled = true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function disable() |
33
|
|
|
{ |
34
|
|
|
$this->isEnabled = false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setOptions(array $options) |
38
|
|
|
{ |
39
|
|
|
foreach ($options as $key => $value) { |
40
|
|
|
$setter = 'set' . ucfirst($key); |
41
|
|
|
if (method_exists($this, $setter)) { |
42
|
|
|
$this->$setter($value); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getFirst() |
48
|
|
|
{ |
49
|
|
|
return ($this->page - 1) * $this->limit; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function getPage(): int |
56
|
|
|
{ |
57
|
|
|
return $this->page; |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* @param int $page |
61
|
|
|
*/ |
62
|
|
|
public function setPage(int $page): void |
63
|
|
|
{ |
64
|
|
|
$this->page = $page; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function getLimit() |
71
|
|
|
{ |
72
|
|
|
return $this->limit; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $limit |
77
|
|
|
*/ |
78
|
|
|
public function setLimit($limit): void |
79
|
|
|
{ |
80
|
|
|
$this->limit = $limit; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $totalCount |
85
|
|
|
*/ |
86
|
|
|
public function setTotalCount(int $totalCount): void |
87
|
|
|
{ |
88
|
|
|
$this->totalCount = $totalCount; |
89
|
|
|
$this->rebuildPaginationOptions(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getPaginationOptions() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'currentPage' => $this->page, |
96
|
|
|
'pages' => $this->pages |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @internal |
102
|
|
|
*/ |
103
|
|
|
public function rebuildPaginationOptions() |
104
|
|
|
{ |
105
|
|
|
if (is_int($this->limit) && is_int($this->totalCount)) { |
106
|
|
|
$this->maxPage = (int)ceil($this->totalCount / $this->limit); |
107
|
|
|
$this->page = $this->page > 0 && $this->page <= $this->maxPage ? $this->page : 1; |
108
|
|
|
$this->pages = $this->buildPages(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function buildPages() |
113
|
|
|
{ |
114
|
|
|
if ($this->maxPage === 0) { |
115
|
|
|
return [1]; |
116
|
|
|
} |
117
|
|
|
if ($this->maxPage <= 10) { |
118
|
|
|
return range(1, $this->maxPage); |
119
|
|
|
} |
120
|
|
|
if ($this->page < 5) { |
121
|
|
|
return array_merge(range(1, 6), [null, $this->maxPage]); |
122
|
|
|
} |
123
|
|
|
if ($this->page > $this->maxPage - 4) { |
124
|
|
|
return array_merge([1, null], range($this->maxPage - 5, $this->maxPage)); |
125
|
|
|
} |
126
|
|
|
return array_merge([1, null], range($this->page - 2, $this->page + 2), [null, $this->maxPage]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|