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: * Provides the base class for a generic list (items can be accessed by index).
19: *
20: * @author David Grudl
21: *
22: * @property-read \ArrayIterator $iterator
23: */
24: class ArrayList extends Object implements \ArrayAccess, \Countable, \IteratorAggregate
25: {
26: private $list = array();
27:
28:
29: /**
30: * Returns an iterator over all items.
31: * @return \ArrayIterator
32: */
33: public function getIterator()
34: {
35: return new \ArrayIterator($this->list);
36: }
37:
38:
39: /**
40: * Returns items count.
41: * @return int
42: */
43: public function count()
44: {
45: return count($this->list);
46: }
47:
48:
49: /**
50: * Replaces or appends a item.
51: * @param int
52: * @param mixed
53: * @return void
54: * @throws OutOfRangeException
55: */
56: public function offsetSet($index, $value)
57: {
58: if ($index === NULL) {
59: $this->list[] = $value;
60:
61: } elseif ($index < 0 || $index >= count($this->list)) {
62: throw new OutOfRangeException("Offset invalid or out of range");
63:
64: } else {
65: $this->list[(int) $index] = $value;
66: }
67: }
68:
69:
70: /**
71: * Returns a item.
72: * @param int
73: * @return mixed
74: * @throws OutOfRangeException
75: */
76: public function offsetGet($index)
77: {
78: if ($index < 0 || $index >= count($this->list)) {
79: throw new OutOfRangeException("Offset invalid or out of range");
80: }
81: return $this->list[(int) $index];
82: }
83:
84:
85: /**
86: * Determines whether a item exists.
87: * @param int
88: * @return bool
89: */
90: public function offsetExists($index)
91: {
92: return $index >= 0 && $index < count($this->list);
93: }
94:
95:
96: /**
97: * Removes the element at the specified position in this list.
98: * @param int
99: * @return void
100: * @throws OutOfRangeException
101: */
102: public function offsetUnset($index)
103: {
104: if ($index < 0 || $index >= count($this->list)) {
105: throw new OutOfRangeException("Offset invalid or out of range");
106: }
107: array_splice($this->list, (int) $index, 1);
108: }
109:
110: }
111: