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\FlowIdTrait; |
13
|
|
|
use fab2s\NodalFlow\Flows\FlowInterface; |
14
|
|
|
use fab2s\NodalFlow\Flows\FlowRegistry; |
15
|
|
|
use fab2s\NodalFlow\Flows\FlowRegistryInterface; |
16
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* abstract Class NodeAbstract |
20
|
|
|
*/ |
21
|
|
|
abstract class NodeAbstract implements NodeInterface |
22
|
|
|
{ |
23
|
|
|
use FlowIdTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The carrying Flow |
27
|
|
|
* |
28
|
|
|
* @var FlowInterface |
29
|
|
|
*/ |
30
|
|
|
public $carrier; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Indicate if this Node is traversable |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $isATraversable; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Indicate if this Node is returning a value |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $isAReturningVal; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Indicate if this Node is a Flow (Branch) |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $isAFlow; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var FlowRegistryInterface |
55
|
|
|
*/ |
56
|
|
|
protected $registry; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $nodeIncrements = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* NodeAbstract constructor. |
65
|
|
|
* |
66
|
|
|
* @throws NodalFlowException |
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
$this->enforceIsATraversable(); |
71
|
|
|
$this->registry = new FlowRegistry; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Indicate if this Node is Traversable |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isTraversable(): bool |
80
|
|
|
{ |
81
|
|
|
return (bool) $this->isATraversable; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Indicate if this Node is a Flow (Branch) |
86
|
|
|
* |
87
|
|
|
* @return bool true if this node instanceof FlowInterface |
88
|
|
|
*/ |
89
|
|
|
public function isFlow(): bool |
90
|
|
|
{ |
91
|
|
|
return (bool) $this->isAFlow; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Indicate if this Node is returning a value |
96
|
|
|
* |
97
|
|
|
* @return bool true if this node is expected to return |
98
|
|
|
* something to pass on next node as param. |
99
|
|
|
* If nothing is returned, the previously |
100
|
|
|
* returned value will be use as param |
101
|
|
|
* for next nodes. |
102
|
|
|
*/ |
103
|
|
|
public function isReturningVal(): bool |
104
|
|
|
{ |
105
|
|
|
return (bool) $this->isAReturningVal; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set/Reset carrying Flow |
110
|
|
|
* |
111
|
|
|
* @param FlowInterface|null $flow |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setCarrier(FlowInterface $flow = null): NodeInterface |
116
|
|
|
{ |
117
|
|
|
$this->carrier = $flow; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get carrying Flow |
124
|
|
|
* |
125
|
|
|
* @return FlowInterface |
126
|
|
|
*/ |
127
|
|
|
public function getCarrier(): ? FlowInterface |
128
|
|
|
{ |
129
|
|
|
return $this->carrier; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get this Node's hash, must be deterministic and unique |
134
|
|
|
* |
135
|
|
|
* @throws \Exception |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
* |
139
|
|
|
* @deprecated use `getId` instead |
140
|
|
|
*/ |
141
|
|
|
public function getNodeHash(): string |
142
|
|
|
{ |
143
|
|
|
return $this->getId(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the custom Node increments to be considered during |
148
|
|
|
* Flow execution |
149
|
|
|
* To set additional increment keys, use : |
150
|
|
|
* 'keyName' => int |
151
|
|
|
* to add keyName as increment, starting at int |
152
|
|
|
* or : |
153
|
|
|
* 'keyName' => 'existingIncrement' |
154
|
|
|
* to assign keyName as a reference to an existingIncrement |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function getNodeIncrements(): array |
159
|
|
|
{ |
160
|
|
|
return $this->nodeIncrements; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $flowId |
165
|
|
|
* @param string|null $nodeId |
166
|
|
|
* @param mixed|null $param |
167
|
|
|
* |
168
|
|
|
* @throws NodalFlowException |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
|
public function sendTo(string $flowId, string $nodeId = null, $param = null) |
173
|
|
|
{ |
174
|
|
|
if (!($flow = $this->registry->getFlow($flowId))) { |
175
|
|
|
throw new NodalFlowException('Cannot sendTo without valid Flow target', 1, null, [ |
176
|
|
|
'flowId' => $flowId, |
177
|
|
|
'nodeId' => $nodeId, |
178
|
|
|
]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $flow->sendTo($nodeId, $param); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Make sure this Node is consistent |
186
|
|
|
* |
187
|
|
|
* @throws NodalFlowException |
188
|
|
|
* |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
protected function enforceIsATraversable(): self |
192
|
|
|
{ |
193
|
|
|
if ($this->isFlow()) { |
194
|
|
|
if ($this->isATraversable) { |
195
|
|
|
throw new NodalFlowException('Cannot Traverse a Branch'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if ($this->isATraversable) { |
202
|
|
|
if (!($this instanceof TraversableNodeInterface)) { |
203
|
|
|
throw new NodalFlowException('Cannot Traverse a Node that does not implement TraversableNodeInterface'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (!($this instanceof ExecNodeInterface)) { |
210
|
|
|
throw new NodalFlowException('Cannot Exec a Node that does not implement ExecNodeInterface'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|