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 | 47 | 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 | 8 | 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 an array of all elements in the collection |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 91 | public function toArray(): array |
|
94 | |||
95 | /** |
||
96 | * Filter the collection using closure |
||
97 | * |
||
98 | * The closure will get passed each element. Returning true from the |
||
99 | * closure will include that element in the new collection. |
||
100 | * |
||
101 | * @param callable $filter |
||
102 | * @return CollectionInterface |
||
103 | */ |
||
104 | 1 | public function filter(callable $filter): CollectionInterface |
|
108 | |||
109 | /** |
||
110 | * Retrieve an external iterator |
||
111 | * |
||
112 | * @return ArrayIterator |
||
113 | */ |
||
114 | 8 | public function getIterator(): ArrayIterator |
|
118 | |||
119 | /** |
||
120 | * Insert element at the specified index |
||
121 | * |
||
122 | * @param int $index |
||
123 | * @param mixed $element |
||
124 | * @return void |
||
125 | * @throws \OutOfBoundsException if the index doesn't exist |
||
126 | */ |
||
127 | 6 | public function insert(int $index, $element) |
|
133 | |||
134 | /** |
||
135 | * Inserts all elements of a collection at index |
||
136 | * |
||
137 | * @param int $index |
||
138 | * @param CollectionInterface $collection |
||
139 | * @return bool |
||
140 | * @throws \OutOfBoundsException if the index doesn't exist |
||
141 | */ |
||
142 | 4 | public function insertAll(int $index, CollectionInterface $collection): bool |
|
151 | |||
152 | /** |
||
153 | * Returns the element at the index |
||
154 | * |
||
155 | * @param int $index |
||
156 | * @return mixed |
||
157 | * @throws \OutOfBoundsException if the index doesn't exist |
||
158 | */ |
||
159 | 13 | public function get(int $index) |
|
167 | |||
168 | /** |
||
169 | * Returns true if an element exists at the index |
||
170 | * |
||
171 | * @param int $index |
||
172 | * @return bool |
||
173 | */ |
||
174 | 19 | public function has(int $index): bool |
|
178 | |||
179 | /** |
||
180 | * Removes the element at the specified index |
||
181 | * |
||
182 | * This returns the element that was previously at this index |
||
183 | * |
||
184 | * @param int $index |
||
185 | * @return mixed |
||
186 | * @throws \OutOfBoundsException if the index doesn't exist |
||
187 | */ |
||
188 | 2 | public function removeAt(int $index) |
|
195 | |||
196 | /** |
||
197 | * Replace the element at the specified index |
||
198 | * |
||
199 | * Returns the element that was previously at this index |
||
200 | * |
||
201 | * @param int $index |
||
202 | * @param mixed $element |
||
203 | * @return mixed |
||
204 | * @throws \OutOfBoundsException if the index doesn't exist |
||
205 | */ |
||
206 | 3 | public function set(int $index, $element) |
|
219 | |||
220 | /** |
||
221 | * Returns a new ListInterface ranging from $fromIndex inclusive to |
||
222 | * $toIndex exclusive |
||
223 | * |
||
224 | * @param int $fromIndex |
||
225 | * @param int $toIndex |
||
226 | * @return ListInterface |
||
227 | * @throws \OutOfBoundsException If to or from index does not exist |
||
228 | */ |
||
229 | 4 | public function subList(int $fromIndex, int $toIndex): ListInterface |
|
239 | |||
240 | /** |
||
241 | * Assert the index is able to be inserted |
||
242 | * |
||
243 | * @param int $index |
||
244 | * @return void |
||
245 | * @throws \OutOfBoundsException If the index is less than 0 or greater than current size |
||
246 | */ |
||
247 | 13 | protected function assertIndex(int $index) |
|
253 | } |
||
254 |