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