1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace graphp\graph; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* class GraphTypeBuilder |
9
|
|
|
* |
10
|
|
|
* @package graphp\graph |
11
|
|
|
*/ |
12
|
|
|
class GraphTypeBuilder |
13
|
|
|
{ |
14
|
|
|
private $directed; |
15
|
|
|
private $undirected; |
16
|
|
|
private $allowSelfLoops; |
17
|
|
|
private $allowMultipleEdges; |
18
|
|
|
private $weighted; |
19
|
|
|
private $allowCycles; |
20
|
|
|
private $modifiable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct a new graph type |
24
|
|
|
* |
25
|
|
|
* @param GraphTypeInterface $type - the graph type |
26
|
|
|
* @param bool $directed - is graph directed |
27
|
|
|
* @param bool $undirected - is graph undirected |
28
|
|
|
*/ |
29
|
4 |
|
public function __construct(?GraphTypeInterface $type = null, ?bool $directed = null, ?bool $undirected = null) |
30
|
|
|
{ |
31
|
4 |
|
if (!is_null($type)) { |
32
|
|
|
$this->directed = $type->isDirected() || $type->isMixed(); |
33
|
|
|
$this->undirected = $type->isUndirected() || $type->isMixed(); |
34
|
|
|
$this->allowSelfLoops = $type->isAllowingSelfLoops(); |
35
|
|
|
$this->allowMultipleEdges = $type->isAllowingMultipleEdges(); |
36
|
|
|
$this->weighted = $type->isWeighted(); |
37
|
|
|
$this->allowCycles = $type->isAllowingCycles(); |
38
|
|
|
$this->modifiable = $type->isModifiable(); |
39
|
4 |
|
} elseif (!is_null($directed) && !is_null($undirected)) { |
40
|
|
|
if (!$directed && !$undirected) { |
41
|
|
|
throw new InvalidArgumentException("At least one of directed or undirected must be true"); |
42
|
|
|
} |
43
|
|
|
$this->directed = $directed; |
44
|
|
|
$this->undirected = $undirected; |
45
|
|
|
$this->allowSelfLoops = true; |
46
|
|
|
$this->allowMultipleEdges = true; |
47
|
|
|
$this->weighted = false; |
48
|
|
|
$this->allowCycles = true; |
49
|
|
|
$this->modifiable = true; |
50
|
|
|
} else { |
51
|
4 |
|
$this->directed = false; |
52
|
4 |
|
$this->undirected = true; |
53
|
4 |
|
$this->allowSelfLoops = true; |
54
|
4 |
|
$this->allowMultipleEdges = true; |
55
|
4 |
|
$this->weighted = false; |
56
|
4 |
|
$this->allowCycles = true; |
57
|
4 |
|
$this->modifiable = true; |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the type as directed. |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
4 |
|
public function directed(): self |
67
|
|
|
{ |
68
|
4 |
|
$this->directed = true; |
69
|
4 |
|
$this->undirected = false; |
70
|
4 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the type as undirected. |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function undirected(): self |
79
|
|
|
{ |
80
|
|
|
$this->directed = false; |
81
|
|
|
$this->undirected = true; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the type as mixed. |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
public function mixed(): self |
91
|
|
|
{ |
92
|
|
|
$this->directed = true; |
93
|
|
|
$this->undirected = true; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set whether to allow self-loops. |
99
|
|
|
* |
100
|
|
|
* @param bool $value - if to allow self loops |
101
|
|
|
* |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
4 |
|
public function allowSelfLoops(bool $value): self |
105
|
|
|
{ |
106
|
4 |
|
$this->allowSelfLoops = $value; |
107
|
4 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set whether to allow multiple edges. |
112
|
|
|
* |
113
|
|
|
* @param bool $value - if to allow multiple edges |
114
|
|
|
* |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
4 |
|
public function allowMultipleEdges(bool $value): self |
118
|
|
|
{ |
119
|
4 |
|
$this->allowMultipleEdges = $value; |
120
|
4 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set whether the graph is weighted. |
125
|
|
|
* |
126
|
|
|
* @param bool $value - if graph is weighted |
127
|
|
|
* |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
4 |
|
public function weighted(?bool $value = null): self |
131
|
|
|
{ |
132
|
4 |
|
$this->weighted = $value ?? false; |
133
|
4 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set whether to allow cycles. |
138
|
|
|
* |
139
|
|
|
* @param bool $value - if to allow cycles |
140
|
|
|
* |
141
|
|
|
* @return self |
142
|
|
|
*/ |
143
|
4 |
|
public function allowCycles(bool $value): self |
144
|
|
|
{ |
145
|
4 |
|
$this->allowCycles = $value; |
146
|
4 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set whether the graph is modifiable. |
151
|
|
|
* |
152
|
|
|
* @param bool $value - if graph is modifiable |
153
|
|
|
* |
154
|
|
|
* @return self |
155
|
|
|
*/ |
156
|
|
|
public function modifiable(bool $value): self |
157
|
|
|
{ |
158
|
|
|
$this->modifiable = $value; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Build the graph type |
164
|
|
|
* |
165
|
|
|
* @return GraphTypeInterface |
166
|
|
|
*/ |
167
|
4 |
|
public function build(): GraphTypeInterface |
168
|
|
|
{ |
169
|
4 |
|
return new DefaultGraphType( |
170
|
4 |
|
$this->directed, |
171
|
4 |
|
$this->undirected, |
172
|
4 |
|
$this->allowSelfLoops, |
173
|
4 |
|
$this->allowMultipleEdges, |
174
|
4 |
|
$this->weighted, |
175
|
4 |
|
$this->allowCycles, |
176
|
4 |
|
$this->modifiable |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|