1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ClassGeneration package. |
5
|
|
|
* |
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ClassGeneration\Collection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Antonio Spinelli <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ArrayCollection implements CollectionInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* An array containing the entries of this collection. |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $elements; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializes a new ArrayCollection. |
28
|
|
|
* |
29
|
|
|
* @param array $elements |
30
|
|
|
*/ |
31
|
197 |
|
public function __construct(array $elements = array()) |
32
|
|
|
{ |
33
|
197 |
|
$this->elements = $elements; |
34
|
197 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets the PHP array representation of this collection. |
38
|
|
|
* @return array The PHP array representation of this collection. |
39
|
|
|
*/ |
40
|
10 |
|
public function toArray() |
41
|
|
|
{ |
42
|
10 |
|
return $this->elements; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
11 |
|
public function first() |
49
|
|
|
{ |
50
|
11 |
|
return reset($this->elements); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
10 |
|
public function last() |
57
|
|
|
{ |
58
|
10 |
|
return end($this->elements); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function key() |
65
|
|
|
{ |
66
|
1 |
|
return key($this->elements); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function next() |
73
|
|
|
{ |
74
|
1 |
|
return next($this->elements); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
16 |
|
public function current() |
81
|
|
|
{ |
82
|
16 |
|
return current($this->elements); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
10 |
|
public function remove($key) |
89
|
|
|
{ |
90
|
10 |
|
if (isset($this->elements[$key])) { |
91
|
10 |
|
$removed = $this->elements[$key]; |
92
|
10 |
|
unset($this->elements[$key]); |
93
|
|
|
|
94
|
10 |
|
return $removed; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
2 |
|
public function removeElement($element) |
104
|
|
|
{ |
105
|
2 |
|
$key = array_search($element, $this->elements, true); |
106
|
|
|
|
107
|
2 |
|
if ($key !== false) { |
108
|
2 |
|
unset($this->elements[$key]); |
109
|
|
|
|
110
|
2 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function offsetExists($offset) |
120
|
|
|
{ |
121
|
1 |
|
return $this->containsKey($offset); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1 |
|
public function offsetGet($offset) |
128
|
|
|
{ |
129
|
1 |
|
return $this->get($offset); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
14 |
|
public function offsetSet($offset, $value) |
136
|
|
|
{ |
137
|
14 |
|
if (!isset($offset)) { |
138
|
1 |
|
return $this->add($value); |
139
|
|
|
} |
140
|
|
|
|
141
|
14 |
|
return $this->set($offset, $value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
1 |
|
public function offsetUnset($offset) |
148
|
|
|
{ |
149
|
1 |
|
return $this->remove($offset); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
8 |
|
public function containsKey($key) |
156
|
|
|
{ |
157
|
8 |
|
return isset($this->elements[$key]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
20 |
|
public function contains($element) |
164
|
|
|
{ |
165
|
20 |
|
foreach ($this->elements as $collectionElement) { |
166
|
16 |
|
if ($element === $collectionElement) { |
167
|
15 |
|
return true; |
168
|
|
|
} |
169
|
17 |
|
} |
170
|
|
|
|
171
|
8 |
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
* @param string $findElement |
177
|
|
|
*/ |
178
|
7 |
|
public function exists($findKey = null, $findElement = null) |
179
|
|
|
{ |
180
|
7 |
|
if (!is_null($findKey) && is_null($findElement)) { |
181
|
5 |
|
return $this->containsKey($findKey); |
182
|
2 |
|
} elseif (is_null($findKey) && !is_null($findElement)) { |
183
|
1 |
|
return $this->contains($findElement); |
184
|
|
|
} else { |
185
|
1 |
|
return $this->containsKey($findKey) && $this->contains($findElement); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
1 |
|
public function indexOf($element) |
193
|
|
|
{ |
194
|
1 |
|
return array_search($element, $this->elements, true); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
57 |
|
public function get($key) |
201
|
|
|
{ |
202
|
57 |
|
if (isset($this->elements[$key])) { |
203
|
57 |
|
return $this->elements[$key]; |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
return null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
78 |
|
public function getKeys() |
213
|
|
|
{ |
214
|
78 |
|
return array_keys($this->elements); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
1 |
|
public function getValues() |
221
|
|
|
{ |
222
|
1 |
|
return array_values($this->elements); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
60 |
|
public function count() |
229
|
|
|
{ |
230
|
60 |
|
return count($this->elements); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
40 |
|
public function set($key, $value) |
237
|
|
|
{ |
238
|
40 |
|
$this->elements[$key] = $value; |
239
|
40 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
44 |
|
public function add($value) |
245
|
|
|
{ |
246
|
44 |
|
$this->elements[] = $value; |
247
|
|
|
|
248
|
44 |
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
15 |
|
public function isEmpty() |
255
|
|
|
{ |
256
|
15 |
|
return !$this->elements; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
6 |
|
public function getIterator() |
263
|
|
|
{ |
264
|
6 |
|
return new CollectionIterator($this); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
1 |
|
public function clear() |
271
|
|
|
{ |
272
|
1 |
|
$this->elements = array(); |
273
|
1 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
1 |
|
public function slice($offset, $length = null) |
279
|
|
|
{ |
280
|
1 |
|
return array_slice($this->elements, $offset, $length, true); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Sorting by values using a user-defined comparison function. |
285
|
|
|
* |
286
|
|
|
* @param \Closure $callable |
287
|
|
|
* |
288
|
|
|
* @return void |
289
|
|
|
*/ |
290
|
3 |
|
public function sort(\Closure $callable) |
291
|
|
|
{ |
292
|
3 |
|
usort($this->elements, $callable); |
293
|
3 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Sort the list by elements. |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
1 |
|
public function sortAsc() |
300
|
|
|
{ |
301
|
|
|
$this->sort(function ($a, $b) { |
302
|
1 |
|
if ($a === $b) { |
303
|
1 |
|
return 0; |
304
|
|
|
} |
305
|
|
|
|
306
|
1 |
|
return ($a < $b) ? -1 : 1; |
307
|
1 |
|
}); |
308
|
1 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Sort the list by elements. |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
public function sortDesc() |
315
|
|
|
{ |
316
|
1 |
|
$this->sort(function ($a, $b) { |
317
|
1 |
|
if ($a === $b) { |
318
|
1 |
|
return 0; |
319
|
|
|
} |
320
|
|
|
|
321
|
1 |
|
return ($a > $b) ? -1 : 1; |
322
|
1 |
|
}); |
323
|
1 |
|
} |
324
|
|
|
} |
325
|
|
|
|