1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Grafizzi\Graph\AbstractNamed: a component of the Grafizzi library. |
6
|
|
|
* |
7
|
|
|
* (c) 2012 Frédéric G. MARAND <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Grafizzi is free software: you can redistribute it and/or modify it under the |
10
|
|
|
* terms of the GNU Lesser General Public License as published by the Free |
11
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
12
|
|
|
* later version. |
13
|
|
|
* |
14
|
|
|
* Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY |
15
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
16
|
|
|
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
17
|
|
|
* details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
20
|
|
|
* along with Grafizzi, in the COPYING.LESSER.txt file. If not, see |
21
|
|
|
* <http://www.gnu.org/licenses/> |
22
|
|
|
*/ |
23
|
|
|
namespace Grafizzi\Graph; |
24
|
|
|
|
25
|
|
|
use Pimple\Container; |
26
|
|
|
|
27
|
|
|
abstract class AbstractNamed implements NamedInterface { |
28
|
|
|
|
29
|
|
|
public $fName = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A shortcut to the injected logger. |
33
|
|
|
* |
34
|
|
|
* @var \Psr\Log\LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
public $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Pimple\Container |
40
|
|
|
*/ |
41
|
|
|
protected $dic; |
42
|
|
|
|
43
|
71 |
|
function __construct(Container $dic) { |
|
|
|
|
44
|
71 |
|
$this->dic = $dic; |
45
|
71 |
|
$this->logger = &$dic['logger']; |
46
|
71 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Will also handle numbers and booleans, and any object with a __toString(). |
50
|
|
|
* |
51
|
|
|
* @param string $string |
52
|
|
|
* The string to escape. |
53
|
|
|
* @param boolean $pseudoHtml |
54
|
|
|
* Wrap GraphViz-style pseudo-HTML text. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
51 |
|
public static function escape($string, $pseudoHtml = false) { |
59
|
|
|
$keywords = array( |
60
|
51 |
|
'digraph', |
61
|
|
|
'edge', |
62
|
|
|
'graph', |
63
|
|
|
'node', |
64
|
|
|
'strict', |
65
|
|
|
'subgraph', |
66
|
|
|
); |
67
|
|
|
|
68
|
51 |
|
$wrapping = false; |
69
|
|
|
|
70
|
|
|
// 1. Handle keywords specifically, convert anything else to string. |
71
|
51 |
|
$s = trim(strtolower($string)); |
72
|
51 |
|
if (in_array($s, $keywords)) { |
73
|
4 |
|
$wrapping = 'dquote'; |
74
|
4 |
|
$s = $string; |
75
|
50 |
|
} elseif (!isset($string)) { |
76
|
|
|
$s = 'false'; |
77
|
50 |
|
} elseif (is_bool($string)) { |
78
|
2 |
|
$s = $string ? 'true' : 'false'; |
79
|
|
|
} else { |
80
|
50 |
|
$s = (string) $string; |
81
|
50 |
|
if (!self::validateId($s)) { |
82
|
16 |
|
$wrapping = 'dquote'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// 2. Wrap requested pseudo-html if it contains at least one terminated element. |
87
|
51 |
|
if ($pseudoHtml && (strpos($s, '</') !== false || strpos($s, '/>') !== false)) { |
88
|
3 |
|
$wrapping = 'html'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// 3. Normalize quotes and new lines |
92
|
51 |
|
if ($wrapping != 'html') { |
93
|
51 |
|
$s = str_replace(array("\r\n", "\n", "\r", '"'), |
94
|
51 |
|
array('\n', '\n', '\n', '\"'), $s); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// 4. Wrap in double quotes if needed. |
98
|
51 |
|
switch ($wrapping) { |
99
|
51 |
|
case 'dquote': |
100
|
18 |
|
$s = '"' . $s . '"'; |
101
|
18 |
|
break; |
102
|
|
|
|
103
|
49 |
|
case 'html': |
104
|
3 |
|
$s = "<$s>"; |
105
|
3 |
|
break; |
106
|
|
|
|
107
|
|
|
default: |
108
|
|
|
// Do not wrap. |
109
|
|
|
} |
110
|
|
|
|
111
|
51 |
|
return $s; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Helper for escape(). Validate non-quoted id. |
116
|
|
|
* |
117
|
|
|
* @see escape() |
118
|
|
|
* |
119
|
|
|
* @param string $id |
120
|
|
|
* |
121
|
|
|
* @return boolean |
122
|
|
|
*/ |
123
|
50 |
|
protected static function validateId($id) { |
124
|
50 |
|
$regex = '^([a-z_][a-z_0-9]*|-?(\.[0-9]+|[0-9]+(\.[0-9]*)?))$'; |
125
|
50 |
|
$ret = preg_match("/$regex/i", $id) ? true : false; |
126
|
50 |
|
return $ret; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @see NamedInterface::getBuildName() |
131
|
|
|
*/ |
132
|
38 |
|
public function getBuildName() { |
133
|
38 |
|
return $this->escape($this->getName()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see NamedInterface::getName() |
138
|
|
|
* |
139
|
|
|
* @throws AttributeNameException |
140
|
|
|
*/ |
141
|
71 |
|
public function getName() { |
142
|
71 |
|
if (!isset($this->fName)) { |
143
|
|
|
$message = 'Getting name for unnamed object.'; |
144
|
|
|
$this->logger->error($message); |
145
|
|
|
throw new AttributeNameException($message); |
146
|
|
|
} |
147
|
71 |
|
return $this->fName; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @see NamedInterface::setName() |
152
|
|
|
*/ |
153
|
71 |
|
public function setName($name) { |
154
|
71 |
|
$this->logger->debug($this->getType() . " attribute name set to $name."); |
155
|
71 |
|
$this->fName = $name; |
156
|
71 |
|
} |
157
|
|
|
} |
158
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.