1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
17: {
18: use Nette\SmartObject;
19:
20: private $list = [];
21:
22:
23: 24: 25: 26:
27: public function getIterator()
28: {
29: return new \ArrayIterator($this->list);
30: }
31:
32:
33: 34: 35: 36:
37: public function count()
38: {
39: return count($this->list);
40: }
41:
42:
43: 44: 45: 46: 47: 48: 49:
50: public function offsetSet($index, $value)
51: {
52: if ($index !== null && !is_int($index)) {
53: trigger_error('Index is not integer.', E_USER_NOTICE);
54: }
55: if ($index === null) {
56: $this->list[] = $value;
57:
58: } elseif ($index < 0 || $index >= count($this->list)) {
59: throw new Nette\OutOfRangeException('Offset invalid or out of range');
60:
61: } else {
62: $this->list[(int) $index] = $value;
63: }
64: }
65:
66:
67: 68: 69: 70: 71: 72:
73: public function offsetGet($index)
74: {
75: if (!is_int($index)) {
76: trigger_error('Index is not integer.', E_USER_NOTICE);
77: }
78: if ($index < 0 || $index >= count($this->list)) {
79: throw new Nette\OutOfRangeException('Offset invalid or out of range');
80: }
81: return $this->list[(int) $index];
82: }
83:
84:
85: 86: 87: 88: 89:
90: public function offsetExists($index)
91: {
92: return $index >= 0 && $index < count($this->list);
93: }
94:
95:
96: 97: 98: 99: 100: 101:
102: public function offsetUnset($index)
103: {
104: if (!is_int($index)) {
105: trigger_error('Index is not integer.', E_USER_NOTICE);
106: }
107: if ($index < 0 || $index >= count($this->list)) {
108: throw new Nette\OutOfRangeException('Offset invalid or out of range');
109: }
110: array_splice($this->list, (int) $index, 1);
111: }
112:
113:
114: 115: 116: 117: 118:
119: public function prepend($value)
120: {
121: $first = array_slice($this->list, 0, 1);
122: $this->offsetSet(0, $value);
123: array_splice($this->list, 1, 0, $first);
124: }
125: }
126: