MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
ArrayCollection.php
Ir para a documentação deste ficheiro.
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
21
22use Closure, ArrayIterator;
23
33{
39 private $_elements;
40
46 public function __construct(array $elements = array())
47 {
48 $this->_elements = $elements;
49 }
50
56 public function toArray()
57 {
58 return $this->_elements;
59 }
60
67 public function first()
68 {
69 return reset($this->_elements);
70 }
71
78 public function last()
79 {
80 return end($this->_elements);
81 }
82
88 public function key()
89 {
90 return key($this->_elements);
91 }
92
98 public function next()
99 {
100 return next($this->_elements);
101 }
102
108 public function current()
109 {
110 return current($this->_elements);
111 }
112
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
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
155 public function offsetExists($offset)
156 {
157 return $this->containsKey($offset);
158 }
159
165 public function offsetGet($offset)
166 {
167 return $this->get($offset);
168 }
169
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
189 public function offsetUnset($offset)
190 {
191 return $this->remove($offset);
192 }
193
200 public function containsKey($key)
201 {
202 return isset($this->_elements[$key]);
203 }
204
215 public function contains($element)
216 {
217 return in_array($element, $this->_elements, true);
218 }
219
226 public function exists(Closure $p)
227 {
228 foreach ($this->_elements as $key => $element)
229 if ($p($key, $element)) return true;
230 return false;
231 }
232
242 public function indexOf($element)
243 {
244 return array_search($element, $this->_elements, true);
245 }
246
253 public function get($key)
254 {
255 if (isset($this->_elements[$key])) {
256 return $this->_elements[$key];
257 }
258 return null;
259 }
260
266 public function getKeys()
267 {
268 return array_keys($this->_elements);
269 }
270
276 public function getValues()
277 {
278 return array_values($this->_elements);
279 }
280
288 public function count()
289 {
290 return count($this->_elements);
291 }
292
302 public function set($key, $value)
303 {
304 $this->_elements[$key] = $value;
305 }
306
313 public function add($value)
314 {
315 $this->_elements[] = $value;
316 return true;
317 }
318
326 public function isEmpty()
327 {
328 return ! $this->_elements;
329 }
330
336 public function getIterator()
337 {
338 return new ArrayIterator($this->_elements);
339 }
340
348 public function map(Closure $func)
349 {
350 return new ArrayCollection(array_map($func, $this->_elements));
351 }
352
360 public function filter(Closure $p)
361 {
362 return new ArrayCollection(array_filter($this->_elements, $p));
363 }
364
372 public function forAll(Closure $p)
373 {
374 foreach ($this->_elements as $key => $element) {
375 if ( ! $p($key, $element)) {
376 return false;
377 }
378 }
379
380 return true;
381 }
382
392 public function partition(Closure $p)
393 {
394 $coll1 = $coll2 = array();
395 foreach ($this->_elements as $key => $element) {
396 if ($p($key, $element)) {
397 $coll1[$key] = $element;
398 } else {
399 $coll2[$key] = $element;
400 }
401 }
402 return array(new ArrayCollection($coll1), new ArrayCollection($coll2));
403 }
404
410 public function __toString()
411 {
412 return __CLASS__ . '@' . spl_object_hash($this);
413 }
414
418 public function clear()
419 {
420 $this->_elements = array();
421 }
422
434 public function slice($offset, $length = null)
435 {
436 return array_slice($this->_elements, $offset, $length, true);
437 }
438}