1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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: * @package Nette
20: */
21: class ArrayList extends Object implements ArrayAccess, Countable, IteratorAggregate
22: {
23: private $list = array();
24:
25:
26:
27: /**
28: * Returns an iterator over all items.
29: * @return ArrayIterator
30: */
31: public function getIterator()
32: {
33: return new ArrayIterator($this->list);
34: }
35:
36:
37:
38: /**
39: * Returns items count.
40: * @return int
41: */
42: public function count()
43: {
44: return count($this->list);
45: }
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: /**
72: * Returns a item.
73: * @param int
74: * @return mixed
75: * @throws OutOfRangeException
76: */
77: public function offsetGet($index)
78: {
79: if ($index < 0 || $index >= count($this->list)) {
80: throw new OutOfRangeException("Offset invalid or out of range");
81: }
82: return $this->list[(int) $index];
83: }
84:
85:
86:
87: /**
88: * Determines whether a item exists.
89: * @param int
90: * @return bool
91: */
92: public function offsetExists($index)
93: {
94: return $index >= 0 && $index < count($this->list);
95: }
96:
97:
98:
99: /**
100: * Removes the element at the specified position in this list.
101: * @param int
102: * @return void
103: * @throws OutOfRangeException
104: */
105: public function offsetUnset($index)
106: {
107: if ($index < 0 || $index >= count($this->list)) {
108: throw new OutOfRangeException("Offset invalid or out of range");
109: }
110: array_splice($this->list, (int) $index, 1);
111: }
112:
113: }
114: