|
1
|
|
|
<?php namespace Mbh\Collection\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
|
4
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
|
5
|
|
|
use Mbh\Collection\CallbackHeap; |
|
6
|
|
|
use Mbh\Iterator\SliceIterator; |
|
7
|
|
|
use Mbh\Iterator\ConcatIterator; |
|
8
|
|
|
use SplFixedArray; |
|
9
|
|
|
use SplHeap; |
|
10
|
|
|
use SplStack; |
|
11
|
|
|
use LimitIterator; |
|
12
|
|
|
use Iterator; |
|
13
|
|
|
use ArrayAccess; |
|
14
|
|
|
use Countable; |
|
15
|
|
|
use CallbackFilterIterator; |
|
16
|
|
|
use JsonSerializable; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
use Traversable; |
|
19
|
|
|
use ReflectionClass; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* MBHFramework |
|
23
|
|
|
* |
|
24
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
|
25
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
|
26
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
trait Sequenceable |
|
30
|
|
|
{ |
|
31
|
|
|
use Collection; |
|
32
|
|
|
use Sort { |
|
33
|
|
|
Sort::heapSort as heapSortWithCallback; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Factory for building FixedArrays from any traversable |
|
38
|
|
|
* |
|
39
|
|
|
* @return FixedArray |
|
|
|
|
|
|
40
|
|
|
*/ |
|
41
|
|
|
public static function fromItems(Traversable $array): self |
|
42
|
|
|
{ |
|
43
|
|
|
// We can only do it this way if we can count it |
|
44
|
|
|
if ($array instanceof Countable) { |
|
45
|
|
|
$sfa = new SplFixedArray(count($array)); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($array as $i => $elem) { |
|
48
|
|
|
$sfa[$i] = $elem; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return new static($sfa); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
|
55
|
|
|
return static::fromArray(iterator_to_array($array)); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Build from an array |
|
60
|
|
|
* |
|
61
|
|
|
* @return FixedArray |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function fromArray(array $array): self |
|
64
|
|
|
{ |
|
65
|
|
|
return new static(SplFixedArray::fromArray($array)); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Creates a shallow copy of the collection. |
|
70
|
|
|
* |
|
71
|
|
|
* @return CollectionInterface a shallow copy of the collection. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function copy(): CollectionInterface |
|
74
|
|
|
{ |
|
75
|
|
|
return static::fromArray($this->toArray()); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Map elements to a new Sequenceable via a callback |
|
80
|
|
|
* |
|
81
|
|
|
* @param callable $callback Function to map new data |
|
82
|
|
|
* @return Sequenceable |
|
83
|
|
|
*/ |
|
84
|
|
|
public function map(callable $callback): SequenceableInterface |
|
85
|
|
|
{ |
|
86
|
|
|
$count = count($this); |
|
|
|
|
|
|
87
|
|
|
$sfa = new SplFixedArray($count); |
|
88
|
|
|
|
|
89
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
90
|
|
|
$sfa[$i] = $callback($this[$i], $i, $this); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new static($sfa); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* forEach, or "walk" the data |
|
98
|
|
|
* Exists primarily to provide a consistent interface, though it's seldom |
|
99
|
|
|
* any better than a simple php foreach. Mainly useful for chaining. |
|
100
|
|
|
* Named walk for historic reasons - forEach is reserved in PHP |
|
101
|
|
|
* |
|
102
|
|
|
* @param callable $callback Function to call on each element |
|
103
|
|
|
* @return Sequenceable |
|
104
|
|
|
*/ |
|
105
|
|
|
public function walk(callable $callback): SequenceableInterface |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($this as $i => $elem) { |
|
108
|
|
|
$callback($elem, $i, $this); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Filter out elements |
|
116
|
|
|
* |
|
117
|
|
|
* @param callable $callback Function to filter out on false |
|
118
|
|
|
* @return Sequenceable |
|
119
|
|
|
*/ |
|
120
|
|
|
public function filter(callable $callback): SequenceableInterface |
|
121
|
|
|
{ |
|
122
|
|
|
$count = count($this); |
|
|
|
|
|
|
123
|
|
|
$sfa = new SplFixedArray($count); |
|
124
|
|
|
$newCount = 0; |
|
125
|
|
|
|
|
126
|
|
|
foreach ($this as $elem) { |
|
127
|
|
|
if ($callback($elem)) { |
|
128
|
|
|
$sfa[$newCount++] = $elem; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$sfa->setSize($newCount); |
|
133
|
|
|
return new static($sfa); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Reduce to a single value |
|
138
|
|
|
* |
|
139
|
|
|
* @param callable $callback Callback( |
|
140
|
|
|
* mixed $previous, mixed $current[, mixed $index, mixed $immArray] |
|
141
|
|
|
* ):mixed Callback to run reducing function |
|
142
|
|
|
* @param mixed $accumulator Initial value for first argument |
|
143
|
|
|
*/ |
|
144
|
|
|
public function reduce(callable $callback, $accumulator = null) |
|
145
|
|
|
{ |
|
146
|
|
|
foreach ($this as $i => $elem) { |
|
147
|
|
|
$accumulator = $callback($accumulator, $elem, $i, $this); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $accumulator; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Join a set of strings together. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $token Main token to put between elements |
|
157
|
|
|
* @param string $secondToken If set, $token on left $secondToken on right |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public function join(string $token = ',', string $secondToken = null): string |
|
161
|
|
|
{ |
|
162
|
|
|
$str = ""; |
|
163
|
|
|
if ($secondToken) { |
|
|
|
|
|
|
164
|
|
|
foreach ($this as $i => $elem) { |
|
165
|
|
|
$str .= $token . (string) $elem . $secondToken; |
|
166
|
|
|
} |
|
167
|
|
|
} else { |
|
168
|
|
|
$this->rewind(); |
|
|
|
|
|
|
169
|
|
|
while ($this->valid()) { |
|
|
|
|
|
|
170
|
|
|
$str .= (string) $this->current(); |
|
|
|
|
|
|
171
|
|
|
$this->next(); |
|
|
|
|
|
|
172
|
|
|
if ($this->valid()) { |
|
173
|
|
|
$str .= $token; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $str; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Take a slice of the array |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $begin Start index of slice |
|
185
|
|
|
* @param int $end End index of slice |
|
186
|
|
|
* @return Sequenceable |
|
187
|
|
|
*/ |
|
188
|
|
|
public function slice(int $begin = 0, int $end = null): SequenceableInterface |
|
189
|
|
|
{ |
|
190
|
|
|
$it = new SliceIterator($this, $begin, $end); |
|
|
|
|
|
|
191
|
|
|
return new static($it); |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Concat to the end of this array |
|
196
|
|
|
* |
|
197
|
|
|
* @param Traversable,... |
|
198
|
|
|
* @return Sequenceable |
|
199
|
|
|
*/ |
|
200
|
|
|
public function concat(): SequenceableInterface |
|
201
|
|
|
{ |
|
202
|
|
|
$args = func_get_args(); |
|
203
|
|
|
array_unshift($args, $this); |
|
204
|
|
|
|
|
205
|
|
|
// Concat this iterator, and variadic args |
|
206
|
|
|
$class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
|
207
|
|
|
$concatIt = $class->newInstanceArgs($args); |
|
208
|
|
|
|
|
209
|
|
|
// Create as new immutable's iterator |
|
210
|
|
|
return new static($concatIt); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Find a single element |
|
215
|
|
|
* |
|
216
|
|
|
* @param callable $callback The test to run on each element |
|
217
|
|
|
* @return mixed The element we found |
|
218
|
|
|
*/ |
|
219
|
|
|
public function find(callable $callback) |
|
220
|
|
|
{ |
|
221
|
|
|
foreach ($this as $i => $elem) { |
|
222
|
|
|
if ($callback($elem, $i, $this)) { |
|
223
|
|
|
return $elem; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Return a new sorted Sequenceable |
|
230
|
|
|
* |
|
231
|
|
|
* @param callable $callback The sort callback |
|
232
|
|
|
* @return Sequenceable |
|
233
|
|
|
*/ |
|
234
|
|
|
public function sort(callable $callback = null) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($callback) { |
|
237
|
|
|
return $this->mergeSort($callback); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $this->arraySort(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Sort a new Sequenceable by filtering through a heap. |
|
245
|
|
|
* Tends to run much faster than array or merge sorts, since you're only |
|
246
|
|
|
* sorting the pointers, and the sort function is running in a highly |
|
247
|
|
|
* optimized space. |
|
248
|
|
|
* |
|
249
|
|
|
* @param SplHeap $heap The heap to run for sorting |
|
250
|
|
|
* @return Sequenceable |
|
251
|
|
|
*/ |
|
252
|
|
|
public function heapSort(SplHeap $heap): SequenceableInterface |
|
253
|
|
|
{ |
|
254
|
|
|
foreach ($this as $item) { |
|
255
|
|
|
$heap->insert($item); |
|
256
|
|
|
} |
|
257
|
|
|
return static::fromItems($heap); |
|
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths