1 | <?php |
||
19 | class ArrayList extends AbstractList |
||
20 | { |
||
21 | /** |
||
22 | * An array of elements |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $elements = []; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param array $elements |
||
32 | */ |
||
33 | 34 | public function __construct(array $elements = []) |
|
37 | |||
38 | /** |
||
39 | * Ensure the element exists in the collection |
||
40 | * |
||
41 | * Returns true if the collection can contain duplicates, |
||
42 | * and false if it cannot. |
||
43 | * |
||
44 | * @param mixed $element |
||
45 | * @return bool |
||
46 | */ |
||
47 | 31 | public function add($element): bool |
|
53 | |||
54 | /** |
||
55 | * Removes object if it exists |
||
56 | * |
||
57 | * By default this method will use strict comparison checking, passing false |
||
58 | * in will use a double equals (==) instead. |
||
59 | * |
||
60 | * Returns true if the element was removed |
||
61 | * |
||
62 | * @param mixed $element |
||
63 | * @param bool $strict |
||
64 | * @return bool |
||
65 | */ |
||
66 | 4 | public function remove($element, bool $strict = true): bool |
|
78 | |||
79 | /** |
||
80 | * Removes all elements from a collection |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | 1 | public function clear() |
|
88 | |||
89 | /** |
||
90 | * Returns the size of the collection |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | 30 | public function count(): int |
|
98 | |||
99 | /** |
||
100 | * Returns true if the collection contains element |
||
101 | * |
||
102 | * @param mixed $element |
||
103 | * @param bool $strict |
||
104 | * @return bool |
||
105 | */ |
||
106 | 2 | public function contains($element, bool $strict = true): bool |
|
110 | |||
111 | /** |
||
112 | * Returns an array of all elements in the collection |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 13 | public function toArray(): array |
|
120 | |||
121 | /** |
||
122 | * Filter the collection using closure |
||
123 | * |
||
124 | * The closure will get passed each element. Returning true from the |
||
125 | * closure will include that element in the new collection. |
||
126 | * |
||
127 | * @param callable $filter |
||
128 | * @return CollectionInterface |
||
129 | */ |
||
130 | 1 | public function filter(callable $filter): CollectionInterface |
|
134 | |||
135 | /** |
||
136 | * Retrieve an external iterator |
||
137 | * |
||
138 | * @return ArrayIterator |
||
139 | */ |
||
140 | 25 | public function getIterator(): ArrayIterator |
|
144 | |||
145 | /** |
||
146 | * Insert element at the specified index |
||
147 | * |
||
148 | * @param int $index |
||
149 | * @param mixed $element |
||
150 | * @return void |
||
151 | * @throws \OutOfBoundsException if the index doesn't exist |
||
152 | */ |
||
153 | 6 | public function insert(int $index, $element) |
|
159 | 5 | ||
160 | 5 | /** |
|
161 | * Inserts all elements of a collection at index |
||
162 | * |
||
163 | * @param int $index |
||
164 | * @param CollectionInterface $collection |
||
165 | * @return bool |
||
166 | * @throws \OutOfBoundsException if the index doesn't exist |
||
167 | */ |
||
168 | public function insertAll(int $index, CollectionInterface $collection): bool |
||
177 | 3 | ||
178 | /** |
||
179 | 3 | * Returns the element at the index |
|
180 | * |
||
181 | * @param int $index |
||
182 | * @return mixed |
||
183 | * @throws \OutOfBoundsException if the index doesn't exist |
||
184 | */ |
||
185 | public function get(int $index) |
||
193 | |||
194 | /** |
||
195 | 11 | * Returns true if an element exists at the index |
|
196 | * |
||
197 | * @param int $index |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function has(int $index): bool |
||
204 | 19 | ||
205 | /** |
||
206 | 19 | * Returns the index of the first instance of the element, -1 if the element |
|
207 | * doesn't exist |
||
208 | * |
||
209 | * By default this method will use strict comparison checking, passing false |
||
210 | * in will use a double equals (==) instead. |
||
211 | * |
||
212 | * @param mixed $element |
||
213 | * @param bool $strict |
||
214 | * @return int |
||
215 | */ |
||
216 | public function indexOf($element, bool $strict = true): int |
||
222 | 7 | ||
223 | /** |
||
224 | 7 | * Returns the index of the last instance of the element, -1 if the element |
|
225 | * doesn't exist |
||
226 | * |
||
227 | * By default this method will use strict comparison checking, passing false |
||
228 | * in will use a double equals (==) instead. |
||
229 | * |
||
230 | * @param mixed $element |
||
231 | * @param bool $strict |
||
232 | * @return int |
||
233 | */ |
||
234 | public function lastIndexOf($element, bool $strict = true): int |
||
243 | |||
244 | /** |
||
245 | 3 | * Removes the element at the specified index |
|
246 | * |
||
247 | * This returns the element that was previously at this index |
||
248 | * |
||
249 | * @param int $index |
||
250 | * @return mixed |
||
251 | * @throws \OutOfBoundsException if the index doesn't exist |
||
252 | */ |
||
253 | public function removeAt(int $index) |
||
260 | 1 | ||
261 | /** |
||
262 | 1 | * Replace the element at the specified index |
|
263 | * |
||
264 | * Returns the element that was previously at this index |
||
265 | * |
||
266 | * @param int $index |
||
267 | * @param mixed $element |
||
268 | * @return mixed |
||
269 | * @throws \OutOfBoundsException if the index doesn't exist |
||
270 | */ |
||
271 | public function set(int $index, $element) |
||
284 | |||
285 | /** |
||
286 | 2 | * Returns a new ListInterface ranging from $fromIndex inclusive to |
|
287 | * $toIndex exclusive |
||
288 | 2 | * |
|
289 | * @param int $fromIndex |
||
290 | * @param int $toIndex |
||
291 | * @return ListInterface |
||
292 | * @throws \OutOfBoundsException If to or from index does not exist |
||
293 | */ |
||
294 | public function subList(int $fromIndex, int $toIndex): ListInterface |
||
304 | |||
305 | /** |
||
306 | 2 | * Assert the index is able to be inserted |
|
307 | * |
||
308 | 2 | * @param int $index |
|
309 | * @return void |
||
310 | * @throws \OutOfBoundsException If the index is less than 0 or greater than current size |
||
311 | */ |
||
312 | private function assertIndex(int $index):void |
||
318 | } |
||
319 |