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 | 67 | 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 | 42 | public function add($element): bool |
|
53 | |||
54 | /** |
||
55 | * Removes object if it exists |
||
56 | * |
||
57 | * Returns true if the element was removed |
||
58 | * |
||
59 | * @param mixed $element |
||
60 | * @return bool |
||
61 | */ |
||
62 | 6 | public function remove($element): bool |
|
74 | |||
75 | /** |
||
76 | * Removes all elements from a collection |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | 1 | public function clear() |
|
84 | |||
85 | /** |
||
86 | * Returns the size of the collection |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 41 | public function count(): int |
|
94 | |||
95 | /** |
||
96 | * Returns true if the collection contains element |
||
97 | * |
||
98 | * @param mixed $element |
||
99 | * @return bool |
||
100 | */ |
||
101 | 13 | public function contains($element): bool |
|
105 | |||
106 | /** |
||
107 | * Returns an array of all elements in the collection |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 20 | public function toArray(): array |
|
115 | |||
116 | /** |
||
117 | * Filter the collection using closure |
||
118 | * |
||
119 | * The closure will get passed each element. Returning true from the |
||
120 | * closure will include that element in the new collection. |
||
121 | * |
||
122 | * @param callable $filter |
||
123 | * @return CollectionInterface |
||
124 | */ |
||
125 | 1 | public function filter(callable $filter): CollectionInterface |
|
129 | |||
130 | /** |
||
131 | * Retrieve an external iterator |
||
132 | * |
||
133 | * @return ArrayIterator |
||
134 | */ |
||
135 | 59 | public function getIterator(): ArrayIterator |
|
139 | |||
140 | /** |
||
141 | * Insert element at the specified index |
||
142 | * |
||
143 | * @param int $index |
||
144 | * @param mixed $element |
||
145 | * @return void |
||
146 | * @throws \OutOfBoundsException if the index doesn't exist |
||
147 | */ |
||
148 | 6 | public function insert(int $index, $element) |
|
154 | |||
155 | /** |
||
156 | * Inserts all elements of a collection at index |
||
157 | * |
||
158 | * @param int $index |
||
159 | * @param CollectionInterface $collection |
||
160 | * @return bool |
||
161 | * @throws \OutOfBoundsException if the index doesn't exist |
||
162 | */ |
||
163 | 4 | public function insertAll(int $index, CollectionInterface $collection): bool |
|
172 | |||
173 | /** |
||
174 | * Returns the element at the index |
||
175 | * |
||
176 | * @param int $index |
||
177 | * @return mixed |
||
178 | * @throws \OutOfBoundsException if the index doesn't exist |
||
179 | */ |
||
180 | 13 | public function get(int $index) |
|
188 | |||
189 | /** |
||
190 | * Returns true if an element exists at the index |
||
191 | * |
||
192 | * @param int $index |
||
193 | * @return bool |
||
194 | */ |
||
195 | 19 | public function has(int $index): bool |
|
199 | |||
200 | /** |
||
201 | * Returns the index of the first instance of the element, -1 if the element |
||
202 | * doesn't exist |
||
203 | * |
||
204 | * @param mixed $element |
||
205 | * @return int |
||
206 | */ |
||
207 | 8 | public function indexOf($element): int |
|
213 | |||
214 | /** |
||
215 | * Returns the index of the last instance of the element, -1 if the element |
||
216 | * doesn't exist |
||
217 | * |
||
218 | * @param mixed $element |
||
219 | * @return int |
||
220 | */ |
||
221 | 2 | public function lastIndexOf($element): int |
|
230 | |||
231 | /** |
||
232 | * Removes the element at the specified index |
||
233 | * |
||
234 | * This returns the element that was previously at this index |
||
235 | * |
||
236 | * @param int $index |
||
237 | * @return mixed |
||
238 | * @throws \OutOfBoundsException if the index doesn't exist |
||
239 | */ |
||
240 | 2 | public function removeAt(int $index) |
|
247 | |||
248 | /** |
||
249 | * Replace the element at the specified index |
||
250 | * |
||
251 | * Returns the element that was previously at this index |
||
252 | * |
||
253 | * @param int $index |
||
254 | * @param mixed $element |
||
255 | * @return mixed |
||
256 | * @throws \OutOfBoundsException if the index doesn't exist |
||
257 | */ |
||
258 | 3 | public function set(int $index, $element) |
|
271 | |||
272 | /** |
||
273 | * Returns a new ListInterface ranging from $fromIndex inclusive to |
||
274 | * $toIndex exclusive |
||
275 | * |
||
276 | * @param int $fromIndex |
||
277 | * @param int $toIndex |
||
278 | * @return ListInterface |
||
279 | * @throws \OutOfBoundsException If to or from index does not exist |
||
280 | */ |
||
281 | 4 | public function subList(int $fromIndex, int $toIndex): ListInterface |
|
291 | |||
292 | /** |
||
293 | * Assert the index is able to be inserted |
||
294 | * |
||
295 | * @param int $index |
||
296 | * @return void |
||
297 | * @throws \OutOfBoundsException If the index is less than 0 or greater than current size |
||
298 | */ |
||
299 | 13 | private function assertIndex(int $index) |
|
305 | } |
||
306 |