1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Collection; |
8
|
|
|
|
9
|
|
|
use ArrayIterator; |
10
|
|
|
use OutOfBoundsException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ArrayList |
14
|
|
|
* |
15
|
|
|
* Implements [@see ListInterface] and maintains an array of elements |
16
|
|
|
* |
17
|
|
|
* @author Nate Brunette <[email protected]> |
18
|
|
|
*/ |
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
|
102 |
|
public function __construct(array $elements = []) |
34
|
|
|
{ |
35
|
102 |
|
$this->elements = array_values($elements); |
36
|
102 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns true if the collection contains element |
40
|
|
|
* |
41
|
|
|
* @param mixed $element |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
6 |
|
public function contains($element): bool |
45
|
|
|
{ |
46
|
6 |
|
return in_array($element, $this->elements, true); |
47
|
|
|
} |
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
|
47 |
|
public function add($element): bool |
59
|
|
|
{ |
60
|
47 |
|
$this->elements[] = $element; |
61
|
|
|
|
62
|
47 |
|
return true; |
63
|
|
|
} |
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
|
8 |
|
public function remove($element): bool |
74
|
|
|
{ |
75
|
8 |
|
$index = $this->indexOf($element); |
76
|
|
|
|
77
|
8 |
|
if (-1 === $index) { |
78
|
2 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
array_splice($this->elements, $index, 1); |
82
|
|
|
|
83
|
7 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Removes all elements from a collection |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
1 |
|
public function clear(): void |
92
|
|
|
{ |
93
|
1 |
|
$this->elements = []; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns an array of all elements in the collection |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
124 |
|
public function toArray(): array |
102
|
|
|
{ |
103
|
124 |
|
return $this->elements; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retrieve an external iterator |
108
|
|
|
* |
109
|
|
|
* @return ArrayIterator |
110
|
|
|
*/ |
111
|
8 |
|
public function getIterator(): ArrayIterator |
112
|
|
|
{ |
113
|
8 |
|
return new ArrayIterator($this->elements); |
114
|
|
|
} |
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
|
6 |
|
public function insert(int $index, $element): void |
125
|
|
|
{ |
126
|
6 |
|
$this->assertIndex($index); |
127
|
|
|
|
128
|
5 |
|
array_splice($this->elements, $index, 0, [$element]); |
129
|
5 |
|
} |
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
|
4 |
|
public function insertAll(int $index, CollectionInterface $collection): bool |
140
|
|
|
{ |
141
|
4 |
|
$this->assertIndex($index); |
142
|
|
|
|
143
|
3 |
|
$size = $this->count(); |
144
|
3 |
|
array_splice($this->elements, $index, 0, $collection->toArray()); |
145
|
|
|
|
146
|
3 |
|
return $size !== $this->count(); |
147
|
|
|
} |
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
|
13 |
|
public function get(int $index) |
157
|
|
|
{ |
158
|
13 |
|
if (!$this->has($index)) { |
159
|
2 |
|
throw new OutOfBoundsException(sprintf('Tried to access element at index "%s"', $index)); |
160
|
|
|
} |
161
|
|
|
|
162
|
11 |
|
return $this->elements[$index]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns true if an element exists at the index |
167
|
|
|
* |
168
|
|
|
* @param int $index |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
19 |
|
public function has(int $index): bool |
172
|
|
|
{ |
173
|
19 |
|
return array_key_exists($index, $this->elements); |
174
|
|
|
} |
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
|
2 |
|
public function removeAt(int $index) |
186
|
|
|
{ |
187
|
2 |
|
$oldElement = $this->get($index); |
188
|
1 |
|
array_splice($this->elements, $index, 1); |
189
|
|
|
|
190
|
1 |
|
return $oldElement; |
191
|
|
|
} |
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
|
3 |
|
public function set(int $index, $element) |
204
|
|
|
{ |
205
|
3 |
|
$this->assertIndex($index); |
206
|
|
|
|
207
|
2 |
|
$oldElement = null; |
208
|
2 |
|
if ($this->has($index)) { |
209
|
1 |
|
$oldElement = $this->get($index); |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
$this->elements[$index] = $element; |
213
|
|
|
|
214
|
2 |
|
return $oldElement; |
215
|
|
|
} |
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
|
4 |
|
public function subList(int $fromIndex, int $toIndex): ListInterface |
227
|
|
|
{ |
228
|
4 |
|
if (!$this->has($fromIndex) || !$this->has($toIndex - 1)) { |
229
|
2 |
|
throw new OutOfBoundsException('Unable to create sub list as $toIndex or $fromIndex do not exist in list'); |
230
|
|
|
} |
231
|
|
|
|
232
|
2 |
|
$newList = array_slice($this->toArray(), $fromIndex, $toIndex - $fromIndex); |
233
|
|
|
|
234
|
2 |
|
return new static($newList); |
235
|
|
|
} |
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
|
13 |
|
protected function assertIndex(int $index): void |
245
|
|
|
{ |
246
|
13 |
|
if ($index < 0 || $index > $this->count()) { |
247
|
3 |
|
throw new OutOfBoundsException(sprintf('Element unable to be inserted at index "%s"', $index)); |
248
|
|
|
} |
249
|
10 |
|
} |
250
|
|
|
} |
251
|
|
|
|