blob: 0f61fe232fe1159ce2e882a3bb0bb45e6301545e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Adldap\Query;
4
5use Countable;
6use ArrayIterator;
7use IteratorAggregate;
8
9class Paginator implements Countable, IteratorAggregate
10{
11 /**
12 * The complete results array.
13 *
14 * @var array
15 */
16 protected $results = [];
17
18 /**
19 * The total amount of pages.
20 *
21 * @var int
22 */
23 protected $pages;
24
25 /**
26 * The amount of entries per page.
27 *
28 * @var int
29 */
30 protected $perPage;
31
32 /**
33 * The current page number.
34 *
35 * @var int
36 */
37 protected $currentPage;
38
39 /**
40 * The current entry offset number.
41 *
42 * @var int
43 */
44 protected $currentOffset;
45
46 /**
47 * Constructor.
48 *
49 * @param array $results
50 * @param int $perPage
51 * @param int $currentPage
52 * @param int $pages
53 */
54 public function __construct(array $results = [], $perPage = 50, $currentPage = 0, $pages = 0)
55 {
56 $this->setResults($results)
57 ->setPerPage($perPage)
58 ->setCurrentPage($currentPage)
59 ->setPages($pages)
60 ->setCurrentOffset(($this->getCurrentPage() * $this->getPerPage()));
61 }
62
63 /**
64 * Get an iterator for the entries.
65 *
66 * @return ArrayIterator
67 */
68 public function getIterator()
69 {
70 $entries = array_slice($this->getResults(), $this->getCurrentOffset(), $this->getPerPage(), true);
71
72 return new ArrayIterator($entries);
73 }
74
75 /**
76 * Returns the complete results array.
77 *
78 * @return array
79 */
80 public function getResults()
81 {
82 return $this->results;
83 }
84
85 /**
86 * Returns the total amount of pages
87 * in a paginated result.
88 *
89 * @return int
90 */
91 public function getPages()
92 {
93 return $this->pages;
94 }
95
96 /**
97 * Returns the total amount of entries
98 * allowed per page.
99 *
100 * @return int
101 */
102 public function getPerPage()
103 {
104 return $this->perPage;
105 }
106
107 /**
108 * Returns the current page number.
109 *
110 * @return int
111 */
112 public function getCurrentPage()
113 {
114 return $this->currentPage;
115 }
116
117 /**
118 * Returns the current offset number.
119 *
120 * @return int
121 */
122 public function getCurrentOffset()
123 {
124 return $this->currentOffset;
125 }
126
127 /**
128 * Returns the total amount of results.
129 *
130 * @return int
131 */
132 public function count()
133 {
134 return count($this->results);
135 }
136
137 /**
138 * Sets the results array property.
139 *
140 * @param array $results
141 *
142 * @return Paginator
143 */
144 protected function setResults(array $results)
145 {
146 $this->results = $results;
147
148 return $this;
149 }
150
151 /**
152 * Sets the total number of pages.
153 *
154 * @param int $pages
155 *
156 * @return Paginator
157 */
158 protected function setPages($pages = 0)
159 {
160 $this->pages = (int) $pages;
161
162 return $this;
163 }
164
165 /**
166 * Sets the number of entries per page.
167 *
168 * @param int $perPage
169 *
170 * @return Paginator
171 */
172 protected function setPerPage($perPage = 50)
173 {
174 $this->perPage = (int) $perPage;
175
176 return $this;
177 }
178
179 /**
180 * Sets the current page number.
181 *
182 * @param int $currentPage
183 *
184 * @return Paginator
185 */
186 protected function setCurrentPage($currentPage = 0)
187 {
188 $this->currentPage = (int) $currentPage;
189
190 return $this;
191 }
192
193 /**
194 * Sets the current offset number.
195 *
196 * @param int $offset
197 *
198 * @return Paginator
199 */
200 protected function setCurrentOffset($offset = 0)
201 {
202 $this->currentOffset = (int) $offset;
203
204 return $this;
205 }
206}