|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Kevin Bond <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
final class Pager implements \Countable, \IteratorAggregate |
|
9
|
|
|
{ |
|
10
|
|
|
public const DEFAULT_LIMIT = 20; |
|
11
|
|
|
|
|
12
|
|
|
private Result $result; |
|
13
|
|
|
private int $page; |
|
14
|
|
|
private int $limit; |
|
15
|
|
|
private ?Page $cachedPage = null; |
|
16
|
|
|
|
|
17
|
15 |
|
public function __construct(Result $result, int $page = 1, int $limit = self::DEFAULT_LIMIT) |
|
18
|
|
|
{ |
|
19
|
15 |
|
$this->result = $result; |
|
20
|
15 |
|
$this->page = $page < 1 ? 1 : $page; |
|
21
|
15 |
|
$this->limit = $limit < 1 ? self::DEFAULT_LIMIT : $limit; |
|
22
|
15 |
|
} |
|
23
|
|
|
|
|
24
|
14 |
|
public function current(): int |
|
25
|
|
|
{ |
|
26
|
14 |
|
$lastPage = $this->last(); |
|
27
|
|
|
|
|
28
|
14 |
|
if ($this->page > $lastPage) { |
|
29
|
1 |
|
return $lastPage; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
13 |
|
return $this->page; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
14 |
|
public function limit(): int |
|
36
|
|
|
{ |
|
37
|
14 |
|
return $this->limit; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return int the count for the current page |
|
42
|
|
|
*/ |
|
43
|
13 |
|
public function count(): int |
|
44
|
|
|
{ |
|
45
|
13 |
|
return $this->getPage()->count(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
14 |
|
public function totalCount(): int |
|
49
|
|
|
{ |
|
50
|
14 |
|
return $this->result->count(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
13 |
|
public function getIterator(): \Traversable |
|
54
|
|
|
{ |
|
55
|
13 |
|
return $this->getPage()->getIterator(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
public function next(): ?int |
|
59
|
|
|
{ |
|
60
|
6 |
|
$currentPage = $this->current(); |
|
61
|
|
|
|
|
62
|
6 |
|
if ($currentPage === $this->last()) { |
|
63
|
4 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
return ++$currentPage; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
6 |
|
public function previous(): ?int |
|
70
|
|
|
{ |
|
71
|
6 |
|
$page = $this->current(); |
|
72
|
|
|
|
|
73
|
6 |
|
if (1 === $page) { |
|
74
|
3 |
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return --$page; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
6 |
|
public function first(): int |
|
81
|
|
|
{ |
|
82
|
6 |
|
return 1; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
14 |
|
public function last(): int |
|
86
|
|
|
{ |
|
87
|
14 |
|
$totalCount = $this->totalCount(); |
|
88
|
|
|
|
|
89
|
14 |
|
if (0 === $totalCount) { |
|
90
|
2 |
|
return 1; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
return \ceil($totalCount / $this->limit()); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
public function pageCount(): int |
|
97
|
|
|
{ |
|
98
|
5 |
|
return $this->last(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
5 |
|
public function haveToPaginate(): bool |
|
102
|
|
|
{ |
|
103
|
5 |
|
return $this->pageCount() > 1; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
13 |
|
private function getPage(): Page |
|
107
|
|
|
{ |
|
108
|
13 |
|
if ($this->cachedPage) { |
|
109
|
13 |
|
return $this->cachedPage; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
13 |
|
$offset = $this->current() * $this->limit() - $this->limit(); |
|
113
|
|
|
|
|
114
|
13 |
|
return $this->cachedPage = $this->result->take($offset, $this->limit()); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|