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 | * Retrieve an external iterator |
||
97 | * |
||
98 | * @return ArrayIterator |
||
99 | */ |
||
100 | 8 | public function getIterator(): ArrayIterator |
|
104 | |||
105 | /** |
||
106 | * Insert element at the specified index |
||
107 | * |
||
108 | * @param int $index |
||
109 | * @param mixed $element |
||
110 | * @return void |
||
111 | * @throws \OutOfBoundsException if the index doesn't exist |
||
112 | */ |
||
113 | 6 | public function insert(int $index, $element) |
|
119 | |||
120 | /** |
||
121 | * Inserts all elements of a collection at index |
||
122 | * |
||
123 | * @param int $index |
||
124 | * @param CollectionInterface $collection |
||
125 | * @return bool |
||
126 | * @throws \OutOfBoundsException if the index doesn't exist |
||
127 | */ |
||
128 | 4 | public function insertAll(int $index, CollectionInterface $collection): bool |
|
137 | |||
138 | /** |
||
139 | * Returns the element at the index |
||
140 | * |
||
141 | * @param int $index |
||
142 | * @return mixed |
||
143 | * @throws \OutOfBoundsException if the index doesn't exist |
||
144 | */ |
||
145 | 13 | public function get(int $index) |
|
153 | |||
154 | /** |
||
155 | * Returns true if an element exists at the index |
||
156 | * |
||
157 | * @param int $index |
||
158 | * @return bool |
||
159 | */ |
||
160 | 19 | public function has(int $index): bool |
|
164 | |||
165 | /** |
||
166 | * Removes the element at the specified index |
||
167 | * |
||
168 | * This returns the element that was previously at this index |
||
169 | * |
||
170 | * @param int $index |
||
171 | * @return mixed |
||
172 | * @throws \OutOfBoundsException if the index doesn't exist |
||
173 | */ |
||
174 | 2 | public function removeAt(int $index) |
|
181 | |||
182 | /** |
||
183 | * Replace the element at the specified index |
||
184 | * |
||
185 | * Returns the element that was previously at this index |
||
186 | * |
||
187 | * @param int $index |
||
188 | * @param mixed $element |
||
189 | * @return mixed |
||
190 | * @throws \OutOfBoundsException if the index doesn't exist |
||
191 | */ |
||
192 | 3 | public function set(int $index, $element) |
|
205 | |||
206 | /** |
||
207 | * Assert the index is able to be inserted |
||
208 | * |
||
209 | * @param int $index |
||
210 | * @return void |
||
211 | * @throws \OutOfBoundsException If the index is less than 0 or greater than current size |
||
212 | */ |
||
213 | 13 | protected function assertIndex(int $index) |
|
219 | } |
||
220 |