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