1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Novactive Collection. |
4
|
|
|
* |
5
|
|
|
* @author Luke Visinoni <[email protected], [email protected]> |
6
|
|
|
* @author Sébastien Morel <[email protected], [email protected]> |
7
|
|
|
* @copyright 2017 Novactive |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Novactive\Tests\Perfs; |
12
|
|
|
|
13
|
|
|
use Novactive\Collection\Collection; |
14
|
|
|
use Novactive\Collection\Factory; |
15
|
|
|
use Traversable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ArrayMethodCollection. |
19
|
|
|
*/ |
20
|
|
|
class ArrayMethodCollection extends Collection |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function map(callable $callback) |
26
|
|
|
{ |
27
|
|
|
$keys = array_keys($this->items); |
28
|
|
|
$items = array_map($callback, $this->items, $keys); |
29
|
|
|
|
30
|
|
|
return Factory::create(array_combine($keys, $items), static::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function filter(callable $callback) |
37
|
|
|
{ |
38
|
|
|
return Factory::create(array_filter($this->items, $callback, ARRAY_FILTER_USE_BOTH), static::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function reduce(callable $callback, $initial = null) |
45
|
|
|
{ |
46
|
|
|
return array_reduce($this->items, $callback, $initial); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function each(callable $callback) |
53
|
|
|
{ |
54
|
|
|
array_walk($this->items, $callback); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function combine($values, $inPlace = false) |
63
|
|
|
{ |
64
|
|
|
if (!is_array($values) && !($values instanceof Traversable)) { |
65
|
|
|
$this->doThrow('Invalid input type for '.($inPlace ? 'replace' : 'combine').'.', $values); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// @todo This may change things performance-wise. I had to add this for Traversable $values to work - LV |
69
|
|
|
$values = Factory::getArrayForItems($values); |
70
|
|
|
if (count($values) != count($this->items)) { |
71
|
|
|
$this->doThrow( |
72
|
|
|
'Invalid input for '.($inPlace ? 'replace' : 'combine').', number of items does not match.', |
73
|
|
|
$values |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
if ($inPlace) { |
77
|
|
|
$this->items = array_combine($this->items, $values); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return Factory::create(array_combine($this->items, $values)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function flip() |
89
|
|
|
{ |
90
|
|
|
return Factory::create(array_flip($this->items), static::class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function values() |
97
|
|
|
{ |
98
|
|
|
return Factory::create(array_values($this->items), static::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function keys() |
105
|
|
|
{ |
106
|
|
|
return Factory::create(array_keys($this->items), static::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function unique() |
113
|
|
|
{ |
114
|
|
|
return Factory::create(array_unique($this->items), static::class); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function contains($value) |
121
|
|
|
{ |
122
|
|
|
return in_array($value, $this->items, true); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function merge($items, $inPlace = false) |
129
|
|
|
{ |
130
|
|
|
if (!is_array($items) && !($items instanceof Traversable)) { |
131
|
|
|
$this->doThrow('Invalid input type for '.__METHOD__.', cannot merge.', $items); |
132
|
|
|
} |
133
|
|
|
if ($items instanceof Collection) { |
134
|
|
|
$items = $items->toArray(); |
135
|
|
|
} |
136
|
|
|
if ($inPlace) { |
137
|
|
|
$this->items = array_merge($this->items, $items); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return Factory::create(array_merge($this->items, $items), static::class); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function union($items, $inPlace = false) |
149
|
|
|
{ |
150
|
|
|
if (!is_array($items) && !($items instanceof Traversable)) { |
151
|
|
|
$this->doThrow('Invalid input type for '.($inPlace ? 'absorb' : 'union'), $items); |
152
|
|
|
} |
153
|
|
|
if ($items instanceof Collection) { |
154
|
|
|
$items = $items->toArray(); |
155
|
|
|
} |
156
|
|
|
if ($inPlace) { |
157
|
|
|
$this->items = $this->items + $items; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return Factory::create($this->items + $items, static::class); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function reverse() |
169
|
|
|
{ |
170
|
|
|
return Factory::create(array_reverse($this->items, true), static::class); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function shift() |
177
|
|
|
{ |
178
|
|
|
return array_shift($this->items); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function pop() |
185
|
|
|
{ |
186
|
|
|
return array_pop($this->items); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function chunk($size) |
193
|
|
|
{ |
194
|
|
|
return Factory::create(array_chunk($this->items, $size, true), static::class); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function slice($offset, $length = PHP_INT_MAX) |
201
|
|
|
{ |
202
|
|
|
return Factory::create(array_slice($this->items, $offset, $length, true), static::class); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function diff($items) |
209
|
|
|
{ |
210
|
|
|
if ($items instanceof Collection) { |
211
|
|
|
$items = $items->toArray(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return Factory::create(array_diff($this->items, $items), static::class); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function diffKeys($items) |
221
|
|
|
{ |
222
|
|
|
if ($items instanceof Collection) { |
223
|
|
|
$items = $items->toArray(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return Factory::create(array_diff_key($this->items, $items), static::class); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function intersect($items) |
233
|
|
|
{ |
234
|
|
|
if ($items instanceof Collection) { |
235
|
|
|
$items = $items->toArray(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return Factory::create(array_intersect($this->items, $items), static::class); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
|
public function intersectKeys($items) |
245
|
|
|
{ |
246
|
|
|
if ($items instanceof Collection) { |
247
|
|
|
$items = $items->toArray(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return Factory::create(array_intersect_key($this->items, $items), static::class); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|