1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://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: */
11:
12: namespace Nette\Collections;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Provides the base class for a generic list (items can be accessed by index).
20: *
21: * @author David Grudl
22: */
23: class ArrayList extends Collection implements IList
24: {
25: /** @var int */
26: protected $base = 0;
27:
28:
29: /**
30: * Inserts the specified element at the specified position in this list.
31: * @param int
32: * @param mixed
33: * @return bool
34: * @throws \ArgumentOutOfRangeException
35: */
36: public function insertAt($index, $item)
37: {
38: $index -= $this->base;
39: if ($index < 0 || $index > count($this)) {
40: throw new \ArgumentOutOfRangeException;
41: }
42:
43: $this->beforeAdd($item);
44: $data = $this->getArrayCopy();
45: array_splice($data, (int) $index, 0, array($item));
46: $this->setArray($data);
47: return TRUE;
48: }
49:
50:
51:
52: /**
53: * Removes the first occurrence of the specified element.
54: * @param mixed
55: * @return bool true if this list changed as a result of the call
56: * @throws \NotSupportedException
57: */
58: public function remove($item)
59: {
60: $this->updating();
61:
62: $index = $this->search($item);
63: if ($index === FALSE) {
64: return FALSE;
65: } else {
66: $data = $this->getArrayCopy();
67: array_splice($data, $index, 1);
68: $this->setArray($data);
69: return TRUE;
70: }
71: }
72:
73:
74:
75: /**
76: * Returns the index of the first occurrence of the specified element,.
77: * or FALSE if this list does not contain this element.
78: * @param mixed
79: * @return int|FALSE
80: */
81: public function indexOf($item)
82: {
83: $index = $this->search($item);
84: return $index === FALSE ? FALSE : $this->base + $index;
85: }
86:
87:
88:
89: /********************* interface \ArrayAccess ****************d*g**/
90:
91:
92:
93: /**
94: * Replaces (or appends) the item (\ArrayAccess implementation).
95: * @param int index
96: * @param object
97: * @return void
98: * @throws \InvalidArgumentException, \NotSupportedException, \ArgumentOutOfRangeException
99: */
100: public function offsetSet($index, $item)
101: {
102: $this->beforeAdd($item);
103:
104: if ($index === NULL) { // append
105: parent::offsetSet(NULL, $item);
106:
107: } else { // replace
108: $index -= $this->base;
109: if ($index < 0 || $index >= count($this)) {
110: throw new \ArgumentOutOfRangeException;
111: }
112: parent::offsetSet($index, $item);
113: }
114: }
115:
116:
117:
118: /**
119: * Returns item (\ArrayAccess implementation).
120: * @param int index
121: * @return mixed
122: * @throws \ArgumentOutOfRangeException
123: */
124: public function offsetGet($index)
125: {
126: $index -= $this->base;
127: if ($index < 0 || $index >= count($this)) {
128: throw new \ArgumentOutOfRangeException;
129: }
130:
131: return parent::offsetGet($index);
132: }
133:
134:
135:
136: /**
137: * Exists item? (\ArrayAccess implementation).
138: * @param int index
139: * @return bool
140: */
141: public function offsetExists($index)
142: {
143: $index -= $this->base;
144: return $index >= 0 && $index < count($this);
145: }
146:
147:
148:
149: /**
150: * Removes the element at the specified position in this list.
151: * @param int index
152: * @return void
153: * @throws \NotSupportedException, \ArgumentOutOfRangeException
154: */
155: public function offsetUnset($index)
156: {
157: $this->updating();
158:
159: $index -= $this->base;
160: if ($index < 0 || $index >= count($this)) {
161: throw new \ArgumentOutOfRangeException;
162: }
163:
164: $data = $this->getArrayCopy();
165: array_splice($data, (int) $index, 1);
166: $this->setArray($data);
167: }
168:
169: }
170: