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
|
|
|
|
77
|
|
|
return Factory::create(array_combine($this->items, $values)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function flip() |
84
|
|
|
{ |
85
|
|
|
return Factory::create(array_flip($this->items), static::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function values() |
92
|
|
|
{ |
93
|
|
|
return Factory::create(array_values($this->items), static::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function keys() |
100
|
|
|
{ |
101
|
|
|
return Factory::create(array_keys($this->items), static::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function unique() |
108
|
|
|
{ |
109
|
|
|
return Factory::create(array_unique($this->items), static::class); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function contains($value) |
116
|
|
|
{ |
117
|
|
|
return in_array($value, $this->items, true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function merge($items, $inPlace = false) |
124
|
|
|
{ |
125
|
|
|
if (!is_array($items) && !($items instanceof Traversable)) { |
126
|
|
|
$this->doThrow('Invalid input type for '.__METHOD__.', cannot merge.', $items); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return Factory::create(array_merge($this->items, $items), static::class); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function union($items, $inPlace = false) |
136
|
|
|
{ |
137
|
|
|
return Factory::create($this->items + $items, static::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function reverse() |
144
|
|
|
{ |
145
|
|
|
return Factory::create(array_reverse($this->items, true), static::class); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function shift() |
152
|
|
|
{ |
153
|
|
|
return array_shift($this->items); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function pop() |
160
|
|
|
{ |
161
|
|
|
return array_pop($this->items); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function chunk($size) |
168
|
|
|
{ |
169
|
|
|
return Factory::create(array_chunk($this->items, $size, true), static::class); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function slice($offset, $length = PHP_INT_MAX) |
176
|
|
|
{ |
177
|
|
|
return Factory::create(array_slice($this->items, $offset, $length, true), static::class); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function diff($items) |
184
|
|
|
{ |
185
|
|
|
return Factory::create(array_diff($this->items, $items), static::class); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function diffKeys($items) |
192
|
|
|
{ |
193
|
|
|
return Factory::create(array_diff_key($this->items, $items), static::class); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function intersect($items) |
200
|
|
|
{ |
201
|
|
|
return Factory::create(array_intersect($this->items, $items), static::class); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function intersectKeys($items) |
208
|
|
|
{ |
209
|
|
|
return Factory::create(array_intersect_key($this->items, $items), static::class); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|