1 | <?php |
||
9 | class BaseCollection implements \Iterator, \ArrayAccess, \Countable { |
||
10 | |||
11 | /** |
||
12 | * @var int |
||
13 | */ |
||
14 | protected $position = 0; |
||
15 | |||
16 | /** |
||
17 | * Array of objects |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $items = array(); |
||
22 | |||
23 | /** |
||
24 | * @param array $items |
||
25 | */ |
||
26 | public function __construct(array $items = array()) { |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | */ |
||
37 | public function __clone() { |
||
44 | |||
45 | /** |
||
46 | * Return number of items in this collection |
||
47 | * |
||
48 | * @return int |
||
49 | */ |
||
50 | public function count() { |
||
53 | |||
54 | /** |
||
55 | * Add one item to begin of collection |
||
56 | * This item is accessible via `$collection->getFirst();` |
||
57 | * |
||
58 | * @param $item |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function prepend($item) { |
||
65 | |||
66 | /** |
||
67 | * Add one item to the end of collection |
||
68 | * This item is accessible via `$collection->getLast();` |
||
69 | * |
||
70 | * @param $item |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function append($item) { |
||
77 | |||
78 | /** |
||
79 | * @param int $index |
||
80 | * @param array $items |
||
81 | * @return $this |
||
82 | * @throws \InvalidArgumentException |
||
83 | */ |
||
84 | public function addAfter($index, $items) { |
||
97 | |||
98 | /** |
||
99 | * Truncate current list of items and add new |
||
100 | * |
||
101 | * @param array $items |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setItems($items) { |
||
114 | |||
115 | /** |
||
116 | * Remove part of items from collection |
||
117 | * Works as array_slice |
||
118 | * |
||
119 | * |
||
120 | * @param $offset |
||
121 | * @param null $length |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function slice($offset, $length = null) { |
||
128 | |||
129 | /** |
||
130 | * Take part of items and return new collection |
||
131 | * Works as array_slice |
||
132 | * At this point items in 2 collection is same |
||
133 | * |
||
134 | * @param int $offset |
||
135 | * @param null $length |
||
136 | * @return self |
||
137 | */ |
||
138 | public function extractItems($offset, $length = null) { |
||
146 | |||
147 | /** |
||
148 | * Rewind current collection |
||
149 | */ |
||
150 | public function rewind() { |
||
154 | |||
155 | /** |
||
156 | * Return last item from collection |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function getLast() { |
||
163 | |||
164 | /** |
||
165 | * Return first item from collection |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getFirst() { |
||
171 | |||
172 | /** |
||
173 | * Return next item from current |
||
174 | * Also can return item with position from current + $step |
||
175 | * |
||
176 | * @param int $step |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function getNext($step = 1) { |
||
183 | |||
184 | /** |
||
185 | * Return previous item |
||
186 | * Also can return previous from current position + $step |
||
187 | * |
||
188 | * @param int $step |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function getPrevious($step = 1) { |
||
195 | |||
196 | /** |
||
197 | * Return current item in collection |
||
198 | * |
||
199 | * @return object |
||
200 | */ |
||
201 | public function current() { |
||
204 | |||
205 | /** |
||
206 | * Return current position |
||
207 | * |
||
208 | * @return int |
||
209 | */ |
||
210 | public function key() { |
||
213 | |||
214 | /** |
||
215 | * Switch to next position |
||
216 | */ |
||
217 | public function next() { |
||
220 | |||
221 | /** |
||
222 | * Check if item exist in current position |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function valid() { |
||
229 | |||
230 | /** |
||
231 | * Add item to the end or modify item with given key |
||
232 | * |
||
233 | * @param int|null $offset |
||
234 | * @param object $item |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function offsetSet($offset, $item) { |
||
248 | |||
249 | /** |
||
250 | * Check if item with given offset exists |
||
251 | * |
||
252 | * @param mixed $offset |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function offsetExists($offset) { |
||
258 | |||
259 | /** |
||
260 | * Remove item from collection |
||
261 | * |
||
262 | * @param int $offset |
||
263 | */ |
||
264 | public function offsetUnset($offset) { |
||
267 | |||
268 | /** |
||
269 | * Get item from collection |
||
270 | * |
||
271 | * @param int $offset |
||
272 | * @return object |
||
273 | */ |
||
274 | public function offsetGet($offset) { |
||
277 | |||
278 | /** |
||
279 | * Return array of items connected to this collection |
||
280 | * |
||
281 | * Rewrite this method in you class |
||
282 | * |
||
283 | * <code> |
||
284 | * foreach($collection->getItems() as $item){ |
||
285 | * echo get_class($item)."\n; |
||
286 | * } |
||
287 | * </code> |
||
288 | * @return object[] |
||
289 | */ |
||
290 | public function getItems() { |
||
293 | |||
294 | /** |
||
295 | * Iterate over objects in collection |
||
296 | * |
||
297 | * <code> |
||
298 | * $collection->map(function($item, $index, $collection){ |
||
299 | * if ( $index > 0 ) { |
||
300 | * $item->remove(); |
||
301 | * } |
||
302 | * }) |
||
303 | * </code> |
||
304 | * |
||
305 | * @param callback $callback |
||
306 | * @return $this |
||
307 | * @throws \InvalidArgumentException |
||
308 | */ |
||
309 | public function map($callback) { |
||
323 | |||
324 | /** |
||
325 | * @param int $index |
||
326 | */ |
||
327 | protected function validateIndex($index) { |
||
332 | |||
333 | } |