|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see AppUtils\URLInfo_Highlighter} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Application Utils |
|
6
|
|
|
* @subpackage URLInfo |
|
7
|
|
|
* @see AppUtils\URLInfo_Highlighter |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Handles highlighting a previously parsed URL. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Application Utils |
|
18
|
|
|
* @subpackage URLInfo |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class URLInfo_Highlighter |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var URLInfo |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $info; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(URLInfo $info) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->info = $info; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function highlight() : string |
|
34
|
|
|
{ |
|
35
|
|
|
$method = 'highlight_'.$this->info->getType(); |
|
36
|
|
|
|
|
37
|
|
|
return |
|
38
|
|
|
'<span class="link">'. |
|
39
|
|
|
$this->$method(). |
|
40
|
|
|
'</span>'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function highlight_email() : string |
|
44
|
|
|
{ |
|
45
|
|
|
return sprintf( |
|
46
|
|
|
'<span class="link-scheme scheme-mailto">mailto:</span>'. |
|
47
|
|
|
'<span class="link-host">%s</span>', |
|
48
|
|
|
$this->info->getPath() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function highlight_fragment() : string |
|
53
|
|
|
{ |
|
54
|
|
|
return sprintf( |
|
55
|
|
|
'<span class="link-fragment-sign">#</span>'. |
|
56
|
|
|
'<span class="link-fragment-value">%s</span>', |
|
57
|
|
|
$this->info->getFragment() |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function highlight_phone() : string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->highlight_url(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function highlight_url() : string |
|
67
|
|
|
{ |
|
68
|
|
|
$result = []; |
|
69
|
|
|
|
|
70
|
|
|
$parts = array( |
|
71
|
|
|
'scheme', |
|
72
|
|
|
'username', |
|
73
|
|
|
'host', |
|
74
|
|
|
'port', |
|
75
|
|
|
'path', |
|
76
|
|
|
'params', |
|
77
|
|
|
'fragment' |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
foreach($parts as $part) |
|
81
|
|
|
{ |
|
82
|
|
|
$method = 'render_'.$part; |
|
83
|
|
|
$result[] = (string)$this->$method(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return implode('', $result); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function render_scheme() : string |
|
90
|
|
|
{ |
|
91
|
|
|
if(!$this->info->hasScheme()) { |
|
92
|
|
|
return ''; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return sprintf( |
|
96
|
|
|
'<span class="link-scheme scheme-%1$s">'. |
|
97
|
|
|
'%1$s:'. |
|
98
|
|
|
'</span>'. |
|
99
|
|
|
'<span class="link-component double-slashes">//</span>', |
|
100
|
|
|
$this->info->getScheme() |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function render_username() : string |
|
105
|
|
|
{ |
|
106
|
|
|
if(!$this->info->hasUsername()) { |
|
107
|
|
|
return ''; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return sprintf( |
|
111
|
|
|
'<span class="link-credentials">%s</span>'. |
|
112
|
|
|
'<span class="link-component credentials-separator">:</span>'. |
|
113
|
|
|
'<span class="link-credentials">%s</span>'. |
|
114
|
|
|
'<span class="link-component credentials-at">@</span>', |
|
115
|
|
|
$this->info->getUsername(), |
|
116
|
|
|
$this->info->getPassword() |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function render_host() : string |
|
121
|
|
|
{ |
|
122
|
|
|
if(!$this->info->hasHost()) { |
|
123
|
|
|
return ''; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return sprintf( |
|
127
|
|
|
'<span class="link-host">%s</span><wbr>', |
|
128
|
|
|
$this->info->getHost() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function render_port() : string |
|
133
|
|
|
{ |
|
134
|
|
|
if(!$this->info->hasPort()) { |
|
135
|
|
|
return ''; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return sprintf( |
|
139
|
|
|
'<span class="link-component port-separator">:</span>'. |
|
140
|
|
|
'<span class="link-port">%s</span>', |
|
141
|
|
|
$this->info->getPort() |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
protected function render_path() : string |
|
146
|
|
|
{ |
|
147
|
|
|
if(!$this->info->hasPath()) { |
|
148
|
|
|
return ''; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$path = str_replace(array(';', '='), array(';<wbr>', '=<wbr>'), $this->info->getPath()); |
|
152
|
|
|
$tokens = explode('/', $path); |
|
153
|
|
|
$path = implode('<span class="link-component path-separator">/</span><wbr>', $tokens); |
|
154
|
|
|
|
|
155
|
|
|
return sprintf( |
|
156
|
|
|
'<span class="link-path">%s</span><wbr>', |
|
157
|
|
|
$path |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Fetches all parameters in the URL, regardless of |
|
163
|
|
|
* whether parameter exclusion is enabled, so they |
|
164
|
|
|
* can be highlighted is need be. |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function resolveParams() : array |
|
169
|
|
|
{ |
|
170
|
|
|
$previous = $this->info->isParamExclusionEnabled(); |
|
171
|
|
|
|
|
172
|
|
|
if($previous) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->info->setParamExclusion(false); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$params = $this->info->getParams(); |
|
178
|
|
|
|
|
179
|
|
|
$this->info->setParamExclusion($previous); |
|
180
|
|
|
|
|
181
|
|
|
return $params; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
protected function render_params() : string |
|
185
|
|
|
{ |
|
186
|
|
|
$params = $this->resolveParams(); |
|
187
|
|
|
|
|
188
|
|
|
if(empty($params)) { |
|
189
|
|
|
return ''; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$tokens = array(); |
|
193
|
|
|
$excluded = array(); |
|
194
|
|
|
|
|
195
|
|
|
if($this->info->isParamExclusionEnabled()) |
|
196
|
|
|
{ |
|
197
|
|
|
$excluded = $this->info->getExcludedParams(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
foreach($params as $param => $value) |
|
201
|
|
|
{ |
|
202
|
|
|
$parts = sprintf( |
|
203
|
|
|
'<span class="link-param-name">%s</span>'. |
|
204
|
|
|
'<span class="link-component param-equals">=</span>'. |
|
205
|
|
|
'<span class="link-param-value">%s</span>'. |
|
206
|
|
|
'<wbr>', |
|
207
|
|
|
$param, |
|
208
|
|
|
str_replace( |
|
209
|
|
|
array(':', '.', '-', '_'), |
|
210
|
|
|
array(':<wbr>', '.<wbr>', '-<wbr>', '_<wbr>'), |
|
211
|
|
|
$value |
|
212
|
|
|
) |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
$tag = $this->resolveTag($excluded, $param); |
|
216
|
|
|
|
|
217
|
|
|
if(!empty($tag)) |
|
218
|
|
|
{ |
|
219
|
|
|
$tokens[] = sprintf($tag, $parts); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return |
|
224
|
|
|
'<span class="link-component query-sign">?</span>'. |
|
225
|
|
|
implode('<span class="link-component param-separator">&</span>', $tokens); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
protected function resolveTag(array $excluded, string $paramName) : string |
|
229
|
|
|
{ |
|
230
|
|
|
// regular, non-excluded parameter |
|
231
|
|
|
if(!isset($excluded[$paramName])) |
|
232
|
|
|
{ |
|
233
|
|
|
return '<span class="link-param">%s</span>'; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// highlight excluded parameters is disabled, ignore this parameter |
|
237
|
|
|
if(!$this->info->isHighlightExcludeEnabled()) |
|
238
|
|
|
{ |
|
239
|
|
|
return ''; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$tooltip = $excluded[$paramName]; |
|
243
|
|
|
|
|
244
|
|
|
return sprintf( |
|
245
|
|
|
'<span class="link-param excluded-param" title="%s" data-toggle="tooltip">%s</span>', |
|
246
|
|
|
$tooltip, |
|
247
|
|
|
'%s' |
|
248
|
|
|
); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
protected function render_fragment() : string |
|
252
|
|
|
{ |
|
253
|
|
|
if(!$this->info->hasFragment()) { |
|
254
|
|
|
return ''; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return sprintf( |
|
258
|
|
|
'<span class="link-fragment-sign">#</span>'. |
|
259
|
|
|
'<span class="link-fragment">%s</span>', |
|
260
|
|
|
$this->info->getFragment() |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public static function getHighlightCSS() : string |
|
265
|
|
|
{ |
|
266
|
|
|
$cssFolder = realpath(__DIR__.'/../../css'); |
|
267
|
|
|
|
|
268
|
|
|
if($cssFolder === false) { |
|
269
|
|
|
throw new BaseException( |
|
270
|
|
|
'Cannot find package CSS folder.', |
|
271
|
|
|
null, |
|
272
|
|
|
URLInfo::ERROR_CANNOT_FIND_CSS_FOLDER |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return FileHelper::readContents($cssFolder.'/urlinfo-highlight.css'); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|