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 preg_replace; |
18
|
|
|
use function strstr; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class representing a single GraphViz attribute. |
22
|
|
|
* |
23
|
|
|
* @link http://phpdoc.org |
24
|
|
|
*/ |
25
|
|
|
class Attribute |
26
|
|
|
{ |
27
|
|
|
/** @var string The name of this attribute */ |
28
|
|
|
protected $key = ''; |
29
|
|
|
|
30
|
|
|
/** @var string The value of this attribute */ |
31
|
|
|
protected $value = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creating a new attribute. |
35
|
|
|
* |
36
|
|
|
* @param string $key Id for the new attribute. |
37
|
|
|
* @param string $value Value for this attribute, |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(string $key, string $value) |
40
|
|
|
{ |
41
|
1 |
|
$this->key = $key; |
42
|
1 |
|
$this->value = $value; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets the key for this attribute. |
47
|
|
|
* |
48
|
|
|
* @param string $key The new name of this attribute. |
49
|
|
|
*/ |
50
|
1 |
|
public function setKey(string $key) : self |
51
|
|
|
{ |
52
|
1 |
|
$this->key = $key; |
53
|
|
|
|
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the name for this attribute. |
59
|
|
|
*/ |
60
|
1 |
|
public function getKey() : string |
61
|
|
|
{ |
62
|
1 |
|
return $this->key; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the value for this attribute. |
67
|
|
|
* |
68
|
|
|
* @param string $value The new value. |
69
|
|
|
*/ |
70
|
1 |
|
public function setValue(string $value) : self |
71
|
|
|
{ |
72
|
1 |
|
$this->value = $value; |
73
|
|
|
|
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the value for this attribute. |
79
|
|
|
*/ |
80
|
1 |
|
public function getValue() : string |
81
|
|
|
{ |
82
|
1 |
|
return $this->value; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the attribute definition as is requested by GraphViz. |
87
|
|
|
*/ |
88
|
2 |
|
public function __toString() : string |
89
|
|
|
{ |
90
|
2 |
|
$key = $this->getKey(); |
91
|
2 |
|
if ($key === 'url') { |
92
|
1 |
|
$key = 'URL'; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$value = $this->getValue(); |
96
|
2 |
|
if ($this->isValueContainingSpecials()) { |
97
|
1 |
|
$value = '"' . $this->encodeSpecials() . '"'; |
98
|
1 |
|
} elseif (!$this->isValueInHtml()) { |
99
|
1 |
|
$value = '"' . addslashes($value) . '"'; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $key . '=' . $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns whether the value contains HTML. |
107
|
|
|
*/ |
108
|
1 |
|
public function isValueInHtml() : bool |
109
|
|
|
{ |
110
|
1 |
|
$value = $this->getValue(); |
111
|
|
|
|
112
|
1 |
|
return isset($value[0]) && ($value[0] === '<'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks whether the value contains any any special characters needing escaping. |
117
|
|
|
*/ |
118
|
1 |
|
public function isValueContainingSpecials() : bool |
119
|
|
|
{ |
120
|
1 |
|
return strstr($this->getValue(), '\\') !== false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Encode special characters so the escape sequences aren't removed |
125
|
|
|
* |
126
|
|
|
* @see http://www.graphviz.org/doc/info/attrs.html#k:escString |
127
|
|
|
*/ |
128
|
1 |
|
protected function encodeSpecials() : string |
129
|
|
|
{ |
130
|
1 |
|
$value = $this->getValue(); |
131
|
1 |
|
$regex = '(\'|"|\\x00|\\\\(?![\\\\NGETHLnlr]))'; |
132
|
|
|
|
133
|
1 |
|
return (string) preg_replace($regex, '\\\\$0', $value); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|