@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | { |
| 210 | 210 | $lines = file($filename, FILE_IGNORE_NEW_LINES); |
| 211 | 211 | |
| 212 | - if(!$lines) { |
|
| 212 | + if (!$lines) { |
|
| 213 | 213 | return []; |
| 214 | 214 | } |
| 215 | 215 | |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | */ |
| 228 | 228 | public function fileName(string $path): string |
| 229 | 229 | { |
| 230 | - return (string)pathinfo($path, PATHINFO_FILENAME); |
|
| 230 | + return (string) pathinfo($path, PATHINFO_FILENAME); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | */ |
| 238 | 238 | public function extension(string $path): string |
| 239 | 239 | { |
| 240 | - return (string)pathinfo($path, PATHINFO_EXTENSION); |
|
| 240 | + return (string) pathinfo($path, PATHINFO_EXTENSION); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
@@ -82,7 +82,7 @@ |
||
| 82 | 82 | if (!empty($value)) { |
| 83 | 83 | $captcha = CaptchaManager::getHandler(); |
| 84 | 84 | |
| 85 | - if (!$captcha->verify($value)){ |
|
| 85 | + if (!$captcha->verify($value)) { |
|
| 86 | 86 | $this->addError($field, 'captcha', $param); |
| 87 | 87 | } |
| 88 | 88 | } |
@@ -124,8 +124,9 @@ |
||
| 124 | 124 | */ |
| 125 | 125 | public function verify(string $code): bool |
| 126 | 126 | { |
| 127 | - if (is_null($this->secretKey)) |
|
| 128 | - throw new Exception('The secret key is not set'); |
|
| 127 | + if (is_null($this->secretKey)) { |
|
| 128 | + throw new Exception('The secret key is not set'); |
|
| 129 | + } |
|
| 129 | 130 | |
| 130 | 131 | if (empty($code)) { |
| 131 | 132 | $this->errorCodes = ['internal-empty-response']; |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | $this->matchedRoutes = $this->findMatches($uri); |
| 103 | 103 | |
| 104 | 104 | if (empty($this->matchedRoutes)) { |
| 105 | - stop(function () { |
|
| 105 | + stop(function() { |
|
| 106 | 106 | $this->handleNotFound(); |
| 107 | 107 | }); |
| 108 | 108 | } |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | $routePattern = '(\/)?'; |
| 204 | 204 | $routeParams = []; |
| 205 | 205 | |
| 206 | - $lastIndex = (int)array_key_last($routeSegments); |
|
| 206 | + $lastIndex = (int) array_key_last($routeSegments); |
|
| 207 | 207 | |
| 208 | 208 | foreach ($routeSegments as $index => $segment) { |
| 209 | 209 | $segmentParam = $this->getSegmentParam($segment, $index, $lastIndex); |
@@ -4,37 +4,37 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface PaginatorInterface |
| 6 | 6 | { |
| 7 | - public function __construct($dbal, int $per_page, int $page); |
|
| 7 | + public function __construct($dbal, int $per_page, int $page); |
|
| 8 | 8 | |
| 9 | - public function getPagination(bool $withBaseUrl): ?string; |
|
| 9 | + public function getPagination(bool $withBaseUrl): ?string; |
|
| 10 | 10 | |
| 11 | - public function currentPageNumber(): int; |
|
| 11 | + public function currentPageNumber(): int; |
|
| 12 | 12 | |
| 13 | - public function currentPageLink(): ?string; |
|
| 13 | + public function currentPageLink(): ?string; |
|
| 14 | 14 | |
| 15 | - public function previousPageLink(): ?string; |
|
| 15 | + public function previousPageLink(): ?string; |
|
| 16 | 16 | |
| 17 | - public function previousPageNumber(): ?int; |
|
| 17 | + public function previousPageNumber(): ?int; |
|
| 18 | 18 | |
| 19 | - public function firstPageLink(): ?string; |
|
| 19 | + public function firstPageLink(): ?string; |
|
| 20 | 20 | |
| 21 | - public function nextPageLink(): ?string; |
|
| 21 | + public function nextPageLink(): ?string; |
|
| 22 | 22 | |
| 23 | - public function nextPageNumber(): ?int; |
|
| 23 | + public function nextPageNumber(): ?int; |
|
| 24 | 24 | |
| 25 | - public function lastPageLink(): ?string; |
|
| 25 | + public function lastPageLink(): ?string; |
|
| 26 | 26 | |
| 27 | - public function lastPageNumber(); |
|
| 27 | + public function lastPageNumber(); |
|
| 28 | 28 | |
| 29 | - public function firstItem(); |
|
| 29 | + public function firstItem(); |
|
| 30 | 30 | |
| 31 | - public function lastItem(); |
|
| 31 | + public function lastItem(); |
|
| 32 | 32 | |
| 33 | - public function perPage(); |
|
| 33 | + public function perPage(); |
|
| 34 | 34 | |
| 35 | - public function total(); |
|
| 35 | + public function total(); |
|
| 36 | 36 | |
| 37 | - public function links(bool $withBaseUrl); |
|
| 37 | + public function links(bool $withBaseUrl); |
|
| 38 | 38 | |
| 39 | - public function data(); |
|
| 39 | + public function data(); |
|
| 40 | 40 | } |
@@ -11,310 +11,310 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Paginator implements PaginatorInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var int |
|
| 16 | - */ |
|
| 17 | - private $total; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var SleekDbal |
|
| 21 | - */ |
|
| 22 | - private $dbal; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var int |
|
| 26 | - */ |
|
| 27 | - protected $per_page; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var int |
|
| 31 | - */ |
|
| 32 | - protected $page; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - public $data; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param $sleekDbal |
|
| 41 | - * @param int $per_page |
|
| 42 | - * @param int $page |
|
| 43 | - * @throws DatabaseException |
|
| 44 | - * @throws ModelException |
|
| 45 | - * @throws IOException |
|
| 46 | - * @throws InvalidArgumentException |
|
| 47 | - * @throws InvalidConfigurationException |
|
| 48 | - */ |
|
| 49 | - public function __construct($sleekDbal, int $per_page, int $page = 1) |
|
| 50 | - { |
|
| 51 | - /** @var SleekDbal $sleekDbal */ |
|
| 52 | - $this->setTotal($sleekDbal); |
|
| 53 | - $this->dbal = $sleekDbal; |
|
| 54 | - $this->dbal->limit($per_page)->offset($per_page * ($page - 1)); |
|
| 55 | - $this->data = $this->dbal->getBuilder()->getQuery()->fetch(); |
|
| 56 | - $this->per_page = $per_page; |
|
| 57 | - $this->page = $page; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return int |
|
| 62 | - */ |
|
| 63 | - public function currentPageNumber(): int |
|
| 64 | - { |
|
| 65 | - return $this->page; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param bool $withBaseUrl |
|
| 70 | - * @return string|null |
|
| 71 | - */ |
|
| 72 | - public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 73 | - { |
|
| 74 | - $current = null; |
|
| 75 | - if (!empty($this->page)) { |
|
| 76 | - $current = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->page; |
|
| 77 | - } |
|
| 78 | - return $current; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @return int|null |
|
| 83 | - */ |
|
| 84 | - public function previousPageNumber(): ?int |
|
| 85 | - { |
|
| 86 | - $previous = null; |
|
| 87 | - if ($this->page > 1) { |
|
| 88 | - $previous = $this->page - 1; |
|
| 89 | - } elseif ($this->page == 1) { |
|
| 90 | - $previous = $this->page; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return $previous; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param bool $withBaseUrl |
|
| 98 | - * @return string|null |
|
| 99 | - */ |
|
| 100 | - public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 101 | - { |
|
| 102 | - $previous = null; |
|
| 103 | - if (!empty($this->previousPageNumber())) { |
|
| 104 | - $previous = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->previousPageNumber(); |
|
| 105 | - } |
|
| 106 | - return $previous; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return int|null |
|
| 111 | - */ |
|
| 112 | - public function nextPageNumber(): ?int |
|
| 113 | - { |
|
| 114 | - $next = null; |
|
| 115 | - if ($this->page < $this->lastPageNumber()) { |
|
| 116 | - $next = $this->page + 1; |
|
| 117 | - } elseif ($this->page == $this->lastPageNumber()) { |
|
| 118 | - $next = $this->page; |
|
| 119 | - } |
|
| 120 | - return $next; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param bool $withBaseUrl |
|
| 125 | - * @return string|null |
|
| 126 | - */ |
|
| 127 | - public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 128 | - { |
|
| 129 | - $next = null; |
|
| 130 | - if (!empty($this->nextPageNumber())) { |
|
| 131 | - $next = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->nextPageNumber(); |
|
| 132 | - } |
|
| 133 | - return $next; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param bool $withBaseUrl |
|
| 138 | - * @return string|null |
|
| 139 | - */ |
|
| 140 | - public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 141 | - { |
|
| 142 | - return $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=1'; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return int |
|
| 147 | - */ |
|
| 148 | - public function lastPageNumber(): int |
|
| 149 | - { |
|
| 150 | - return (int)ceil($this->total() / $this->per_page); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param bool $withBaseUrl |
|
| 155 | - * @return string|null |
|
| 156 | - */ |
|
| 157 | - public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 158 | - { |
|
| 159 | - $last = null; |
|
| 160 | - if (!empty($this->lastPageNumber())) { |
|
| 161 | - $last = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->lastPageNumber(); |
|
| 162 | - } |
|
| 163 | - return $last; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return mixed |
|
| 168 | - */ |
|
| 169 | - public function firstItem() |
|
| 170 | - { |
|
| 171 | - return $this->data[array_key_first($this->data)]; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return mixed |
|
| 176 | - */ |
|
| 177 | - public function lastItem() |
|
| 178 | - { |
|
| 179 | - return $this->data[array_key_last($this->data)]; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - public function perPage(): int |
|
| 186 | - { |
|
| 187 | - return $this->per_page; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function total(): int |
|
| 194 | - { |
|
| 195 | - return $this->total; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param bool $withBaseUrl |
|
| 200 | - * @return array |
|
| 201 | - */ |
|
| 202 | - public function links(bool $withBaseUrl = false): array |
|
| 203 | - { |
|
| 204 | - $links = []; |
|
| 205 | - for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 206 | - $links[] = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $i; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $links; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param bool $withBaseUrl |
|
| 214 | - * @param $pageItemsCount |
|
| 215 | - * @return string|null |
|
| 216 | - */ |
|
| 217 | - public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 218 | - { |
|
| 219 | - if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 220 | - $pageItemsCount = 3; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $currentPage = $this->currentPageNumber(); |
|
| 224 | - $totalPages = $this->lastPageNumber(); |
|
| 225 | - |
|
| 226 | - if ($totalPages <= 1) { |
|
| 227 | - return null; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $pagination = '<ul class="pagination">'; |
|
| 231 | - |
|
| 232 | - if ($currentPage > 1) { |
|
| 233 | - $pagination .= '<li><a href="' . $this->previousPageLink() . '">« Previous</a></li>'; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if ($pageItemsCount) { |
|
| 237 | - $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 238 | - $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 239 | - $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 240 | - |
|
| 241 | - if ($startPage > 1) { |
|
| 242 | - $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 243 | - if ($startPage > 2) { |
|
| 244 | - $pagination .= '<li><span>...</span></li>'; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $links = $this->links($withBaseUrl); |
|
| 249 | - for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 250 | - $active = $i == $currentPage ? 'class="active"' : ''; |
|
| 251 | - $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ($endPage < $totalPages) { |
|
| 255 | - if ($endPage < $totalPages - 1) { |
|
| 256 | - $pagination .= '<li><span>...</span></li>'; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if ($currentPage < $totalPages) { |
|
| 264 | - $pagination .= '<li><a href="' . $this->nextPageLink() . '">Next »</a></li>'; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $pagination .= '</ul>'; |
|
| 268 | - |
|
| 269 | - return $pagination; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @return array|SleekDbal[] |
|
| 274 | - */ |
|
| 275 | - public function data(): array |
|
| 276 | - { |
|
| 277 | - $result = array_map(function ($element) { |
|
| 278 | - $item = clone $this->dbal; |
|
| 279 | - $item->setData($element); |
|
| 280 | - $item->setModifiedFields($element); |
|
| 281 | - $item->setIsNew(false); |
|
| 282 | - return $item; |
|
| 283 | - }, $this->data); |
|
| 284 | - |
|
| 285 | - return $result ?? []; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param SleekDbal $sleekDbal |
|
| 290 | - * @return void |
|
| 291 | - * @throws DatabaseException |
|
| 292 | - * @throws ModelException |
|
| 293 | - * @throws IOException |
|
| 294 | - * @throws InvalidArgumentException |
|
| 295 | - * @throws InvalidConfigurationException |
|
| 296 | - */ |
|
| 297 | - private function setTotal(SleekDbal $sleekDbal) |
|
| 298 | - { |
|
| 299 | - $this->total = count($sleekDbal->getBuilder()->getQuery()->fetch()); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @param bool $withBaseUrl |
|
| 304 | - * @return string |
|
| 305 | - */ |
|
| 306 | - private function getUri(bool $withBaseUrl = false): string |
|
| 307 | - { |
|
| 308 | - $base_url = base_url(); |
|
| 309 | - $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 310 | - $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 311 | - $url = $routeUrl; |
|
| 312 | - if ($withBaseUrl) { |
|
| 313 | - $url = $base_url . $routeUrl; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $delimiter = str_contains($url, '?') ? '&' : '?'; |
|
| 317 | - |
|
| 318 | - return $url . $delimiter; |
|
| 319 | - } |
|
| 14 | + /** |
|
| 15 | + * @var int |
|
| 16 | + */ |
|
| 17 | + private $total; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var SleekDbal |
|
| 21 | + */ |
|
| 22 | + private $dbal; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var int |
|
| 26 | + */ |
|
| 27 | + protected $per_page; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | + protected $page; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + public $data; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param $sleekDbal |
|
| 41 | + * @param int $per_page |
|
| 42 | + * @param int $page |
|
| 43 | + * @throws DatabaseException |
|
| 44 | + * @throws ModelException |
|
| 45 | + * @throws IOException |
|
| 46 | + * @throws InvalidArgumentException |
|
| 47 | + * @throws InvalidConfigurationException |
|
| 48 | + */ |
|
| 49 | + public function __construct($sleekDbal, int $per_page, int $page = 1) |
|
| 50 | + { |
|
| 51 | + /** @var SleekDbal $sleekDbal */ |
|
| 52 | + $this->setTotal($sleekDbal); |
|
| 53 | + $this->dbal = $sleekDbal; |
|
| 54 | + $this->dbal->limit($per_page)->offset($per_page * ($page - 1)); |
|
| 55 | + $this->data = $this->dbal->getBuilder()->getQuery()->fetch(); |
|
| 56 | + $this->per_page = $per_page; |
|
| 57 | + $this->page = $page; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return int |
|
| 62 | + */ |
|
| 63 | + public function currentPageNumber(): int |
|
| 64 | + { |
|
| 65 | + return $this->page; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param bool $withBaseUrl |
|
| 70 | + * @return string|null |
|
| 71 | + */ |
|
| 72 | + public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 73 | + { |
|
| 74 | + $current = null; |
|
| 75 | + if (!empty($this->page)) { |
|
| 76 | + $current = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->page; |
|
| 77 | + } |
|
| 78 | + return $current; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @return int|null |
|
| 83 | + */ |
|
| 84 | + public function previousPageNumber(): ?int |
|
| 85 | + { |
|
| 86 | + $previous = null; |
|
| 87 | + if ($this->page > 1) { |
|
| 88 | + $previous = $this->page - 1; |
|
| 89 | + } elseif ($this->page == 1) { |
|
| 90 | + $previous = $this->page; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return $previous; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param bool $withBaseUrl |
|
| 98 | + * @return string|null |
|
| 99 | + */ |
|
| 100 | + public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 101 | + { |
|
| 102 | + $previous = null; |
|
| 103 | + if (!empty($this->previousPageNumber())) { |
|
| 104 | + $previous = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->previousPageNumber(); |
|
| 105 | + } |
|
| 106 | + return $previous; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return int|null |
|
| 111 | + */ |
|
| 112 | + public function nextPageNumber(): ?int |
|
| 113 | + { |
|
| 114 | + $next = null; |
|
| 115 | + if ($this->page < $this->lastPageNumber()) { |
|
| 116 | + $next = $this->page + 1; |
|
| 117 | + } elseif ($this->page == $this->lastPageNumber()) { |
|
| 118 | + $next = $this->page; |
|
| 119 | + } |
|
| 120 | + return $next; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param bool $withBaseUrl |
|
| 125 | + * @return string|null |
|
| 126 | + */ |
|
| 127 | + public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 128 | + { |
|
| 129 | + $next = null; |
|
| 130 | + if (!empty($this->nextPageNumber())) { |
|
| 131 | + $next = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->nextPageNumber(); |
|
| 132 | + } |
|
| 133 | + return $next; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param bool $withBaseUrl |
|
| 138 | + * @return string|null |
|
| 139 | + */ |
|
| 140 | + public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 141 | + { |
|
| 142 | + return $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=1'; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return int |
|
| 147 | + */ |
|
| 148 | + public function lastPageNumber(): int |
|
| 149 | + { |
|
| 150 | + return (int)ceil($this->total() / $this->per_page); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param bool $withBaseUrl |
|
| 155 | + * @return string|null |
|
| 156 | + */ |
|
| 157 | + public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 158 | + { |
|
| 159 | + $last = null; |
|
| 160 | + if (!empty($this->lastPageNumber())) { |
|
| 161 | + $last = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->lastPageNumber(); |
|
| 162 | + } |
|
| 163 | + return $last; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return mixed |
|
| 168 | + */ |
|
| 169 | + public function firstItem() |
|
| 170 | + { |
|
| 171 | + return $this->data[array_key_first($this->data)]; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return mixed |
|
| 176 | + */ |
|
| 177 | + public function lastItem() |
|
| 178 | + { |
|
| 179 | + return $this->data[array_key_last($this->data)]; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + public function perPage(): int |
|
| 186 | + { |
|
| 187 | + return $this->per_page; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function total(): int |
|
| 194 | + { |
|
| 195 | + return $this->total; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param bool $withBaseUrl |
|
| 200 | + * @return array |
|
| 201 | + */ |
|
| 202 | + public function links(bool $withBaseUrl = false): array |
|
| 203 | + { |
|
| 204 | + $links = []; |
|
| 205 | + for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 206 | + $links[] = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $i; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $links; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param bool $withBaseUrl |
|
| 214 | + * @param $pageItemsCount |
|
| 215 | + * @return string|null |
|
| 216 | + */ |
|
| 217 | + public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 218 | + { |
|
| 219 | + if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 220 | + $pageItemsCount = 3; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $currentPage = $this->currentPageNumber(); |
|
| 224 | + $totalPages = $this->lastPageNumber(); |
|
| 225 | + |
|
| 226 | + if ($totalPages <= 1) { |
|
| 227 | + return null; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $pagination = '<ul class="pagination">'; |
|
| 231 | + |
|
| 232 | + if ($currentPage > 1) { |
|
| 233 | + $pagination .= '<li><a href="' . $this->previousPageLink() . '">« Previous</a></li>'; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if ($pageItemsCount) { |
|
| 237 | + $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 238 | + $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 239 | + $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 240 | + |
|
| 241 | + if ($startPage > 1) { |
|
| 242 | + $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 243 | + if ($startPage > 2) { |
|
| 244 | + $pagination .= '<li><span>...</span></li>'; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $links = $this->links($withBaseUrl); |
|
| 249 | + for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 250 | + $active = $i == $currentPage ? 'class="active"' : ''; |
|
| 251 | + $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ($endPage < $totalPages) { |
|
| 255 | + if ($endPage < $totalPages - 1) { |
|
| 256 | + $pagination .= '<li><span>...</span></li>'; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if ($currentPage < $totalPages) { |
|
| 264 | + $pagination .= '<li><a href="' . $this->nextPageLink() . '">Next »</a></li>'; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $pagination .= '</ul>'; |
|
| 268 | + |
|
| 269 | + return $pagination; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @return array|SleekDbal[] |
|
| 274 | + */ |
|
| 275 | + public function data(): array |
|
| 276 | + { |
|
| 277 | + $result = array_map(function ($element) { |
|
| 278 | + $item = clone $this->dbal; |
|
| 279 | + $item->setData($element); |
|
| 280 | + $item->setModifiedFields($element); |
|
| 281 | + $item->setIsNew(false); |
|
| 282 | + return $item; |
|
| 283 | + }, $this->data); |
|
| 284 | + |
|
| 285 | + return $result ?? []; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param SleekDbal $sleekDbal |
|
| 290 | + * @return void |
|
| 291 | + * @throws DatabaseException |
|
| 292 | + * @throws ModelException |
|
| 293 | + * @throws IOException |
|
| 294 | + * @throws InvalidArgumentException |
|
| 295 | + * @throws InvalidConfigurationException |
|
| 296 | + */ |
|
| 297 | + private function setTotal(SleekDbal $sleekDbal) |
|
| 298 | + { |
|
| 299 | + $this->total = count($sleekDbal->getBuilder()->getQuery()->fetch()); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @param bool $withBaseUrl |
|
| 304 | + * @return string |
|
| 305 | + */ |
|
| 306 | + private function getUri(bool $withBaseUrl = false): string |
|
| 307 | + { |
|
| 308 | + $base_url = base_url(); |
|
| 309 | + $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 310 | + $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 311 | + $url = $routeUrl; |
|
| 312 | + if ($withBaseUrl) { |
|
| 313 | + $url = $base_url . $routeUrl; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $delimiter = str_contains($url, '?') ? '&' : '?'; |
|
| 317 | + |
|
| 318 | + return $url . $delimiter; |
|
| 319 | + } |
|
| 320 | 320 | } |
| 321 | 321 | \ No newline at end of file |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | */ |
| 148 | 148 | public function lastPageNumber(): int |
| 149 | 149 | { |
| 150 | - return (int)ceil($this->total() / $this->per_page); |
|
| 150 | + return (int) ceil($this->total() / $this->per_page); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | */ |
| 275 | 275 | public function data(): array |
| 276 | 276 | { |
| 277 | - $result = array_map(function ($element) { |
|
| 277 | + $result = array_map(function($element) { |
|
| 278 | 278 | $item = clone $this->dbal; |
| 279 | 279 | $item->setData($element); |
| 280 | 280 | $item->setModifiedFields($element); |
@@ -182,20 +182,20 @@ |
||
| 182 | 182 | self::$connection = $config; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public function setData(array $data) |
|
| 186 | - { |
|
| 187 | - $this->data = $data; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - public function setModifiedFields($modifiedFields) |
|
| 191 | - { |
|
| 192 | - $this->modifiedFields = $modifiedFields; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - public function setIsNew($isNew) |
|
| 196 | - { |
|
| 197 | - $this->isNew = $isNew; |
|
| 198 | - } |
|
| 185 | + public function setData(array $data) |
|
| 186 | + { |
|
| 187 | + $this->data = $data; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + public function setModifiedFields($modifiedFields) |
|
| 191 | + { |
|
| 192 | + $this->modifiedFields = $modifiedFields; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + public function setIsNew($isNew) |
|
| 196 | + { |
|
| 197 | + $this->isNew = $isNew; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * @inheritDoc |
@@ -35,15 +35,15 @@ |
||
| 35 | 35 | return $this->getOrmModel()->find_many(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param int $perPage |
|
| 40 | - * @param int $currentPage |
|
| 41 | - * @return PaginatorInterface |
|
| 42 | - */ |
|
| 43 | - public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 44 | - { |
|
| 45 | - return new Paginator($this, $perPage, $currentPage); |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * @param int $perPage |
|
| 40 | + * @param int $currentPage |
|
| 41 | + * @return PaginatorInterface |
|
| 42 | + */ |
|
| 43 | + public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 44 | + { |
|
| 45 | + return new Paginator($this, $perPage, $currentPage); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * @inheritDoc |
@@ -8,294 +8,294 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Paginator implements PaginatorInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var int |
|
| 13 | - */ |
|
| 14 | - private $total; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var IdiormDbal |
|
| 18 | - */ |
|
| 19 | - private $dbal; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var int |
|
| 23 | - */ |
|
| 24 | - protected $per_page; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var int |
|
| 28 | - */ |
|
| 29 | - protected $page; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var array|IdiormResultSet |
|
| 33 | - */ |
|
| 34 | - public $data; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param $idiormDbal |
|
| 38 | - * @param int $per_page |
|
| 39 | - * @param int $page |
|
| 40 | - * @throws DatabaseException |
|
| 41 | - */ |
|
| 42 | - public function __construct($idiormDbal, int $per_page, int $page = 1) |
|
| 43 | - { |
|
| 44 | - /** @var IdiormDbal $idiormDbal */ |
|
| 45 | - $this->setTotal($idiormDbal); |
|
| 46 | - $this->dbal = $idiormDbal; |
|
| 47 | - $this->dbal->limit($per_page)->offset($per_page * ($page - 1)); |
|
| 48 | - $this->data = $this->dbal->getOrmModel()->find_many(); |
|
| 49 | - $this->per_page = $per_page; |
|
| 50 | - $this->page = $page; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return int |
|
| 55 | - */ |
|
| 56 | - public function currentPageNumber(): int |
|
| 57 | - { |
|
| 58 | - return $this->page; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param bool $withBaseUrl |
|
| 63 | - * @return string|null |
|
| 64 | - */ |
|
| 65 | - public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 66 | - { |
|
| 67 | - $current = null; |
|
| 68 | - if (!empty($this->page)) { |
|
| 69 | - $current = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->page; |
|
| 70 | - } |
|
| 71 | - return $current; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return int|null |
|
| 76 | - */ |
|
| 77 | - public function previousPageNumber(): ?int |
|
| 78 | - { |
|
| 79 | - $previous = null; |
|
| 80 | - if ($this->page > 1) { |
|
| 81 | - $previous = $this->page - 1; |
|
| 82 | - } elseif ($this->page == 1) { |
|
| 83 | - $previous = $this->page; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $previous; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param bool $withBaseUrl |
|
| 91 | - * @return string|null |
|
| 92 | - */ |
|
| 93 | - public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 94 | - { |
|
| 95 | - $previous = null; |
|
| 96 | - if (!empty($this->previousPageNumber())) { |
|
| 97 | - $previous = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->previousPageNumber(); |
|
| 98 | - } |
|
| 99 | - return $previous; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return int|null |
|
| 104 | - */ |
|
| 105 | - public function nextPageNumber(): ?int |
|
| 106 | - { |
|
| 107 | - $next = null; |
|
| 108 | - if ($this->page < $this->lastPageNumber()) { |
|
| 109 | - $next = $this->page + 1; |
|
| 110 | - } elseif ($this->page == $this->lastPageNumber()) { |
|
| 111 | - $next = $this->page; |
|
| 112 | - } |
|
| 113 | - return $next; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param bool $withBaseUrl |
|
| 118 | - * @return string|null |
|
| 119 | - */ |
|
| 120 | - public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 121 | - { |
|
| 122 | - $next = null; |
|
| 123 | - if (!empty($this->nextPageNumber())) { |
|
| 124 | - $next = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->nextPageNumber(); |
|
| 125 | - } |
|
| 126 | - return $next; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param bool $withBaseUrl |
|
| 131 | - * @return string|null |
|
| 132 | - */ |
|
| 133 | - public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 134 | - { |
|
| 135 | - return $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=1'; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return int |
|
| 140 | - */ |
|
| 141 | - public function lastPageNumber(): int |
|
| 142 | - { |
|
| 143 | - return (int)ceil($this->total() / $this->per_page); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param bool $withBaseUrl |
|
| 148 | - * @return string|null |
|
| 149 | - */ |
|
| 150 | - public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 151 | - { |
|
| 152 | - $last = null; |
|
| 153 | - if (!empty($this->lastPageNumber())) { |
|
| 154 | - $last = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->lastPageNumber(); |
|
| 155 | - } |
|
| 156 | - return $last; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return mixed |
|
| 161 | - */ |
|
| 162 | - public function firstItem() |
|
| 163 | - { |
|
| 164 | - return $this->data[array_key_first($this->data)]; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return mixed |
|
| 169 | - */ |
|
| 170 | - public function lastItem() |
|
| 171 | - { |
|
| 172 | - return $this->data[array_key_last($this->data)]; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @return int |
|
| 177 | - */ |
|
| 178 | - public function perPage() |
|
| 179 | - { |
|
| 180 | - return $this->per_page; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @return int |
|
| 185 | - */ |
|
| 186 | - public function total() |
|
| 187 | - { |
|
| 188 | - return $this->total; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @param bool $withBaseUrl |
|
| 193 | - * @return array |
|
| 194 | - */ |
|
| 195 | - public function links(bool $withBaseUrl = false): array |
|
| 196 | - { |
|
| 197 | - $links = []; |
|
| 198 | - for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 199 | - $links[] = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $i; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $links; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param bool $withBaseUrl |
|
| 207 | - * @param $pageItemsCount |
|
| 208 | - * @return string|null |
|
| 209 | - */ |
|
| 210 | - public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 211 | - { |
|
| 212 | - if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 213 | - $pageItemsCount = 3; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $currentPage = $this->currentPageNumber(); |
|
| 217 | - $totalPages = $this->lastPageNumber(); |
|
| 218 | - |
|
| 219 | - if ($totalPages <= 1) { |
|
| 220 | - return null; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $pagination = '<ul class="pagination">'; |
|
| 224 | - |
|
| 225 | - if ($currentPage > 1) { |
|
| 226 | - $pagination .= '<li><a href="' . $this->previousPageLink() . '">« Previous</a></li>'; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - if ($pageItemsCount) { |
|
| 230 | - $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 231 | - $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 232 | - $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 233 | - |
|
| 234 | - if ($startPage > 1) { |
|
| 235 | - $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 236 | - if ($startPage > 2) { |
|
| 237 | - $pagination .= '<li><span>...</span></li>'; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $links = $this->links($withBaseUrl); |
|
| 242 | - for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 243 | - $active = $i == $currentPage ? 'class="active"' : ''; |
|
| 244 | - $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if ($endPage < $totalPages) { |
|
| 248 | - if ($endPage < $totalPages - 1) { |
|
| 249 | - $pagination .= '<li><span>...</span></li>'; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - if ($currentPage < $totalPages) { |
|
| 257 | - $pagination .= '<li><a href="' . $this->nextPageLink() . '">Next »</a></li>'; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $pagination .= '</ul>'; |
|
| 261 | - |
|
| 262 | - return $pagination; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @return array|IdiormResultSet |
|
| 267 | - */ |
|
| 268 | - public function data() |
|
| 269 | - { |
|
| 270 | - return $this->data ?? []; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param IdiormDbal $idiormDbal |
|
| 275 | - * @return void |
|
| 276 | - * @throws DatabaseException |
|
| 277 | - */ |
|
| 278 | - private function setTotal(IdiormDbal $idiormDbal) |
|
| 279 | - { |
|
| 280 | - $this->total = $idiormDbal->getOrmModel()->count(); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @param bool $withBaseUrl |
|
| 285 | - * @return string |
|
| 286 | - */ |
|
| 287 | - private function getUri(bool $withBaseUrl = false) |
|
| 288 | - { |
|
| 289 | - $base_url = base_url(); |
|
| 290 | - $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 291 | - $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 292 | - $url = $routeUrl; |
|
| 293 | - if ($withBaseUrl) { |
|
| 294 | - $url = $base_url . $routeUrl; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $delimiter = str_contains($url, '?') ? '&' : '?'; |
|
| 298 | - |
|
| 299 | - return $url . $delimiter; |
|
| 300 | - } |
|
| 11 | + /** |
|
| 12 | + * @var int |
|
| 13 | + */ |
|
| 14 | + private $total; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var IdiormDbal |
|
| 18 | + */ |
|
| 19 | + private $dbal; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var int |
|
| 23 | + */ |
|
| 24 | + protected $per_page; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var int |
|
| 28 | + */ |
|
| 29 | + protected $page; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var array|IdiormResultSet |
|
| 33 | + */ |
|
| 34 | + public $data; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param $idiormDbal |
|
| 38 | + * @param int $per_page |
|
| 39 | + * @param int $page |
|
| 40 | + * @throws DatabaseException |
|
| 41 | + */ |
|
| 42 | + public function __construct($idiormDbal, int $per_page, int $page = 1) |
|
| 43 | + { |
|
| 44 | + /** @var IdiormDbal $idiormDbal */ |
|
| 45 | + $this->setTotal($idiormDbal); |
|
| 46 | + $this->dbal = $idiormDbal; |
|
| 47 | + $this->dbal->limit($per_page)->offset($per_page * ($page - 1)); |
|
| 48 | + $this->data = $this->dbal->getOrmModel()->find_many(); |
|
| 49 | + $this->per_page = $per_page; |
|
| 50 | + $this->page = $page; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return int |
|
| 55 | + */ |
|
| 56 | + public function currentPageNumber(): int |
|
| 57 | + { |
|
| 58 | + return $this->page; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param bool $withBaseUrl |
|
| 63 | + * @return string|null |
|
| 64 | + */ |
|
| 65 | + public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 66 | + { |
|
| 67 | + $current = null; |
|
| 68 | + if (!empty($this->page)) { |
|
| 69 | + $current = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->page; |
|
| 70 | + } |
|
| 71 | + return $current; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return int|null |
|
| 76 | + */ |
|
| 77 | + public function previousPageNumber(): ?int |
|
| 78 | + { |
|
| 79 | + $previous = null; |
|
| 80 | + if ($this->page > 1) { |
|
| 81 | + $previous = $this->page - 1; |
|
| 82 | + } elseif ($this->page == 1) { |
|
| 83 | + $previous = $this->page; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $previous; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param bool $withBaseUrl |
|
| 91 | + * @return string|null |
|
| 92 | + */ |
|
| 93 | + public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 94 | + { |
|
| 95 | + $previous = null; |
|
| 96 | + if (!empty($this->previousPageNumber())) { |
|
| 97 | + $previous = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->previousPageNumber(); |
|
| 98 | + } |
|
| 99 | + return $previous; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return int|null |
|
| 104 | + */ |
|
| 105 | + public function nextPageNumber(): ?int |
|
| 106 | + { |
|
| 107 | + $next = null; |
|
| 108 | + if ($this->page < $this->lastPageNumber()) { |
|
| 109 | + $next = $this->page + 1; |
|
| 110 | + } elseif ($this->page == $this->lastPageNumber()) { |
|
| 111 | + $next = $this->page; |
|
| 112 | + } |
|
| 113 | + return $next; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param bool $withBaseUrl |
|
| 118 | + * @return string|null |
|
| 119 | + */ |
|
| 120 | + public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 121 | + { |
|
| 122 | + $next = null; |
|
| 123 | + if (!empty($this->nextPageNumber())) { |
|
| 124 | + $next = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->nextPageNumber(); |
|
| 125 | + } |
|
| 126 | + return $next; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param bool $withBaseUrl |
|
| 131 | + * @return string|null |
|
| 132 | + */ |
|
| 133 | + public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 134 | + { |
|
| 135 | + return $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=1'; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return int |
|
| 140 | + */ |
|
| 141 | + public function lastPageNumber(): int |
|
| 142 | + { |
|
| 143 | + return (int)ceil($this->total() / $this->per_page); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param bool $withBaseUrl |
|
| 148 | + * @return string|null |
|
| 149 | + */ |
|
| 150 | + public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 151 | + { |
|
| 152 | + $last = null; |
|
| 153 | + if (!empty($this->lastPageNumber())) { |
|
| 154 | + $last = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $this->lastPageNumber(); |
|
| 155 | + } |
|
| 156 | + return $last; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return mixed |
|
| 161 | + */ |
|
| 162 | + public function firstItem() |
|
| 163 | + { |
|
| 164 | + return $this->data[array_key_first($this->data)]; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return mixed |
|
| 169 | + */ |
|
| 170 | + public function lastItem() |
|
| 171 | + { |
|
| 172 | + return $this->data[array_key_last($this->data)]; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @return int |
|
| 177 | + */ |
|
| 178 | + public function perPage() |
|
| 179 | + { |
|
| 180 | + return $this->per_page; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @return int |
|
| 185 | + */ |
|
| 186 | + public function total() |
|
| 187 | + { |
|
| 188 | + return $this->total; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @param bool $withBaseUrl |
|
| 193 | + * @return array |
|
| 194 | + */ |
|
| 195 | + public function links(bool $withBaseUrl = false): array |
|
| 196 | + { |
|
| 197 | + $links = []; |
|
| 198 | + for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 199 | + $links[] = $this->getUri($withBaseUrl) . 'per_page=' . $this->per_page . '&page=' . $i; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $links; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param bool $withBaseUrl |
|
| 207 | + * @param $pageItemsCount |
|
| 208 | + * @return string|null |
|
| 209 | + */ |
|
| 210 | + public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 211 | + { |
|
| 212 | + if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 213 | + $pageItemsCount = 3; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $currentPage = $this->currentPageNumber(); |
|
| 217 | + $totalPages = $this->lastPageNumber(); |
|
| 218 | + |
|
| 219 | + if ($totalPages <= 1) { |
|
| 220 | + return null; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $pagination = '<ul class="pagination">'; |
|
| 224 | + |
|
| 225 | + if ($currentPage > 1) { |
|
| 226 | + $pagination .= '<li><a href="' . $this->previousPageLink() . '">« Previous</a></li>'; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + if ($pageItemsCount) { |
|
| 230 | + $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 231 | + $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 232 | + $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 233 | + |
|
| 234 | + if ($startPage > 1) { |
|
| 235 | + $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 236 | + if ($startPage > 2) { |
|
| 237 | + $pagination .= '<li><span>...</span></li>'; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $links = $this->links($withBaseUrl); |
|
| 242 | + for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 243 | + $active = $i == $currentPage ? 'class="active"' : ''; |
|
| 244 | + $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if ($endPage < $totalPages) { |
|
| 248 | + if ($endPage < $totalPages - 1) { |
|
| 249 | + $pagination .= '<li><span>...</span></li>'; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + if ($currentPage < $totalPages) { |
|
| 257 | + $pagination .= '<li><a href="' . $this->nextPageLink() . '">Next »</a></li>'; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $pagination .= '</ul>'; |
|
| 261 | + |
|
| 262 | + return $pagination; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @return array|IdiormResultSet |
|
| 267 | + */ |
|
| 268 | + public function data() |
|
| 269 | + { |
|
| 270 | + return $this->data ?? []; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param IdiormDbal $idiormDbal |
|
| 275 | + * @return void |
|
| 276 | + * @throws DatabaseException |
|
| 277 | + */ |
|
| 278 | + private function setTotal(IdiormDbal $idiormDbal) |
|
| 279 | + { |
|
| 280 | + $this->total = $idiormDbal->getOrmModel()->count(); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @param bool $withBaseUrl |
|
| 285 | + * @return string |
|
| 286 | + */ |
|
| 287 | + private function getUri(bool $withBaseUrl = false) |
|
| 288 | + { |
|
| 289 | + $base_url = base_url(); |
|
| 290 | + $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 291 | + $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 292 | + $url = $routeUrl; |
|
| 293 | + if ($withBaseUrl) { |
|
| 294 | + $url = $base_url . $routeUrl; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + $delimiter = str_contains($url, '?') ? '&' : '?'; |
|
| 298 | + |
|
| 299 | + return $url . $delimiter; |
|
| 300 | + } |
|
| 301 | 301 | } |
| 302 | 302 | \ No newline at end of file |
@@ -140,7 +140,7 @@ |
||
| 140 | 140 | */ |
| 141 | 141 | public function lastPageNumber(): int |
| 142 | 142 | { |
| 143 | - return (int)ceil($this->total() / $this->per_page); |
|
| 143 | + return (int) ceil($this->total() / $this->per_page); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |