|
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\Flows; |
|
11
|
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\Nodes\NodeInterface; |
|
13
|
|
|
use fab2s\NodalFlow\Nodes\TraversableNodeInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Abstract Class FlowAncestryAbstract |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class FlowAncestryAbstract implements FlowInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The underlying node structure |
|
22
|
|
|
* |
|
23
|
|
|
* @var TraversableNodeInterface|NodeInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $nodes = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The current Node index, being the next slot in $this->nodes for |
|
29
|
|
|
* node additions and the current node index when executing the flow |
|
30
|
|
|
* |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $nodeIdx = 0; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The last index value |
|
37
|
|
|
* |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $lastIdx = 0; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The parent Flow, only set when branched |
|
44
|
|
|
* |
|
45
|
|
|
* @var FlowInterface|null |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $parent; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set parent Flow, happens only when branched |
|
51
|
|
|
* |
|
52
|
|
|
* @param FlowInterface $flow |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setParent(FlowInterface $flow): FlowInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$this->parent = $flow; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get eventual parent Flow |
|
65
|
|
|
* |
|
66
|
|
|
* @return FlowInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getParent(): FlowInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->parent; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Tells if this flow has a parent |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function hasParent(): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return isset($this->parent); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get this Flow's root Flow |
|
85
|
|
|
* |
|
86
|
|
|
* @param FlowInterface $flow Root Flow, or self if root flow |
|
87
|
|
|
* |
|
88
|
|
|
* @return FlowInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRootFlow(FlowInterface $flow): FlowInterface |
|
91
|
|
|
{ |
|
92
|
|
|
while ($flow->hasParent()) { |
|
93
|
|
|
$flow = $flow->getParent(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $flow; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|