1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_pager; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class BasicPager implements Interfaces\IPager |
7
|
|
|
{ |
8
|
|
|
protected int $maxResults = 0; |
9
|
|
|
protected int $actualPage = 0; |
10
|
|
|
protected int $limitPerPage = 0; |
11
|
|
|
|
12
|
3 |
|
public function setMaxResults(int $maxResults): Interfaces\IPager |
13
|
|
|
{ |
14
|
3 |
|
$this->maxResults = $maxResults; |
15
|
3 |
|
return $this; |
16
|
|
|
} |
17
|
|
|
|
18
|
2 |
|
public function getMaxResults(): int |
19
|
|
|
{ |
20
|
2 |
|
return $this->maxResults; |
21
|
|
|
} |
22
|
|
|
|
23
|
3 |
|
public function setActualPage(int $page): Interfaces\IPager |
24
|
|
|
{ |
25
|
3 |
|
$this->actualPage = $page; |
26
|
3 |
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function getActualPage(): int |
30
|
|
|
{ |
31
|
2 |
|
return $this->actualPage; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function setLimit(int $limit): Interfaces\IPager |
35
|
|
|
{ |
36
|
3 |
|
$this->limitPerPage = $limit; |
37
|
3 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
public function getLimit(): int |
41
|
|
|
{ |
42
|
2 |
|
return $this->limitPerPage; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function getOffset(): int |
46
|
|
|
{ |
47
|
3 |
|
$page = intval($this->actualPage - 1); |
48
|
3 |
|
if ($this->pageExists($page)) { |
49
|
2 |
|
return intval($page * $this->limitPerPage); |
50
|
|
|
} else { |
51
|
2 |
|
return 0; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
public function getPagesCount(): int |
56
|
|
|
{ |
57
|
3 |
|
if (0 >= $this->maxResults) { |
58
|
1 |
|
return 1; |
59
|
|
|
} |
60
|
2 |
|
$lastPageItems = $this->maxResults % $this->limitPerPage; |
61
|
2 |
|
$page = intval(floor($this->maxResults / $this->limitPerPage)); |
62
|
2 |
|
return (0 < $lastPageItems) ? $page + 1 : $page ; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function pageExists(int $i): bool |
66
|
|
|
{ |
67
|
3 |
|
return (0 < $i) && ($i <= $this->getPagesCount()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|