1 | <?php |
||
14 | final class Iterator implements \Iterator, \Countable, \ArrayAccess |
||
15 | { |
||
16 | /** |
||
17 | * @var Query |
||
18 | */ |
||
19 | private $query; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $start; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $pointer; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $limit; |
||
35 | |||
36 | /** |
||
37 | * @var int|bool |
||
38 | */ |
||
39 | private $loadedStart; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $models; |
||
45 | |||
46 | /** |
||
47 | * @var int|bool |
||
48 | */ |
||
49 | private $count; |
||
50 | |||
51 | public function __construct(Query $query) |
||
70 | |||
71 | public function getQuery(): Query |
||
75 | |||
76 | ////////////////////////// |
||
77 | // Iterator Interface |
||
78 | ////////////////////////// |
||
79 | |||
80 | /** |
||
81 | * Rewind the Iterator to the first element. |
||
82 | */ |
||
83 | public function rewind() |
||
90 | |||
91 | /** |
||
92 | * Returns the current element. |
||
93 | * |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function current() |
||
111 | |||
112 | /** |
||
113 | * Return the key of the current element. |
||
114 | * |
||
115 | * @return int |
||
116 | */ |
||
117 | public function key() |
||
121 | |||
122 | /** |
||
123 | * Move forward to the next element. |
||
124 | */ |
||
125 | public function next() |
||
129 | |||
130 | /** |
||
131 | * Checks if current position is valid. |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function valid() |
||
139 | |||
140 | ////////////////////////// |
||
141 | // Countable Interface |
||
142 | ////////////////////////// |
||
143 | |||
144 | /** |
||
145 | * Get total number of models matching query. |
||
146 | * |
||
147 | * @return int |
||
148 | */ |
||
149 | public function count() |
||
155 | |||
156 | ////////////////////////// |
||
157 | // ArrayAccess Interface |
||
158 | ////////////////////////// |
||
159 | |||
160 | public function offsetExists($offset) |
||
164 | |||
165 | public function offsetGet($offset) |
||
175 | |||
176 | public function offsetSet($offset, $value) |
||
181 | |||
182 | public function offsetUnset($offset) |
||
187 | |||
188 | ////////////////////////// |
||
189 | // Private Methods |
||
190 | ////////////////////////// |
||
191 | |||
192 | /** |
||
193 | * Load the next round of models. |
||
194 | */ |
||
195 | private function loadModels() |
||
205 | |||
206 | /** |
||
207 | * Updates the total count of models. For better performance |
||
208 | * the count is only updated on edges, which is when new models |
||
209 | * need to be loaded. |
||
210 | */ |
||
211 | private function updateCount() |
||
236 | |||
237 | /** |
||
238 | * Generates the starting page given a pointer and limit. |
||
239 | */ |
||
240 | private function rangeStart(int $pointer, int $limit) |
||
244 | |||
245 | /** |
||
246 | * Cast Iterator to array. |
||
247 | */ |
||
248 | public function toArray(): array |
||
252 | } |
||
253 |