|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\GraphTools\Structs; |
|
4
|
|
|
|
|
5
|
|
|
use Smoren\GraphTools\Models\Interfaces\EdgeInterface; |
|
6
|
|
|
use Smoren\GraphTools\Models\Interfaces\VertexInterface; |
|
7
|
|
|
use Smoren\GraphTools\Store\Interfaces\GraphRepositoryInterface; |
|
8
|
|
|
use Smoren\GraphTools\Structs\Interfaces\TraverseBranchContextInterface; |
|
9
|
|
|
use Smoren\GraphTools\Structs\Interfaces\TraverseContextInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Traverse context implementation |
|
13
|
|
|
* @author Smoren <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class TraverseContext implements TraverseContextInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var VertexInterface current vertex |
|
19
|
|
|
*/ |
|
20
|
|
|
protected VertexInterface $vertex; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var EdgeInterface|null edge which led to current vertex |
|
23
|
|
|
*/ |
|
24
|
|
|
protected ?EdgeInterface $edge; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var GraphRepositoryInterface graph repository |
|
27
|
|
|
*/ |
|
28
|
|
|
protected GraphRepositoryInterface $repository; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var TraverseBranchContextInterface current branch context |
|
31
|
|
|
*/ |
|
32
|
|
|
protected TraverseBranchContextInterface $branchContext; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array<string, VertexInterface> passed vertexes map |
|
35
|
|
|
*/ |
|
36
|
|
|
protected array $passedVertexesMap; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array<string, VertexInterface> passed vertexes map of all the branches |
|
39
|
|
|
*/ |
|
40
|
|
|
protected array $globalPassedVertexesMap; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* TraverseContext constructor |
|
44
|
|
|
* @param VertexInterface $vertex current vertex |
|
45
|
|
|
* @param EdgeInterface|null $edge edge which led to current vertex |
|
46
|
|
|
* @param GraphRepositoryInterface $repository graph repository |
|
47
|
|
|
* @param TraverseBranchContextInterface $branchContext current branch context |
|
48
|
|
|
* @param array<string, VertexInterface> $passedVertexesMap passed vertexes map |
|
49
|
|
|
* @param array<string, VertexInterface> $globalPassedVertexesMap passed vertexes map of all the branches |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
VertexInterface $vertex, |
|
53
|
|
|
?EdgeInterface $edge, |
|
54
|
|
|
GraphRepositoryInterface $repository, |
|
55
|
|
|
TraverseBranchContextInterface $branchContext, |
|
56
|
|
|
array $passedVertexesMap, |
|
57
|
|
|
array &$globalPassedVertexesMap |
|
58
|
|
|
) { |
|
59
|
|
|
$this->vertex = $vertex; |
|
60
|
|
|
$this->edge = $edge; |
|
61
|
|
|
$this->repository = $repository; |
|
62
|
|
|
$this->branchContext = $branchContext; |
|
63
|
|
|
$this->passedVertexesMap = $passedVertexesMap; |
|
64
|
|
|
$this->globalPassedVertexesMap = &$globalPassedVertexesMap; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getVertex(): VertexInterface |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->vertex; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getPrevVertex(): ?VertexInterface |
|
79
|
|
|
{ |
|
80
|
|
|
$candidate = end($this->passedVertexesMap); |
|
81
|
|
|
return $candidate ?: null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getEdge(): ?EdgeInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->edge; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritDoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getRepository(): GraphRepositoryInterface |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->repository; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getBranchContext(): TraverseBranchContextInterface |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->branchContext; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getPassedVertexesMap(): array |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->passedVertexesMap; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritDoc |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getGlobalPassedVertexesMap(): array |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->globalPassedVertexesMap; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritDoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isLoop(): bool |
|
128
|
|
|
{ |
|
129
|
|
|
return isset($this->passedVertexesMap[$this->vertex->getId()]); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|