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 | public function __construct(array $elements = []) |
||
37 | |||
38 | /** |
||
39 | * Returns true if the collection contains element |
||
40 | * |
||
41 | * @param mixed $element |
||
42 | * @return bool |
||
43 | */ |
||
44 | public function contains($element): bool |
||
48 | |||
49 | /** |
||
50 | * Ensure the element exists in the collection |
||
51 | * |
||
52 | * Returns true if the collection can contain duplicates, |
||
53 | * and false if it cannot. |
||
54 | * |
||
55 | * @param mixed $element |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function add($element): bool |
||
64 | |||
65 | /** |
||
66 | * Removes object if it exists |
||
67 | * |
||
68 | * Returns true if the element was removed |
||
69 | * |
||
70 | * @param mixed $element |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function remove($element): bool |
||
85 | |||
86 | /** |
||
87 | * Removes all elements from a collection |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | public function clear(): void |
||
95 | |||
96 | /** |
||
97 | * Returns an array of all elements in the collection |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function toArray(): array |
||
105 | |||
106 | /** |
||
107 | * Retrieve an external iterator |
||
108 | * |
||
109 | * @return ArrayIterator |
||
110 | */ |
||
111 | public function getIterator(): ArrayIterator |
||
115 | |||
116 | /** |
||
117 | * Insert element at the specified index |
||
118 | * |
||
119 | * @param int $index |
||
120 | * @param mixed $element |
||
121 | * @return void |
||
122 | * @throws \OutOfBoundsException if the index doesn't exist |
||
123 | */ |
||
124 | public function insert(int $index, $element): void |
||
130 | |||
131 | /** |
||
132 | * Inserts all elements of a collection at index |
||
133 | * |
||
134 | * @param int $index |
||
135 | * @param CollectionInterface $collection |
||
136 | * @return bool |
||
137 | * @throws \OutOfBoundsException if the index doesn't exist |
||
138 | */ |
||
139 | public function insertAll(int $index, CollectionInterface $collection): bool |
||
148 | |||
149 | /** |
||
150 | * Returns the element at the index |
||
151 | * |
||
152 | * @param int $index |
||
153 | * @return mixed |
||
154 | * @throws \OutOfBoundsException if the index doesn't exist |
||
155 | */ |
||
156 | public function get(int $index) |
||
164 | |||
165 | /** |
||
166 | * Returns true if an element exists at the index |
||
167 | * |
||
168 | * @param int $index |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function has(int $index): bool |
||
175 | |||
176 | /** |
||
177 | * Removes the element at the specified index |
||
178 | * |
||
179 | * This returns the element that was previously at this index |
||
180 | * |
||
181 | * @param int $index |
||
182 | * @return mixed |
||
183 | * @throws \OutOfBoundsException if the index doesn't exist |
||
184 | */ |
||
185 | public function removeAt(int $index) |
||
192 | |||
193 | /** |
||
194 | * Replace the element at the specified index |
||
195 | * |
||
196 | * Returns the element that was previously at this index |
||
197 | * |
||
198 | * @param int $index |
||
199 | * @param mixed $element |
||
200 | * @return mixed |
||
201 | * @throws \OutOfBoundsException if the index doesn't exist |
||
202 | */ |
||
203 | public function set(int $index, $element) |
||
216 | |||
217 | /** |
||
218 | * Returns a new ListInterface ranging from $fromIndex inclusive to |
||
219 | * $toIndex exclusive |
||
220 | * |
||
221 | * @param int $fromIndex |
||
222 | * @param int $toIndex |
||
223 | * @return ListInterface |
||
224 | * @throws \OutOfBoundsException If to or from index does not exist |
||
225 | */ |
||
226 | public function subList(int $fromIndex, int $toIndex): ListInterface |
||
236 | |||
237 | /** |
||
238 | * Assert the index is able to be inserted |
||
239 | * |
||
240 | * @param int $index |
||
241 | * @return void |
||
242 | * @throws \OutOfBoundsException If the index is less than 0 or greater than current size |
||
243 | */ |
||
244 | protected function assertIndex(int $index): void |
||
250 | } |
||
251 |