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