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