1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of NodalFlow. |
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow |
6
|
|
|
* This source file is licensed under the MIT license which you will |
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fab2s\NodalFlow\Nodes; |
11
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\Flows\FlowInterface; |
13
|
|
|
use fab2s\NodalFlow\NodalFlow; |
14
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
15
|
|
|
use Generator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* class AggregateNode |
19
|
|
|
*/ |
20
|
|
|
class AggregateNode extends PayloadNodeAbstract implements AggregateNodeInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The underlying pseudo Flow |
24
|
|
|
* |
25
|
|
|
* @var FlowInterface |
26
|
|
|
*/ |
27
|
|
|
protected $payload; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Instantiate an Aggregate Node |
31
|
|
|
* |
32
|
|
|
* @param bool $isAReturningVal |
33
|
|
|
* |
34
|
|
|
* @throws NodalFlowException |
35
|
|
|
*/ |
36
|
|
|
public function __construct(bool $isAReturningVal) |
37
|
|
|
{ |
38
|
|
|
parent::__construct(new NodalFlow, $isAReturningVal); |
39
|
|
|
$this->isATraversable = true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add a traversable to the aggregate |
44
|
|
|
* |
45
|
|
|
* @param TraversableNodeInterface $node |
46
|
|
|
* |
47
|
|
|
* @throws NodalFlowException |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function addTraversable(TraversableNodeInterface $node): AggregateNodeInterface |
52
|
|
|
{ |
53
|
|
|
$this->payload->add($node); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the traversable to traverse within the Flow |
60
|
|
|
* |
61
|
|
|
* @param mixed $param |
62
|
|
|
* |
63
|
|
|
* @return Generator |
64
|
|
|
*/ |
65
|
|
|
public function getTraversable($param = null): iterable |
66
|
|
|
{ |
67
|
|
|
$value = null; |
68
|
|
|
/** @var $nodes TraversableNodeInterface[] */ |
69
|
|
|
$nodes = $this->payload->getNodes(); |
70
|
|
|
foreach ($nodes as $node) { |
71
|
|
|
$returnVal = $node->isReturningVal(); |
72
|
|
|
foreach ($node->getTraversable($param) as $value) { |
73
|
|
|
if ($returnVal) { |
74
|
|
|
yield $value; |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
yield $param; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($returnVal) { |
82
|
|
|
// since this node is returning something |
83
|
|
|
// we will pass its last yield to the next |
84
|
|
|
// traversable. It will be up to him to |
85
|
|
|
// do whatever is necessary with it, including |
86
|
|
|
// nothing |
87
|
|
|
$param = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Execute the BranchNode |
94
|
|
|
* |
95
|
|
|
* @param mixed $param |
96
|
|
|
* |
97
|
|
|
* @throws NodalFlowException |
98
|
|
|
*/ |
99
|
|
|
public function exec($param = null) |
100
|
|
|
{ |
101
|
|
|
throw new NodalFlowException('AggregateNode cannot be executed, use getTraversable to iterate instead'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|