1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: /**
14: * Provides the base class for a generic list (items can be accessed by index).
15: *
16: * @property-read \ArrayIterator $iterator
17: */
18: class ArrayList extends Nette\Object implements \ArrayAccess, \Countable, \IteratorAggregate
19: {
20: private $list = array();
21:
22:
23: /**
24: * Returns an iterator over all items.
25: * @return \ArrayIterator
26: */
27: public function getIterator()
28: {
29: return new \ArrayIterator($this->list);
30: }
31:
32:
33: /**
34: * Returns items count.
35: * @return int
36: */
37: public function count()
38: {
39: return count($this->list);
40: }
41:
42:
43: /**
44: * Replaces or appends a item.
45: * @param int|NULL
46: * @param mixed
47: * @return void
48: * @throws Nette\OutOfRangeException
49: */
50: public function offsetSet($index, $value)
51: {
52: if ($index === NULL) {
53: $this->list[] = $value;
54:
55: } elseif ($index < 0 || $index >= count($this->list)) {
56: throw new Nette\OutOfRangeException('Offset invalid or out of range');
57:
58: } else {
59: $this->list[(int) $index] = $value;
60: }
61: }
62:
63:
64: /**
65: * Returns a item.
66: * @param int
67: * @return mixed
68: * @throws Nette\OutOfRangeException
69: */
70: public function offsetGet($index)
71: {
72: if ($index < 0 || $index >= count($this->list)) {
73: throw new Nette\OutOfRangeException('Offset invalid or out of range');
74: }
75: return $this->list[(int) $index];
76: }
77:
78:
79: /**
80: * Determines whether a item exists.
81: * @param int
82: * @return bool
83: */
84: public function offsetExists($index)
85: {
86: return $index >= 0 && $index < count($this->list);
87: }
88:
89:
90: /**
91: * Removes the element at the specified position in this list.
92: * @param int
93: * @return void
94: * @throws Nette\OutOfRangeException
95: */
96: public function offsetUnset($index)
97: {
98: if ($index < 0 || $index >= count($this->list)) {
99: throw new Nette\OutOfRangeException('Offset invalid or out of range');
100: }
101: array_splice($this->list, (int) $index, 1);
102: }
103:
104: }
105: