1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* phpDocumentor |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\GraphViz; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\GraphViz\Contract\AttributesAwareInterface; |
17
|
|
|
use phpDocumentor\GraphViz\Contract\GraphAwareInterface; |
18
|
|
|
use function addslashes; |
19
|
|
|
use function implode; |
20
|
|
|
use function strtolower; |
21
|
|
|
use function substr; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class representing an edge (arrow, line). |
25
|
|
|
* |
26
|
|
|
* @link http://phpdoc.org |
27
|
|
|
*/ |
28
|
|
|
class Edge implements AttributesAwareInterface |
29
|
|
|
{ |
30
|
|
|
use AttributesAware; |
31
|
|
|
|
32
|
|
|
/** @var Node Node from where to link */ |
33
|
|
|
private $from; |
34
|
|
|
|
35
|
|
|
/** @var Node Node where to to link */ |
36
|
|
|
private $to; |
37
|
|
|
|
38
|
|
|
/** @var int The direction */ |
39
|
|
|
private $directed; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates a new Edge / Link between the given nodes. |
43
|
1 |
|
* |
44
|
|
|
* @param Node $from Starting node to create an Edge from. |
45
|
1 |
|
* @param Node $to Destination node where to create and |
46
|
1 |
|
* edge to. |
47
|
1 |
|
* @param int $direction |
48
|
|
|
* 0 means undirected, greater than 0 means from FROM to TO, smaller than 0 |
49
|
|
|
* means from TO to FROM. |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Node $from, Node $to, int $direction = 1) |
52
|
|
|
{ |
53
|
|
|
if ($direction >= 0) { |
54
|
|
|
$this->from = $from; |
55
|
|
|
$this->to = $to; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
if ($direction < 0) { |
59
|
|
|
$this->from = $to; |
60
|
1 |
|
$this->to = $from; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->directed = ($direction ** 2) > 0; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
/** |
67
|
|
|
* @return bool |
68
|
1 |
|
*/ |
69
|
|
|
public function isDirected(): bool |
70
|
|
|
{ |
71
|
|
|
return $this->directed; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
/** |
75
|
|
|
* Factory method used to assist with fluent interface handling. |
76
|
1 |
|
* |
77
|
|
|
* See the examples for more details. |
78
|
|
|
* |
79
|
|
|
* @param Node $from Starting node to create an Edge from. |
80
|
|
|
* @param Node $to Destination node where to create and |
81
|
|
|
* edge to. |
82
|
|
|
* @param int $direction |
83
|
|
|
* 0 means undirected, greater than 0 means from FROM to TO, smaller than 0 |
84
|
|
|
* means from TO to FROM. |
85
|
|
|
* |
86
|
|
|
* @return \phpDocumentor\GraphViz\Edge |
87
|
|
|
*/ |
88
|
|
|
public static function create(Node $from, Node $to, int $direction = 1) : self |
89
|
|
|
{ |
90
|
|
|
return new self($from, $to, $direction); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \phpDocumentor\GraphViz\Node $from |
95
|
|
|
* @param \phpDocumentor\GraphViz\Node $to |
96
|
|
|
* |
97
|
1 |
|
* @return \phpDocumentor\GraphViz\Edge |
98
|
|
|
*/ |
99
|
1 |
|
public static function directed(Node $from, Node $to) : self |
100
|
1 |
|
{ |
101
|
1 |
|
return new self($from, $to, 1); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
/** |
105
|
1 |
|
* @param \phpDocumentor\GraphViz\Node $from |
106
|
|
|
* @param \phpDocumentor\GraphViz\Node $to |
107
|
|
|
* |
108
|
1 |
|
* @return \phpDocumentor\GraphViz\Edge |
109
|
|
|
*/ |
110
|
|
|
public static function undirected(Node $from, Node $to) : self |
111
|
|
|
{ |
112
|
|
|
return new self($from, $to, 0); |
113
|
|
|
} |
114
|
1 |
|
|
115
|
|
|
/** |
116
|
1 |
|
* Returns the source Node for this Edge. |
117
|
1 |
|
*/ |
118
|
1 |
|
public function getFrom() : Node |
119
|
|
|
{ |
120
|
|
|
return $this->from; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
/** |
124
|
1 |
|
* Returns the destination Node for this Edge. |
125
|
|
|
*/ |
126
|
|
|
public function getTo() : Node |
127
|
1 |
|
{ |
128
|
1 |
|
return $this->to; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Magic method to provide a getter/setter to add attributes on the edge. |
133
|
|
|
* |
134
|
|
|
* Using this method we make sure that we support any attribute without too |
135
|
|
|
* much hassle. If the name for this method does not start with get or set |
136
|
|
|
* we return null. |
137
|
|
|
* |
138
|
|
|
* Set methods return this graph (fluent interface) whilst get methods |
139
|
|
|
* return the attribute value. |
140
|
|
|
* |
141
|
|
|
* @param string $name name of the invoked method, expect it to be |
142
|
|
|
* setX or getX. |
143
|
|
|
* @param mixed[] $arguments Arguments for the setter, only 1 is expected: value |
144
|
|
|
* |
145
|
|
|
* @return Attribute|Edge|null |
146
|
|
|
* |
147
|
|
|
* @throws AttributeNotFound |
148
|
|
|
*/ |
149
|
|
|
public function __call(string $name, array $arguments) |
150
|
|
|
{ |
151
|
|
|
$key = strtolower(substr($name, 3)); |
152
|
|
|
|
153
|
|
|
if (stripos($name, 'set') === 0) { |
154
|
|
|
return $this->setAttribute($key, (string) $arguments[0]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (stripos($name, 'get') === 0) { |
158
|
|
|
return $this->getAttribute($key); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns the edge definition as is requested by GraphViz. |
166
|
|
|
*/ |
167
|
|
|
public function __toString() : string |
168
|
|
|
{ |
169
|
|
|
$attributes = []; |
170
|
|
|
foreach ($this->attributes as $value) { |
171
|
|
|
$attributes[] = (string) $value; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$attributes = implode("\n", $attributes); |
175
|
|
|
|
176
|
|
|
$from_name = addslashes($this->getFrom()->getName()); |
177
|
|
|
$to_name = addslashes($this->getTo()->getName()); |
178
|
|
|
|
179
|
|
|
$direction = $this->isDirected() ? |
180
|
|
|
'->' : |
181
|
|
|
'--'; |
182
|
|
|
|
183
|
|
|
return <<<DOT |
184
|
|
|
"${from_name}" ${direction} "${to_name}" [ |
185
|
|
|
${attributes} |
186
|
|
|
] |
187
|
|
|
DOT; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.