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