1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace graphp\graph; |
4
|
|
|
|
5
|
|
|
use graphp\edge\EdgeInterface; |
6
|
|
|
use graphp\vertex\VertexInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class GraphBuilder |
10
|
|
|
* |
11
|
|
|
* @package graphp\graph |
12
|
|
|
*/ |
13
|
|
|
class GraphBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The graph |
17
|
|
|
* |
18
|
|
|
* @var GraphInterface |
19
|
|
|
*/ |
20
|
|
|
private $graph; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct the graph |
24
|
|
|
* |
25
|
|
|
* @param GraphInterface $graph - the graph |
26
|
|
|
*/ |
27
|
|
|
public function __construct(GraphInterface $graph) |
28
|
|
|
{ |
29
|
|
|
$this->graph = $graph; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add a vertex to the graph being built |
34
|
|
|
* |
35
|
|
|
* @param VertexInterface $vertex - the vertex |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
|
|
public function addVertex(VertexInterface $vertex): self |
40
|
|
|
{ |
41
|
|
|
$this->graph->addVertex($vertex); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add vertices to the graph being built |
47
|
|
|
* |
48
|
|
|
* @param VertexInterface $vertex - the vertex |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function addVertices(array $vertices): self |
53
|
|
|
{ |
54
|
|
|
foreach ($vertices as $vertex) { |
55
|
|
|
$this->addVertex($vertex); |
56
|
|
|
} |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add an edge to the graph being built |
62
|
|
|
* |
63
|
|
|
* @param VertexInterface $vertex - the source vertex |
64
|
|
|
* @param VertexInterface $targetVertex - the target vertex |
65
|
|
|
* @param EdgeInterface $edge - the edge to be added |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function addEdge( |
70
|
|
|
VertexInterface $sourceVertex, |
71
|
|
|
VertexInterface $targetVertex, |
72
|
|
|
?EdgeInterface $edge = null, |
73
|
|
|
?float $weight = null |
74
|
|
|
): self { |
75
|
|
|
if (is_null($edge)) { |
76
|
|
|
GraphUtils::addEdgeWithVertices($this->graph, $sourceVertex, $targetVertex, $weight); |
77
|
|
|
} else { |
78
|
|
|
$this->addVertex($sourceVertex); |
79
|
|
|
$this->addVertex($targetVertex); |
80
|
|
|
$this->graph->addEdge($sourceVertex, $targetVertex, $edge); |
81
|
|
|
} |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add an chain of edges to the graph being built |
87
|
|
|
* |
88
|
|
|
* @param VertexInterface $first - the first vertex |
89
|
|
|
* @param VertexInterface $second - the second vertex |
90
|
|
|
* @param mixed $vertices - the remaining vertices |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function addEdgeChain(VertexInterface $first, VertexInterface $second, ...$vertices): self |
95
|
|
|
{ |
96
|
|
|
$this->addEdge($first, $second); |
97
|
|
|
$last = $second; |
98
|
|
|
foreach ($vertices as $vertex) { |
99
|
|
|
$this->addEdge($last, $vertex); |
100
|
|
|
$last = $vertex; |
101
|
|
|
} |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add all the vertices and all the edges of the specified graph to the graph being built |
107
|
|
|
* |
108
|
|
|
* @param GraphInterface $sourceGrap - the graph |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
public function addGraph(GraphInterface $targetGraphp): self |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
GraphUtils::addGraph($this->graph, $sourceGraph); |
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Remove the specified vertex from the graph being built |
120
|
|
|
* |
121
|
|
|
* @param VertexInterface $vertex - the vertex to remove |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
|
|
public function removeVertex(VertexInterface $vertex): self |
126
|
|
|
{ |
127
|
|
|
$this->graph->removeVertex($vertex); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Remove vertices from the graph being built |
133
|
|
|
* |
134
|
|
|
* @param array $vertices - the vertices to remove |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function removeVertices(array $vertices): self |
139
|
|
|
{ |
140
|
|
|
foreach ($vertices as $vertex) { |
141
|
|
|
$this->removeVertex($vertex); |
142
|
|
|
} |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Remove the edge from the graph being built |
148
|
|
|
* |
149
|
|
|
* @param VertexInterface $sourceVertex - the edge source vertex |
150
|
|
|
* @param VertexInterface $tagretVertex - the edge target vertex |
151
|
|
|
* @param EdgeInterface $edge - the edge |
152
|
|
|
* |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
|
|
public function removeEdge( |
156
|
|
|
?VertexInterface $sourceVertex = null, |
157
|
|
|
?VertexInterface $targetVertex = null, |
158
|
|
|
?EdgeInterface $edge = null |
159
|
|
|
): self { |
160
|
|
|
$this->graph->removeEdge($sourceVertex, $targetVertex, $edge); |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the graph being built |
166
|
|
|
* |
167
|
|
|
* @return GraphInterface |
168
|
|
|
*/ |
169
|
|
|
public function build(): GraphInterface |
170
|
|
|
{ |
171
|
|
|
return $this->graph; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.