1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Fabrica. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Salomé <[email protected]> |
7
|
|
|
* (c) Julien DIDIER <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the GPL license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Fabrica\Component\Pagination; |
14
|
|
|
|
15
|
|
|
class Pager implements \IteratorAggregate, \Countable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var PagerAdapterInteface |
19
|
|
|
*/ |
20
|
|
|
private $adapter; |
21
|
|
|
|
22
|
|
|
private $offset = 0; |
23
|
|
|
private $perPage; |
24
|
|
|
private $total; |
25
|
|
|
|
26
|
|
|
public function __construct(PagerAdapterInterface $adapter, $perPage = 10) |
27
|
|
|
{ |
28
|
|
|
$this->adapter = $adapter; |
|
|
|
|
29
|
|
|
$this->perPage = $perPage; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function setOffset($offset) |
33
|
|
|
{ |
34
|
|
|
$this->offset = $offset; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setPage($page) |
38
|
|
|
{ |
39
|
|
|
$this->offset = (max(1, (int) $page) - 1) * $this->perPage; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function isFirstPage() |
45
|
|
|
{ |
46
|
|
|
return $this->getPage() == 1; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isLastPage() |
50
|
|
|
{ |
51
|
|
|
return $this->getPage() == $this->getPageCount(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getPage() |
55
|
|
|
{ |
56
|
|
|
return floor($this->offset/$this->perPage) + 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getOffset() |
60
|
|
|
{ |
61
|
|
|
return $this->offset; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setPerPage($perPage) |
65
|
|
|
{ |
66
|
|
|
$this->perPage = (int) $perPage; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function count() |
70
|
|
|
{ |
71
|
|
|
if (null === $this->total) { |
72
|
|
|
$this->total = $this->adapter->count(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->total; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Can be zero. |
80
|
|
|
*/ |
81
|
|
|
public function getPageCount() |
82
|
|
|
{ |
83
|
|
|
return ceil($this->count() / $this->perPage); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getResults() |
87
|
|
|
{ |
88
|
|
|
return $this->getIterator(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getIterator() |
92
|
|
|
{ |
93
|
|
|
return new \ArrayIterator($this->adapter->get($this->offset, $this->perPage)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..