1 | <?php |
||
51 | class Psi |
||
52 | { |
||
53 | /** @var array */ |
||
54 | private $inputs; |
||
55 | |||
56 | /** @var \ArrayIterator */ |
||
57 | private $operationChain; |
||
58 | /** @var PsiFactoryInterface */ |
||
59 | private $factory; |
||
60 | |||
61 | private $options; |
||
62 | |||
63 | /** |
||
64 | * @param mixed $_ Everything that can be iterated, Provide as many params as you want (from 1 to n) |
||
65 | * |
||
66 | * @return Psi |
||
67 | * @throws PsiException |
||
68 | */ |
||
69 | 70 | public static function it($_ = null) |
|
73 | |||
74 | /** |
||
75 | * @param array $inputs |
||
76 | */ |
||
77 | 70 | protected function __construct($inputs) |
|
85 | |||
86 | //// CONFIGURATION METHODS /////////////////////////////////////////////////////////////////////////////////////// |
||
87 | |||
88 | /** |
||
89 | * @param PsiFactoryInterface $factory |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function useFactory(PsiFactoryInterface $factory) |
||
99 | |||
100 | /** |
||
101 | * Configure if the keys should be preserved when we have multiple inputs. |
||
102 | * |
||
103 | * Depending on the inputs, preserving the keys will most likely lead to conflicts in the operation like |
||
104 | * - sort |
||
105 | * - unique |
||
106 | * Thus the outcome of these operations will be unpredicted. |
||
107 | * |
||
108 | * By default this option is turned OFF. So for multiple inputs the keys will get lost. On the other hand all |
||
109 | * values will survive all operations. |
||
110 | * |
||
111 | * @param bool $preserve |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function preserveKeysOfMultipleInputs($preserve = true) |
||
121 | |||
122 | //// INTERMEDIATE OPERATIONS - working on one item at a time ///////////////////////////////////////////////////// |
||
123 | |||
124 | /** |
||
125 | * @param \Closure|UnaryFunctionInterface $unaryFunction |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | 8 | public function filter($unaryFunction) |
|
135 | |||
136 | /** |
||
137 | * @param \Closure|UnaryFunctionInterface $unaryFunction |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | 5 | public function filterKey($unaryFunction) |
|
147 | |||
148 | /** |
||
149 | * @param \Closure|BinaryFunctionInterface $biPredicate |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | 5 | public function filterValueKey($biPredicate) |
|
159 | |||
160 | /** |
||
161 | * @param \Closure|UnaryFunctionInterface $unaryFunction |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | 7 | public function anyMatch($unaryFunction) |
|
171 | |||
172 | /** |
||
173 | * @param \Closure|BinaryFunctionInterface $biFunction |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | 4 | public function each($biFunction) |
|
183 | |||
184 | /** |
||
185 | * @param \Closure|BinaryFunctionInterface $function |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | 12 | public function map($function) |
|
195 | |||
196 | //// FULL SET OPERATIONS - working on all items at a time //////////////////////////////////////////////////////// |
||
197 | |||
198 | /** |
||
199 | * Flattens the input recursively into a no longer nested iterator. |
||
200 | * |
||
201 | * Example: [1, [2, [3, 4], 5], 6] will become [1, 2, 3, 4, 5, 6] |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | 4 | public function flatten() |
|
211 | |||
212 | /** |
||
213 | * @param UnaryFunctionInterface|\Closure $function |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function group($function) |
||
223 | |||
224 | /** |
||
225 | * @param int|null $sortFlags |
||
226 | * |
||
227 | * @see sort() |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | 3 | public function sort($sortFlags = null) |
|
237 | |||
238 | /** |
||
239 | * @param UnaryFunctionInterface|\Closure $function Return the value used for comparison |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function sortBy($function) |
||
249 | |||
250 | /** |
||
251 | * @param int|null $sortFlags |
||
252 | * |
||
253 | * @see sort() |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | 1 | public function rsort($sortFlags = null) |
|
263 | |||
264 | /** |
||
265 | * @param int|null $sortFlags |
||
266 | * |
||
267 | * @see ksort() |
||
268 | * |
||
269 | * @return $this |
||
270 | */ |
||
271 | 1 | public function ksort($sortFlags = null) |
|
277 | |||
278 | /** |
||
279 | * @param int|null $sortFlags |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | 1 | public function krsort($sortFlags = null) |
|
289 | |||
290 | /** |
||
291 | * @param BinaryFunctionInterface|\Closure $biFunction |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | 2 | public function usort($biFunction) |
|
301 | |||
302 | /** |
||
303 | * @param bool $compareStrict |
||
304 | * |
||
305 | * @return $this |
||
306 | */ |
||
307 | 1 | public function unique($compareStrict = true) |
|
313 | |||
314 | /** |
||
315 | * @param BinaryFunctionInterface|\Closure $biFunction |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | 1 | public function uksort($biFunction) |
|
325 | |||
326 | /** |
||
327 | * @return $this |
||
328 | */ |
||
329 | 1 | public function reverse() |
|
335 | |||
336 | //// TERMINAL OPERATIONS - generating output and ending the chain //////////////////////////////////////////////// |
||
337 | |||
338 | /** |
||
339 | * @return \Iterator |
||
340 | */ |
||
341 | 33 | public function collect() |
|
345 | |||
346 | /** |
||
347 | * Terminal operation that will collect a "real" array with numeric keys 0, 1, 2, ... |
||
348 | * |
||
349 | * The original keys will be lost. If you need the original keys please use toKeyValueArray() |
||
350 | * |
||
351 | * @see Psi::toKeyValueArray() |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | 24 | public function toArray() |
|
359 | |||
360 | /** |
||
361 | * Terminal operation that will collect a php array while keeping the keys. |
||
362 | * |
||
363 | * Use this over toArray() if keeping the keys is necessary |
||
364 | * |
||
365 | * @see Psi::toArray() |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | 3 | public function toKeyValueArray() |
|
373 | |||
374 | /** |
||
375 | * @param BinaryFunctionInterface|Callable $keyMapper Maps the value down to a string or number |
||
376 | * @param BinaryFunctionInterface|Callable $valueMapper If not given the value will not be changed |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | public function toMap($keyMapper, $valueMapper = null) |
||
381 | { |
||
382 | return $this->solveOperationsAndApplyTerminal( |
||
383 | new CollectToMapOperation( |
||
384 | $keyMapper, |
||
385 | $valueMapper ? $valueMapper : new IdentityMapper() |
||
386 | ) |
||
387 | ); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param string $delimiter |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | 1 | public function join($delimiter) |
|
399 | |||
400 | /** |
||
401 | * @param mixed $default |
||
402 | * |
||
403 | * @return mixed |
||
404 | */ |
||
405 | 2 | public function getFirst($default = null) |
|
409 | |||
410 | /** |
||
411 | * @return float |
||
412 | */ |
||
413 | 1 | public function min() |
|
417 | |||
418 | /** |
||
419 | * @return float |
||
420 | */ |
||
421 | 1 | public function max() |
|
425 | |||
426 | /** |
||
427 | * @return float |
||
428 | */ |
||
429 | 2 | public function sum() |
|
433 | |||
434 | /** |
||
435 | * @return float |
||
436 | */ |
||
437 | 2 | public function avg() |
|
441 | |||
442 | /** |
||
443 | * @return int |
||
444 | */ |
||
445 | 1 | public function count() |
|
449 | |||
450 | //// PRIVATE METHODS ///////////////////////////////////////////////////////////////////////////////////////////// |
||
451 | |||
452 | /** |
||
453 | * @param TerminalOperationInterface $terminal |
||
454 | * |
||
455 | * @return mixed |
||
456 | */ |
||
457 | 70 | private function solveOperationsAndApplyTerminal(TerminalOperationInterface $terminal) |
|
472 | } |
||
473 |