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