|
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
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* class AggregateNode |
|
18
|
|
|
*/ |
|
19
|
|
|
class AggregateNode extends PayloadNodeAbstract implements AggregateNodeInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The underlying pseudo Flow |
|
23
|
|
|
* |
|
24
|
|
|
* @var FlowInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $payload; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instantiate an Aggregate Node |
|
30
|
|
|
* |
|
31
|
|
|
* @param bool $isAReturningVal |
|
32
|
|
|
* |
|
33
|
|
|
* @throws NodalFlowException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($isAReturningVal) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct(new NodalFlow, $isAReturningVal); |
|
38
|
|
|
$this->isATraversable = true; |
|
39
|
|
|
$this->isAFlow = false; |
|
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) |
|
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) |
|
66
|
|
|
{ |
|
67
|
|
|
$value = null; |
|
68
|
|
|
/** @var $nodes TraversableNodeInterface[] */ |
|
69
|
|
|
$nodes = $this->payload->getNodes(); |
|
70
|
|
|
foreach ($nodes as $node) { |
|
71
|
|
|
$returnVal = $node->isReturningVal(); |
|
72
|
|
|
$nodeStats = &$node->getCarrier()->getFlowMap()->getNodeStat($node->getId()); |
|
73
|
|
|
foreach ($node->getTraversable($param) as $value) { |
|
74
|
|
|
++$nodeStats['num_iterate']; |
|
75
|
|
|
if ($returnVal) { |
|
76
|
|
|
yield $value; |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
yield $param; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
++$nodeStats['num_exec']; |
|
84
|
|
|
if ($returnVal) { |
|
85
|
|
|
// since this node is returning something |
|
86
|
|
|
// we will pass its last yield to the next |
|
87
|
|
|
// traversable. It will be up to him to |
|
88
|
|
|
// do whatever is necessary with it, including |
|
89
|
|
|
// nothing |
|
90
|
|
|
$param = $value; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|