|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Kevin Bond <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
abstract class Pager implements \Countable, \IteratorAggregate, \JsonSerializable |
|
9
|
|
|
{ |
|
10
|
25 |
|
final public function getNextPage(): ?int |
|
11
|
|
|
{ |
|
12
|
25 |
|
$currentPage = $this->getCurrentPage(); |
|
13
|
|
|
|
|
14
|
25 |
|
if ($currentPage === $this->getLastPage()) { |
|
15
|
13 |
|
return null; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
12 |
|
return ++$currentPage; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
25 |
|
final public function getPreviousPage(): ?int |
|
22
|
|
|
{ |
|
23
|
25 |
|
$page = $this->getCurrentPage(); |
|
24
|
|
|
|
|
25
|
25 |
|
if (1 === $page) { |
|
26
|
12 |
|
return null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
13 |
|
return --$page; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
25 |
|
final public function getFirstPage(): int |
|
33
|
|
|
{ |
|
34
|
25 |
|
return 1; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
26 |
|
final public function getLastPage(): int |
|
38
|
|
|
{ |
|
39
|
26 |
|
$totalCount = $this->totalCount(); |
|
40
|
|
|
|
|
41
|
26 |
|
if (0 === $totalCount) { |
|
42
|
5 |
|
return 1; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
21 |
|
return (int) \ceil($totalCount / $this->getLimit()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
16 |
|
final public function pagesCount(): int |
|
49
|
|
|
{ |
|
50
|
16 |
|
return $this->getLastPage(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function jsonSerialize(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
4 |
|
'items' => \iterator_to_array($this), |
|
57
|
4 |
|
'count' => $this->count(), |
|
58
|
4 |
|
'total' => $this->totalCount(), |
|
59
|
4 |
|
'limit' => $this->getLimit(), |
|
60
|
4 |
|
'pages' => $this->pagesCount(), |
|
61
|
4 |
|
'first' => $this->getFirstPage(), |
|
62
|
4 |
|
'previous' => $this->getPreviousPage(), |
|
63
|
4 |
|
'current' => $this->getCurrentPage(), |
|
64
|
4 |
|
'next' => $this->getNextPage(), |
|
65
|
4 |
|
'last' => $this->getLastPage(), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
abstract public function getCurrentPage(): int; |
|
70
|
|
|
|
|
71
|
|
|
abstract public function getLimit(): int; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The result count for the current page. |
|
75
|
|
|
*/ |
|
76
|
|
|
abstract public function count(): int; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The total result count. |
|
80
|
|
|
*/ |
|
81
|
|
|
abstract public function totalCount(): int; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return an iterator over selected windows of results of the paginatable. |
|
85
|
|
|
*/ |
|
86
|
|
|
abstract public function getIterator(): \Iterator; |
|
87
|
|
|
} |
|
88
|
|
|
|