1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Filter\Filter; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
class PaginationFilter implements FilterInterface |
9
|
|
|
{ |
10
|
|
|
/** @var int $page */ |
11
|
|
|
private $page; |
12
|
|
|
|
13
|
|
|
/** @var int $numPerPage */ |
14
|
|
|
private $numPerPage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ArrayIterator $collection |
18
|
|
|
* @return ArrayIterator |
19
|
|
|
*/ |
20
|
2 |
|
public function filter(ArrayIterator $collection): ArrayIterator |
21
|
|
|
{ |
22
|
|
|
// If pagination wasnt set, dont use it! |
23
|
2 |
|
if (!$this->page || !$this->numPerPage) { |
24
|
1 |
|
return $collection; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
$totalRecords = $collection->count(); |
28
|
1 |
|
$resultsOffset = ($this->page * $this->numPerPage) - $this->numPerPage; |
29
|
1 |
|
$resultsEndOffset = $resultsOffset + $this->numPerPage; |
30
|
|
|
|
31
|
1 |
|
if ($resultsOffset > $totalRecords) { |
32
|
1 |
|
throw new LogicException('There aren\'t that many pages for this result set.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
$results = $this->getResults($collection, $totalRecords, $resultsOffset, $resultsEndOffset); |
36
|
|
|
|
37
|
1 |
|
return $results; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
private function getResults(ArrayIterator $collection, int $totalRecords, int $resultsOffset, int $resultsEndOffset) |
41
|
|
|
{ |
42
|
1 |
|
$results = new ArrayIterator(); |
43
|
|
|
|
44
|
1 |
|
$collection->rewind(); |
45
|
|
|
|
46
|
1 |
|
for ($x = 0; $x < $totalRecords; $x ++) { |
47
|
1 |
|
$this->handleRow($x, $resultsOffset, $resultsEndOffset, $collection, $results); |
48
|
|
|
} |
49
|
1 |
|
return $results; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
private function handleRow(int $x, int $resultsOffset, $resultsEndOffset, ArrayIterator $collection, ArrayIterator $results) |
53
|
|
|
{ |
54
|
1 |
|
if ($collection->valid()) { |
55
|
1 |
|
$row = $collection->current(); |
56
|
1 |
|
if ($x >= $resultsOffset && $x < $resultsEndOffset) { |
57
|
1 |
|
$results->append($row); |
58
|
|
|
} |
59
|
1 |
|
$collection->next(); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
1 |
|
public function getPage(): int |
68
|
|
|
{ |
69
|
1 |
|
return $this->page; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param int $page |
74
|
|
|
* @return PaginationFilter |
75
|
|
|
*/ |
76
|
1 |
|
public function setPage(int $page): PaginationFilter |
77
|
|
|
{ |
78
|
1 |
|
$this->page = $page; |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
1 |
|
public function getNumPerPage(): int |
86
|
|
|
{ |
87
|
1 |
|
return $this->numPerPage; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $numPerPage |
92
|
|
|
* @return PaginationFilter |
93
|
|
|
*/ |
94
|
1 |
|
public function setNumPerPage(int $numPerPage): PaginationFilter |
95
|
|
|
{ |
96
|
1 |
|
$this->numPerPage = $numPerPage; |
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |