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 ArrayList extends Object implements ArrayAccess, Countable, IteratorAggregate
24: {
25: private $list = array();
26:
27:
28: /**
29: * Returns an iterator over all items.
30: * @return ArrayIterator
31: */
32: public function getIterator()
33: {
34: return new ArrayIterator($this->list);
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: * 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: * Returns a item.
71: * @param int
72: * @return mixed
73: * @throws OutOfRangeException
74: */
75: public function offsetGet($index)
76: {
77: if ($index < 0 || $index >= count($this->list)) {
78: throw new OutOfRangeException("Offset invalid or out of range");
79: }
80: return $this->list[(int) $index];
81: }
82:
83:
84: /**
85: * Determines whether a item exists.
86: * @param int
87: * @return bool
88: */
89: public function offsetExists($index)
90: {
91: return $index >= 0 && $index < count($this->list);
92: }
93:
94:
95: /**
96: * Removes the element at the specified position in this list.
97: * @param int
98: * @return void
99: * @throws OutOfRangeException
100: */
101: public function offsetUnset($index)
102: {
103: if ($index < 0 || $index >= count($this->list)) {
104: throw new OutOfRangeException("Offset invalid or out of range");
105: }
106: array_splice($this->list, (int) $index, 1);
107: }
108:
109: }
110: