1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Marius Orcsik <[email protected]> |
4
|
|
|
* @created 2015-07-03 |
5
|
|
|
*/ |
6
|
|
|
namespace vsc\infrastructure\urls; |
7
|
|
|
|
8
|
|
|
use vsc\infrastructure\Object; |
9
|
|
|
|
10
|
|
|
class Url extends Object |
11
|
|
|
{ |
12
|
|
|
static protected $queryEncodingType = PHP_QUERY_RFC1738; |
13
|
|
|
static protected $validSchemes = ['http', 'https', 'file']; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $scheme; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $host; |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $port; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $path; |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $query = []; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $fragment; |
39
|
|
|
|
40
|
1 |
|
public function __toString() { |
41
|
1 |
|
return $this->getUrl(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $scheme |
46
|
|
|
*/ |
47
|
20 |
|
public function setScheme($scheme) { |
48
|
20 |
|
if (static::isValidScheme($scheme)) { |
49
|
20 |
|
$this->scheme = $scheme; |
50
|
|
|
} |
51
|
20 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $host |
55
|
|
|
*/ |
56
|
2 |
|
public function setHost($host) { |
57
|
2 |
|
$schPos = strpos($host, '//'); |
58
|
2 |
|
if ($schPos === false) { |
59
|
2 |
|
$this->host = $host; |
60
|
|
|
} else { |
61
|
|
|
$this->host = substr($host, $schPos+2); |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $port |
67
|
|
|
*/ |
68
|
1 |
|
public function setPort($port) { |
69
|
1 |
|
$this->port = $port; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $path |
74
|
|
|
*/ |
75
|
18 |
|
public function setPath($path) { |
76
|
18 |
|
$this->path = $path; |
77
|
18 |
|
} |
78
|
|
|
/** |
79
|
|
|
* @param [] $query |
|
|
|
|
80
|
|
|
*/ |
81
|
1 |
|
public function setQuery($query) { |
82
|
1 |
|
$this->query = $query; |
83
|
1 |
|
} |
84
|
|
|
/** |
85
|
|
|
* @param string $query |
86
|
|
|
*/ |
87
|
1 |
|
public function setRawQuery($query) { |
88
|
1 |
|
parse_str($query, $this->query); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $fragment |
93
|
|
|
*/ |
94
|
1 |
|
public function setFragment($fragment) { |
95
|
1 |
|
$this->fragment = $fragment; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
|
|
|
|
100
|
|
|
*/ |
101
|
1 |
|
public function getScheme() { |
102
|
1 |
|
if ($this->hasHost()) { |
103
|
|
|
if ($this->hasScheme()) { |
104
|
|
|
return $this->scheme . '://'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return '//'; |
108
|
|
|
} |
109
|
1 |
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
public function getHost() { |
116
|
1 |
|
return $this->host; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
|
|
|
|
121
|
|
|
*/ |
122
|
1 |
|
public function getPort() { |
123
|
1 |
|
if ($this->hasPort() && $this->port != 80) { |
124
|
1 |
|
return ':' . $this->port; |
125
|
|
|
} |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
18 |
|
public function getPath() { |
133
|
18 |
|
$path = ''; |
134
|
18 |
|
if ($this->hasPath()) { |
135
|
17 |
|
$path = UrlParserA::normalizePath($this->path); |
136
|
|
|
} |
137
|
18 |
|
if (($this->hasHost() || !empty($path))&& !UrlParserA::hasGoodTermination($path)) { |
138
|
17 |
|
$path .= '/'; |
139
|
|
|
} |
140
|
18 |
|
return $path; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
2 |
|
public function getQuery() { |
147
|
2 |
|
return $this->query; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param bool $encoded |
152
|
|
|
* @return string |
|
|
|
|
153
|
|
|
*/ |
154
|
1 |
|
public function getRawQueryString($encoded = true) { |
155
|
1 |
|
if ($this->hasQuery()) { |
156
|
1 |
|
return '?' . http_build_query($this->getQuery(), '', $encoded ? '&' : '&', static::$queryEncodingType); |
157
|
|
|
} |
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
|
|
|
|
163
|
|
|
*/ |
164
|
1 |
|
public function getFragment() { |
165
|
1 |
|
if ($this->hasFragment()) { |
166
|
|
|
return '#' . $this->fragment; |
167
|
|
|
} |
168
|
1 |
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $schema |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
19 |
|
static public function isValidScheme($schema) { |
|
|
|
|
176
|
19 |
|
return in_array($schema, static::$validSchemes); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
1 |
|
public function hasScheme() { |
183
|
1 |
|
return static::isValidScheme($this->scheme); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
18 |
|
public function hasHost() { |
190
|
18 |
|
return (null !== $this->host && !empty($this->host)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
1 |
|
public function hasPort() { |
197
|
1 |
|
return (null !== $this->port && !empty($this->port)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
18 |
|
public function hasPath() { |
204
|
18 |
|
return !empty($this->path); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
1 |
|
public function hasQuery() { |
211
|
1 |
|
return count($this->query) > 0; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
1 |
|
public function hasFragment() { |
218
|
1 |
|
return (null !== $this->fragment && !empty($this->fragment)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
3 |
|
public function isLocal() { |
225
|
3 |
|
return (($this->getScheme() == 'file' || !$this->hasScheme()) && !$this->hasHost() && $this->hasPath()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
2 |
|
public function getUrl() { |
232
|
2 |
|
return $this->getScheme() . |
233
|
2 |
|
$this->getHost() . |
234
|
2 |
|
$this->getPort() . |
235
|
2 |
|
$this->getPath() . |
236
|
2 |
|
$this->getRawQueryString() . |
237
|
2 |
|
$this->getFragment(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string $sPath |
242
|
|
|
* @returns UrlParserA |
243
|
|
|
*/ |
244
|
3 |
|
public function addPath($sPath) { |
245
|
3 |
|
$sExistingPath = $this->getPath(); |
246
|
3 |
|
if (substr($sExistingPath, -1) != '/') { |
247
|
|
|
$sPath = '/' . $sPath; |
248
|
|
|
} |
249
|
3 |
|
$this->setPath($sExistingPath . $sPath); |
250
|
3 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.