1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\Common\Collections; |
---|
21 | |
---|
22 | use Closure, ArrayIterator; |
---|
23 | |
---|
24 | /** |
---|
25 | * An ArrayCollection is a Collection implementation that wraps a regular PHP array. |
---|
26 | * |
---|
27 | * @since 2.0 |
---|
28 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
29 | * @author Jonathan Wage <jonwage@gmail.com> |
---|
30 | * @author Roman Borschel <roman@code-factory.org> |
---|
31 | */ |
---|
32 | class ArrayCollection implements Collection |
---|
33 | { |
---|
34 | /** |
---|
35 | * An array containing the entries of this collection. |
---|
36 | * |
---|
37 | * @var array |
---|
38 | */ |
---|
39 | private $_elements; |
---|
40 | |
---|
41 | /** |
---|
42 | * Initializes a new ArrayCollection. |
---|
43 | * |
---|
44 | * @param array $elements |
---|
45 | */ |
---|
46 | public function __construct(array $elements = array()) |
---|
47 | { |
---|
48 | $this->_elements = $elements; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Gets the PHP array representation of this collection. |
---|
53 | * |
---|
54 | * @return array The PHP array representation of this collection. |
---|
55 | */ |
---|
56 | public function toArray() |
---|
57 | { |
---|
58 | return $this->_elements; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Sets the internal iterator to the first element in the collection and |
---|
63 | * returns this element. |
---|
64 | * |
---|
65 | * @return mixed |
---|
66 | */ |
---|
67 | public function first() |
---|
68 | { |
---|
69 | return reset($this->_elements); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Sets the internal iterator to the last element in the collection and |
---|
74 | * returns this element. |
---|
75 | * |
---|
76 | * @return mixed |
---|
77 | */ |
---|
78 | public function last() |
---|
79 | { |
---|
80 | return end($this->_elements); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Gets the current key/index at the current internal iterator position. |
---|
85 | * |
---|
86 | * @return mixed |
---|
87 | */ |
---|
88 | public function key() |
---|
89 | { |
---|
90 | return key($this->_elements); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Moves the internal iterator position to the next element. |
---|
95 | * |
---|
96 | * @return mixed |
---|
97 | */ |
---|
98 | public function next() |
---|
99 | { |
---|
100 | return next($this->_elements); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Gets the element of the collection at the current internal iterator position. |
---|
105 | * |
---|
106 | * @return mixed |
---|
107 | */ |
---|
108 | public function current() |
---|
109 | { |
---|
110 | return current($this->_elements); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Removes an element with a specific key/index from the collection. |
---|
115 | * |
---|
116 | * @param mixed $key |
---|
117 | * @return mixed The removed element or NULL, if no element exists for the given key. |
---|
118 | */ |
---|
119 | public function remove($key) |
---|
120 | { |
---|
121 | if (isset($this->_elements[$key])) { |
---|
122 | $removed = $this->_elements[$key]; |
---|
123 | unset($this->_elements[$key]); |
---|
124 | |
---|
125 | return $removed; |
---|
126 | } |
---|
127 | |
---|
128 | return null; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Removes the specified element from the collection, if it is found. |
---|
133 | * |
---|
134 | * @param mixed $element The element to remove. |
---|
135 | * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. |
---|
136 | */ |
---|
137 | public function removeElement($element) |
---|
138 | { |
---|
139 | $key = array_search($element, $this->_elements, true); |
---|
140 | |
---|
141 | if ($key !== false) { |
---|
142 | unset($this->_elements[$key]); |
---|
143 | |
---|
144 | return true; |
---|
145 | } |
---|
146 | |
---|
147 | return false; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * ArrayAccess implementation of offsetExists() |
---|
152 | * |
---|
153 | * @see containsKey() |
---|
154 | */ |
---|
155 | public function offsetExists($offset) |
---|
156 | { |
---|
157 | return $this->containsKey($offset); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * ArrayAccess implementation of offsetGet() |
---|
162 | * |
---|
163 | * @see get() |
---|
164 | */ |
---|
165 | public function offsetGet($offset) |
---|
166 | { |
---|
167 | return $this->get($offset); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * ArrayAccess implementation of offsetGet() |
---|
172 | * |
---|
173 | * @see add() |
---|
174 | * @see set() |
---|
175 | */ |
---|
176 | public function offsetSet($offset, $value) |
---|
177 | { |
---|
178 | if ( ! isset($offset)) { |
---|
179 | return $this->add($value); |
---|
180 | } |
---|
181 | return $this->set($offset, $value); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * ArrayAccess implementation of offsetUnset() |
---|
186 | * |
---|
187 | * @see remove() |
---|
188 | */ |
---|
189 | public function offsetUnset($offset) |
---|
190 | { |
---|
191 | return $this->remove($offset); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Checks whether the collection contains a specific key/index. |
---|
196 | * |
---|
197 | * @param mixed $key The key to check for. |
---|
198 | * @return boolean TRUE if the given key/index exists, FALSE otherwise. |
---|
199 | */ |
---|
200 | public function containsKey($key) |
---|
201 | { |
---|
202 | return isset($this->_elements[$key]); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Checks whether the given element is contained in the collection. |
---|
207 | * Only element values are compared, not keys. The comparison of two elements |
---|
208 | * is strict, that means not only the value but also the type must match. |
---|
209 | * For objects this means reference equality. |
---|
210 | * |
---|
211 | * @param mixed $element |
---|
212 | * @return boolean TRUE if the given element is contained in the collection, |
---|
213 | * FALSE otherwise. |
---|
214 | */ |
---|
215 | public function contains($element) |
---|
216 | { |
---|
217 | foreach ($this->_elements as $collectionElement) { |
---|
218 | if ($element === $collectionElement) { |
---|
219 | return true; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | return false; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Tests for the existance of an element that satisfies the given predicate. |
---|
228 | * |
---|
229 | * @param Closure $p The predicate. |
---|
230 | * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise. |
---|
231 | */ |
---|
232 | public function exists(Closure $p) |
---|
233 | { |
---|
234 | foreach ($this->_elements as $key => $element) { |
---|
235 | if ($p($key, $element)) { |
---|
236 | return true; |
---|
237 | } |
---|
238 | } |
---|
239 | return false; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Searches for a given element and, if found, returns the corresponding key/index |
---|
244 | * of that element. The comparison of two elements is strict, that means not |
---|
245 | * only the value but also the type must match. |
---|
246 | * For objects this means reference equality. |
---|
247 | * |
---|
248 | * @param mixed $element The element to search for. |
---|
249 | * @return mixed The key/index of the element or FALSE if the element was not found. |
---|
250 | */ |
---|
251 | public function indexOf($element) |
---|
252 | { |
---|
253 | return array_search($element, $this->_elements, true); |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * Gets the element with the given key/index. |
---|
258 | * |
---|
259 | * @param mixed $key The key. |
---|
260 | * @return mixed The element or NULL, if no element exists for the given key. |
---|
261 | */ |
---|
262 | public function get($key) |
---|
263 | { |
---|
264 | if (isset($this->_elements[$key])) { |
---|
265 | return $this->_elements[$key]; |
---|
266 | } |
---|
267 | return null; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Gets all keys/indexes of the collection elements. |
---|
272 | * |
---|
273 | * @return array |
---|
274 | */ |
---|
275 | public function getKeys() |
---|
276 | { |
---|
277 | return array_keys($this->_elements); |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Gets all elements. |
---|
282 | * |
---|
283 | * @return array |
---|
284 | */ |
---|
285 | public function getValues() |
---|
286 | { |
---|
287 | return array_values($this->_elements); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Returns the number of elements in the collection. |
---|
292 | * |
---|
293 | * Implementation of the Countable interface. |
---|
294 | * |
---|
295 | * @return integer The number of elements in the collection. |
---|
296 | */ |
---|
297 | public function count() |
---|
298 | { |
---|
299 | return count($this->_elements); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Adds/sets an element in the collection at the index / with the specified key. |
---|
304 | * |
---|
305 | * When the collection is a Map this is like put(key,value)/add(key,value). |
---|
306 | * When the collection is a List this is like add(position,value). |
---|
307 | * |
---|
308 | * @param mixed $key |
---|
309 | * @param mixed $value |
---|
310 | */ |
---|
311 | public function set($key, $value) |
---|
312 | { |
---|
313 | $this->_elements[$key] = $value; |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * Adds an element to the collection. |
---|
318 | * |
---|
319 | * @param mixed $value |
---|
320 | * @return boolean Always TRUE. |
---|
321 | */ |
---|
322 | public function add($value) |
---|
323 | { |
---|
324 | $this->_elements[] = $value; |
---|
325 | return true; |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * Checks whether the collection is empty. |
---|
330 | * |
---|
331 | * Note: This is preferrable over count() == 0. |
---|
332 | * |
---|
333 | * @return boolean TRUE if the collection is empty, FALSE otherwise. |
---|
334 | */ |
---|
335 | public function isEmpty() |
---|
336 | { |
---|
337 | return ! $this->_elements; |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * Gets an iterator for iterating over the elements in the collection. |
---|
342 | * |
---|
343 | * @return ArrayIterator |
---|
344 | */ |
---|
345 | public function getIterator() |
---|
346 | { |
---|
347 | return new ArrayIterator($this->_elements); |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * Applies the given function to each element in the collection and returns |
---|
352 | * a new collection with the elements returned by the function. |
---|
353 | * |
---|
354 | * @param Closure $func |
---|
355 | * @return Collection |
---|
356 | */ |
---|
357 | public function map(Closure $func) |
---|
358 | { |
---|
359 | return new static(array_map($func, $this->_elements)); |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * Returns all the elements of this collection that satisfy the predicate p. |
---|
364 | * The order of the elements is preserved. |
---|
365 | * |
---|
366 | * @param Closure $p The predicate used for filtering. |
---|
367 | * @return Collection A collection with the results of the filter operation. |
---|
368 | */ |
---|
369 | public function filter(Closure $p) |
---|
370 | { |
---|
371 | return new static(array_filter($this->_elements, $p)); |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * Applies the given predicate p to all elements of this collection, |
---|
376 | * returning true, if the predicate yields true for all elements. |
---|
377 | * |
---|
378 | * @param Closure $p The predicate. |
---|
379 | * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. |
---|
380 | */ |
---|
381 | public function forAll(Closure $p) |
---|
382 | { |
---|
383 | foreach ($this->_elements as $key => $element) { |
---|
384 | if ( ! $p($key, $element)) { |
---|
385 | return false; |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | return true; |
---|
390 | } |
---|
391 | |
---|
392 | /** |
---|
393 | * Partitions this collection in two collections according to a predicate. |
---|
394 | * Keys are preserved in the resulting collections. |
---|
395 | * |
---|
396 | * @param Closure $p The predicate on which to partition. |
---|
397 | * @return array An array with two elements. The first element contains the collection |
---|
398 | * of elements where the predicate returned TRUE, the second element |
---|
399 | * contains the collection of elements where the predicate returned FALSE. |
---|
400 | */ |
---|
401 | public function partition(Closure $p) |
---|
402 | { |
---|
403 | $coll1 = $coll2 = array(); |
---|
404 | foreach ($this->_elements as $key => $element) { |
---|
405 | if ($p($key, $element)) { |
---|
406 | $coll1[$key] = $element; |
---|
407 | } else { |
---|
408 | $coll2[$key] = $element; |
---|
409 | } |
---|
410 | } |
---|
411 | return array(new static($coll1), new static($coll2)); |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * Returns a string representation of this object. |
---|
416 | * |
---|
417 | * @return string |
---|
418 | */ |
---|
419 | public function __toString() |
---|
420 | { |
---|
421 | return __CLASS__ . '@' . spl_object_hash($this); |
---|
422 | } |
---|
423 | |
---|
424 | /** |
---|
425 | * Clears the collection. |
---|
426 | */ |
---|
427 | public function clear() |
---|
428 | { |
---|
429 | $this->_elements = array(); |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Extract a slice of $length elements starting at position $offset from the Collection. |
---|
434 | * |
---|
435 | * If $length is null it returns all elements from $offset to the end of the Collection. |
---|
436 | * Keys have to be preserved by this method. Calling this method will only return the |
---|
437 | * selected slice and NOT change the elements contained in the collection slice is called on. |
---|
438 | * |
---|
439 | * @param int $offset |
---|
440 | * @param int $length |
---|
441 | * @return array |
---|
442 | */ |
---|
443 | public function slice($offset, $length = null) |
---|
444 | { |
---|
445 | return array_slice($this->_elements, $offset, $length, true); |
---|
446 | } |
---|
447 | } |
---|