1 |
5dea830a
|
Jan Šedivý
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace App\Utils;
|
4 |
|
|
|
5 |
|
|
|
6 |
82862b2d
|
Jan Šedivý
|
use App\Enum\EPageLimit;
|
7 |
|
|
|
8 |
5dea830a
|
Jan Šedivý
|
class Paginator
|
9 |
|
|
{
|
10 |
|
|
|
11 |
|
|
private $limit;
|
12 |
|
|
private $page;
|
13 |
|
|
|
14 |
|
|
private $pageCount;
|
15 |
|
|
private $itemCount;
|
16 |
|
|
|
17 |
|
|
private $startingPage;
|
18 |
|
|
private $endingPage;
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* Implementation of spring Data Pageable to paginate and sort JPA queries
|
22 |
|
|
* @param $page int query offset
|
23 |
|
|
* @param $limit int query limit
|
24 |
|
|
*/
|
25 |
|
|
public function __construct($page, $limit)
|
26 |
|
|
{
|
27 |
|
|
if ($page < 1)
|
28 |
|
|
{
|
29 |
|
|
$page = 1;
|
30 |
|
|
}
|
31 |
|
|
$this->page = $page;
|
32 |
|
|
|
33 |
82862b2d
|
Jan Šedivý
|
foreach (EPageLimit::$limits as $l)
|
34 |
5dea830a
|
Jan Šedivý
|
{
|
35 |
|
|
if ($l == $limit)
|
36 |
|
|
{
|
37 |
|
|
$this->limit = $limit;
|
38 |
|
|
}
|
39 |
|
|
}
|
40 |
|
|
if ($this->limit == 0)
|
41 |
|
|
{
|
42 |
82862b2d
|
Jan Šedivý
|
$this->limit = EPageLimit::$defaultLimit;
|
43 |
5dea830a
|
Jan Šedivý
|
}
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* @return int
|
48 |
|
|
*/
|
49 |
|
|
public function getPageNumber()
|
50 |
|
|
{
|
51 |
|
|
return $this->page;
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* @return int
|
56 |
|
|
*/
|
57 |
|
|
public function getPageSize()
|
58 |
|
|
{
|
59 |
|
|
return $this->limit;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
public function getOffset()
|
63 |
|
|
{
|
64 |
|
|
return ($this->page - 1) * $this->limit;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* @return Paginator
|
69 |
|
|
*/
|
70 |
|
|
public function next()
|
71 |
|
|
{
|
72 |
|
|
return $this->hasNext() ? new Paginator($this->getPageNumber() + 1, $this->getPageSize()) : $this;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* @return Paginator
|
77 |
|
|
*/
|
78 |
|
|
public function previous()
|
79 |
|
|
{
|
80 |
|
|
return $this->hasPrevious() ? new Paginator($this->getPageNumber() - 1, $this->getPageSize()) : $this;
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
public function previousOrFirst()
|
84 |
|
|
{
|
85 |
|
|
return $this->hasPrevious() ? $this->previous() : $this->first();
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
public function first()
|
89 |
|
|
{
|
90 |
|
|
return new Paginator(1, $this->getPageSize());
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* @return bool
|
95 |
|
|
*/
|
96 |
|
|
public function hasPrevious()
|
97 |
|
|
{
|
98 |
|
|
return $this->getOffset() >= $this->limit;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
/**
|
102 |
|
|
* @return bool
|
103 |
|
|
*/
|
104 |
|
|
public function hasNext()
|
105 |
|
|
{
|
106 |
|
|
return $this->getPageNumber() < $this->getPageCount();
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* @return Paginator
|
111 |
|
|
*/
|
112 |
|
|
public function last()
|
113 |
|
|
{
|
114 |
|
|
return new Paginator($this->getPageCount(), $this->getPageSize());
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
public function getPageCount()
|
118 |
|
|
{
|
119 |
|
|
return $this->pageCount;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
public function getItemCount()
|
123 |
|
|
{
|
124 |
|
|
return $this->itemCount;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Sets maximum possible page to be displayed and total number of items
|
129 |
|
|
* @param $itemCount int total count
|
130 |
|
|
*/
|
131 |
|
|
public function setPageCount($itemCount)
|
132 |
|
|
{
|
133 |
|
|
$this->itemCount = $itemCount;
|
134 |
|
|
$this->pageCount = ceil($itemCount / $this->getPageSize());
|
135 |
|
|
$this->setPagesList();
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/**
|
139 |
|
|
* Sets starting and ending page number to be displayed in template
|
140 |
|
|
*/
|
141 |
|
|
private function setPagesList()
|
142 |
|
|
{
|
143 |
|
|
$offset = 2;
|
144 |
|
|
|
145 |
|
|
if ($this->getPageNumber() + $offset >= $this->getPageCount())
|
146 |
|
|
{
|
147 |
|
|
$this->startingPage = max(1, $this->getPageCount() - 2 * $offset);
|
148 |
|
|
$this->endingPage = $this->getPageCount();
|
149 |
|
|
} else
|
150 |
|
|
{
|
151 |
|
|
$this->startingPage = max(1, $this->getPageNumber() - $offset);
|
152 |
|
|
$this->endingPage = min($this->startingPage + 2 * $offset, $this->getPageCount());
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
public function getStartingPage()
|
157 |
|
|
{
|
158 |
|
|
return $this->startingPage;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
public function getEndingPage()
|
162 |
|
|
{
|
163 |
|
|
return $this->endingPage;
|
164 |
|
|
}
|
165 |
|
|
}
|