PluginProbe
Disk Usage Insights / trunk
Disk Usage Insights vtrunk
trunk 1.0 1.10 1.11 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
disk-usage-insights / src / Frontend / Pagination.php

Pagination.php in Disk Usage Insights trunk, at src/Frontend/Pagination.php

77 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Mgleis\DiskUsageInsights\Frontend;
3
4 class Pagination {
5
6 private string $urlPattern;
7 private int $totalItemCount;
8 private int $itemsPerPage;
9 private int $page;
10
11 public function __construct(string $urlPattern, $totalItemCount = 0, $page = 0, $itemsPerPage = 10) {
12 $this->urlPattern = $urlPattern;
13 $this->totalItemCount = $totalItemCount;
14 $this->itemsPerPage = $itemsPerPage;
15 $this->page = $page;
16 }
17
18 public static function parseFromString(string $url): Pagination {
19 $result = [];
20 list($path, $query) = explode('?', $url);
21
22 parse_str($query, $result);
23
24 $page = $result['p'] ?? 0;
25 $itemsPerPage = $result['ipc'] ?? 10;
26 $totalItemCount = $result['tic'] ?? 10;
27
28 unset($result['p']);
29 unset($result['ipc']);
30 unset($result['tic']);
31
32 $urlPattern = $path . '?1=1';
33 foreach ($result as $key => $value) {
34 $urlPattern .= sprintf('&%s=%s', $key, urlencode($value));
35 }
36 return new Pagination($urlPattern, $totalItemCount, $page, $itemsPerPage);
37 }
38
39 public function hasNextPage(): bool {
40 return $this->page+1 < $this->calcTotalPages();
41 }
42
43 public function hasPreviousPage(): bool {
44 return $this->page != 0;
45 }
46
47 private function buildPageUrl(int $page): string {
48 return sprintf('%s%sp=%s&ipp=%s&tic=%s',
49 $this->urlPattern,
50 str_contains($this->urlPattern, '?') ? '&' : '?',
51 $page,
52 $this->itemsPerPage,
53 $this->totalItemCount
54 );
55 }
56
57 public function buildNextPageUrl(): string {
58 return $this->buildPageUrl(min($this->page + 1, $this->calcTotalPages()));
59 }
60
61 public function buildPreviousPageUrl(): string {
62 return $this->buildPageUrl(max(0, $this->page - 1));
63 }
64
65 public function calcTotalPages(): int {
66 return ceil($this->totalItemCount / $this->itemsPerPage);
67 }
68 public function calcOffset(): int {
69 return $this->page * $this->itemsPerPage;
70 }
71
72 public function getPage(): int { return $this->page; }
73 public function getTotalItemCount(): int { return $this->totalItemCount; }
74 public function getItemsPerPage(): int { return $this->itemsPerPage; }
75
76 }
77