|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2018 Sebastian Böttger <[email protected]> |
|
4
|
|
|
* You may use, distribute and modify this code under the |
|
5
|
|
|
* terms of the MIT license. |
|
6
|
|
|
* |
|
7
|
|
|
* You should have received a copy of the MIT license with |
|
8
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Seboettg\Collection\ArrayList; |
|
12
|
|
|
|
|
13
|
|
|
use closure; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait ArrayListTrait |
|
17
|
|
|
* @package Seboettg\Collection |
|
18
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ArrayListTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $array; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
use ArrayAccessTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* flush array list |
|
31
|
|
|
* |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function clear() |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->array = []; |
|
37
|
1 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function get($key) |
|
44
|
|
|
{ |
|
45
|
6 |
|
return isset($this->array[$key]) ? $this->array[$key] : null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function current() |
|
52
|
|
|
{ |
|
53
|
2 |
|
return current($this->array); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function next() |
|
60
|
|
|
{ |
|
61
|
3 |
|
return next($this->array); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function prev() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return prev($this->array); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function set($key, $element) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->array[$key] = $element; |
|
78
|
1 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
3 |
|
public function append($element) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$this->array[] = $element; |
|
87
|
3 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function add($key, $element) |
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
1 |
|
if (!array_key_exists($key, $this->array)) { |
|
97
|
1 |
|
$this->array[$key] = $element; |
|
98
|
1 |
|
} elseif (is_array($this->array[$key])) { |
|
99
|
|
|
$this->array[$key][] = $element; |
|
100
|
|
|
} else { |
|
101
|
1 |
|
$this->array[$key] = [$this->array[$key], $element]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function remove($key) |
|
111
|
|
|
{ |
|
112
|
1 |
|
unset($this->array[$key]); |
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
4 |
|
public function hasKey($key) |
|
120
|
|
|
{ |
|
121
|
4 |
|
return array_key_exists($key, $this->array); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function hasElement($value) |
|
128
|
|
|
{ |
|
129
|
2 |
|
$result = array_search($value, $this->array, true); |
|
130
|
2 |
|
return ($result !== false); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the first element |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function first() |
|
138
|
|
|
{ |
|
139
|
1 |
|
reset($this->array); |
|
140
|
1 |
|
return $this->array[key($this->array)]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns the last element |
|
145
|
|
|
* @return mixed |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function last() |
|
148
|
|
|
{ |
|
149
|
1 |
|
$item = end($this->array); |
|
150
|
1 |
|
reset($this->array); |
|
151
|
1 |
|
return $item; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritDoc} |
|
156
|
|
|
*/ |
|
157
|
12 |
|
public function toArray() |
|
158
|
|
|
{ |
|
159
|
12 |
|
return $this->array; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle |
|
164
|
|
|
* @see http://php.net/manual/en/function.shuffle.php |
|
165
|
|
|
* @return ArrayListInterface|ArrayListTrait |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function shuffle() |
|
168
|
|
|
{ |
|
169
|
1 |
|
shuffle($this->array); |
|
170
|
1 |
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* returns a clone of this ArrayList, filtered by the given closure function |
|
175
|
|
|
* @param closure $closure |
|
176
|
|
|
* @return ArrayListInterface|ArrayListTrait |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function filter(closure $closure) |
|
179
|
|
|
{ |
|
180
|
1 |
|
$newInstance = new self(); |
|
181
|
1 |
|
$newInstance->setArray(array_filter($this->array, $closure)); |
|
182
|
1 |
|
return $newInstance; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
25 |
|
public function setArray(array $array) |
|
189
|
|
|
{ |
|
190
|
25 |
|
return $this->replace($array); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
27 |
|
public function replace(array $data) |
|
197
|
|
|
{ |
|
198
|
27 |
|
$this->array = $data; |
|
199
|
27 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* returns a clone of this ArrayList, filtered by the given array keys |
|
204
|
|
|
* @param array $keys |
|
205
|
|
|
* @return ArrayListInterface|ArrayListTrait |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function filterByKeys(array $keys) |
|
208
|
|
|
{ |
|
209
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
210
|
1 |
|
$newInstance = new self(); |
|
211
|
1 |
|
$newInstance->setArray(array_filter($this->array, function ($key) use ($keys) { |
|
212
|
1 |
|
return array_search($key, $keys) !== false; |
|
213
|
1 |
|
}, ARRAY_FILTER_USE_KEY)); |
|
214
|
1 |
|
return $newInstance; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* returns a new ArrayList containing all the elements of this ArrayList after applying the callback function to each one. |
|
219
|
|
|
* @param closure $mapFunction |
|
220
|
|
|
* @return ArrayListInterface|ArrayListTrait |
|
221
|
|
|
*/ |
|
222
|
1 |
|
public function map(closure $mapFunction) |
|
223
|
|
|
{ |
|
224
|
1 |
|
$newInstance = new self(); |
|
225
|
1 |
|
$newInstance->setArray(array_map($mapFunction, $this->array)); |
|
226
|
1 |
|
return $newInstance; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns a new ArrayList containing an one-dimensional array of all elements of this ArrayList. Keys are going lost. |
|
231
|
|
|
* @return ArrayListInterface|ArrayListTrait |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function flatten() |
|
234
|
|
|
{ |
|
235
|
1 |
|
$flattenedArray = []; |
|
236
|
1 |
|
array_walk_recursive($this->array, function ($item) use (&$flattenedArray) { |
|
237
|
1 |
|
$flattenedArray[] = $item; |
|
238
|
1 |
|
}); |
|
239
|
1 |
|
$newInstance = new self(); |
|
240
|
1 |
|
$newInstance->setArray($flattenedArray); |
|
241
|
1 |
|
return $newInstance; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Merges the elements of the passed list together with this list so that the values of the passed list are appended |
|
246
|
|
|
* to the end of the this list |
|
247
|
|
|
* @param ArrayListInterface $list |
|
248
|
|
|
* @return void |
|
249
|
|
|
*/ |
|
250
|
1 |
|
public function merge(ArrayListInterface $list) |
|
251
|
|
|
{ |
|
252
|
1 |
|
$this->array = array_merge($this->array, $list->toArray()); |
|
253
|
1 |
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|