1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
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: class ArrayList implements ArrayAccess, Countable, IteratorAggregate
21: {
22: private $list = array();
23:
24:
25:
26: /**
27: * Returns an iterator over all items.
28: * @return ArrayIterator
29: */
30: public function getIterator()
31: {
32: return new ArrayIterator($this->list);
33: }
34:
35:
36:
37: /**
38: * Returns items count.
39: * @return int
40: */
41: public function count()
42: {
43: return count($this->list);
44: }
45:
46:
47:
48: /**
49: * Replaces or appends a item.
50: * @param int
51: * @param mixed
52: * @return void
53: * @throws OutOfRangeException
54: */
55: public function offsetSet($index, $value)
56: {
57: if ($index === NULL) {
58: $this->list[] = $value;
59:
60: } elseif ($index < 0 || $index >= count($this->list)) {
61: throw new OutOfRangeException("Offset invalid or out of range");
62:
63: } else {
64: $this->list[(int) $index] = $value;
65: }
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: /**
87: * Determines whether a item exists.
88: * @param int
89: * @return bool
90: */
91: public function offsetExists($index)
92: {
93: return $index >= 0 && $index < count($this->list);
94: }
95:
96:
97:
98: /**
99: * Removes the element at the specified position in this list.
100: * @param int
101: * @return void
102: * @throws OutOfRangeException
103: */
104: public function offsetUnset($index)
105: {
106: if ($index < 0 || $index >= count($this->list)) {
107: throw new OutOfRangeException("Offset invalid or out of range");
108: }
109: array_splice($this->list, (int) $index, 1);
110: }
111:
112: }
113: