|
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 function addslashes; |
|
17
|
|
|
use function implode; |
|
18
|
|
|
use function strtolower; |
|
19
|
|
|
use function substr; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class representing a node / element in a graph. |
|
23
|
|
|
* |
|
24
|
|
|
* @link http://phpdoc.org |
|
25
|
|
|
* |
|
26
|
|
|
* @method void setLabel(string $name) Sets the label for this node. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Node |
|
29
|
|
|
{ |
|
30
|
|
|
use Attributes; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string Name for this node */ |
|
33
|
|
|
protected $name = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a new node with name and optional label. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name Name of the new node. |
|
39
|
|
|
* @param string|null $label Optional label text. |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function __construct(string $name, ?string $label = null) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->setName($name); |
|
44
|
1 |
|
if ($label === null) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$this->setLabel($label); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Factory method used to assist with fluent interface handling. |
|
53
|
|
|
* |
|
54
|
|
|
* See the examples for more details. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name Name of the new node. |
|
57
|
|
|
* @param string|null $label Optional label text. |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function create(string $name, ?string $label = null) : self |
|
60
|
|
|
{ |
|
61
|
1 |
|
return new self($name, $label); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the name for this node. |
|
66
|
|
|
* |
|
67
|
|
|
* Not to confuse with the label. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name Name for this node. |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function setName(string $name) : self |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->name = $name; |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the name for this node. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function getName() : string |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->name; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Magic method to provide a getter/setter to add attributes on the Node. |
|
88
|
|
|
* |
|
89
|
|
|
* Using this method we make sure that we support any attribute without |
|
90
|
|
|
* too much hassle. If the name for this method does not start with get or |
|
91
|
|
|
* set we return null. |
|
92
|
|
|
* |
|
93
|
|
|
* Set methods return this graph (fluent interface) whilst get methods |
|
94
|
|
|
* return the attribute value. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name Method name; either getX or setX is expected. |
|
97
|
|
|
* @param mixed[] $arguments List of arguments; only 1 is expected for setX. |
|
98
|
|
|
* |
|
99
|
|
|
* @return Attribute|Node|null |
|
100
|
|
|
* |
|
101
|
|
|
* @throws AttributeNotFound |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function __call(string $name, array $arguments) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$key = strtolower(substr($name, 3)); |
|
106
|
1 |
|
if (strtolower(substr($name, 0, 3)) === 'set') { |
|
107
|
1 |
|
return $this->setAttribute($key, (string) $arguments[0]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
if (strtolower(substr($name, 0, 3)) === 'get') { |
|
111
|
1 |
|
return $this->getAttribute($key); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the node definition as is requested by GraphViz. |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function __toString() : string |
|
121
|
|
|
{ |
|
122
|
2 |
|
$attributes = []; |
|
123
|
2 |
|
foreach ($this->attributes as $value) { |
|
124
|
2 |
|
$attributes[] = (string) $value; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
$attributes = implode("\n", $attributes); |
|
128
|
|
|
|
|
129
|
2 |
|
$name = addslashes($this->getName()); |
|
130
|
|
|
|
|
131
|
|
|
return <<<DOT |
|
132
|
2 |
|
"{$name}" [ |
|
133
|
2 |
|
${attributes} |
|
134
|
|
|
] |
|
135
|
|
|
DOT; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|