|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ipl\IteratorPipeline; |
|
6
|
|
|
|
|
7
|
|
|
use Ipl as i; |
|
8
|
|
|
use function Jasny\expect_type; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Functional-style operations, such as map-reduce transformations on arrays and iterators. |
|
12
|
|
|
* A pipeline uses Generators, meaning it can be used only once. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Pipeline implements \IteratorAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
use Traits\MappingTrait; |
|
17
|
|
|
use Traits\FilteringTrait; |
|
18
|
|
|
use Traits\SortingTrait; |
|
19
|
|
|
use Traits\FindingTrait; |
|
20
|
|
|
use Traits\AggregationTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var iterable |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $iterable; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Pipeline constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param iterable $iterable |
|
31
|
|
|
*/ |
|
32
|
15 |
|
public function __construct(iterable $iterable) |
|
33
|
|
|
{ |
|
34
|
15 |
|
$this->iterable = $iterable; |
|
35
|
15 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Define the next step via a callback that returns an array or Traversable object. |
|
39
|
|
|
* |
|
40
|
|
|
* @param callable $callback |
|
41
|
|
|
* @param mixed ...$args |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function then(callable $callback, ...$args): self |
|
45
|
|
|
{ |
|
46
|
3 |
|
$next = $callback($this->iterable, ...$args); |
|
47
|
|
|
|
|
48
|
3 |
|
expect_type( |
|
49
|
3 |
|
$next, |
|
50
|
3 |
|
'iterable', |
|
51
|
3 |
|
\UnexpectedValueException::class, |
|
52
|
3 |
|
"Expected an array or Traversable, %s returned" |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
2 |
|
$this->iterable = $next; |
|
56
|
|
|
|
|
57
|
2 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get iterator. |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Iterator |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function getIterator(): \Iterator |
|
67
|
|
|
{ |
|
68
|
4 |
|
return i\iterable_to_iterator($this->iterable); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get iterable as array. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function toArray(): array |
|
77
|
|
|
{ |
|
78
|
4 |
|
return i\iterable_to_array($this->iterable, true); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Traverse over the iterator, not capturing the values. |
|
83
|
|
|
* This is particularly useful after `apply()`. |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function walk(): void |
|
88
|
|
|
{ |
|
89
|
1 |
|
i\iterable_walk($this->iterable); |
|
|
|
|
|
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Factory method |
|
95
|
|
|
* |
|
96
|
|
|
* @param iterable $iterable |
|
97
|
|
|
* @return static |
|
98
|
|
|
*/ |
|
99
|
4 |
|
final public static function with(iterable $iterable): self |
|
100
|
|
|
{ |
|
101
|
4 |
|
return new static($iterable); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Factory method for PipelineBuilder |
|
106
|
|
|
* |
|
107
|
|
|
* @return PipelineBuilder |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public static function build(): PipelineBuilder |
|
110
|
|
|
{ |
|
111
|
1 |
|
return new PipelineBuilder(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|