/
Url.php
382 lines (338 loc) · 10.1 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* <!--
* This file is part of the adventure php framework (APF) published under
* https://adventure-php-framework.org.
*
* The APF is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The APF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the APF. If not, see http://www.gnu.org/licenses/lgpl-3.0.txt.
* -->
*/
namespace APF\tools\link;
use APF\core\http\mixins\GetRequestResponse;
/**
* This class represents a url designed to generate related urls using
* the APF's link scheme implementations.
*
* @author Christian Achatz
* @version
* Version 0.1, 29.03.2011<br />
* Version 0.2, 05.09.2015 (ID#258: added support for array URL params)<br />
*/
final class Url {
use GetRequestResponse;
const DEFAULT_HTTP_PORT = '80';
const DEFAULT_HTTPS_PORT = '443';
private $scheme;
private $host;
private $port;
private $path;
private $query = [];
private $anchor;
/**
* Constructs a url for link generation purposes.
*
* @param string $scheme The url's scheme (e.g. http, ftp).
* @param string $host The url's host (e.g. example.com).
* @param int|null $port The url's port (e.g. 80, 443).
* @param string $path The url's path (e.g. /foo/bar).
* @param array $query An associative array of query parameters.
* @param string $anchor An optional anchor (e.g. #top).
*
* @author Christian Achatz
* @version
* Version 0.1, 29.03.2011<br />
*/
public function __construct(string $scheme = null, string $host = null, int $port = null, string $path = null, array $query = [], string $anchor = null) {
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->path = $path;
$this->query = $query;
$this->anchor = $anchor;
}
/**
* Let's you construct a url applying a string.
*
* @param string $url The url to parse.
*
* @return Url The resulting url.
* @throws UrlFormatException In case the given string is not a valid url.
*
* @author Christian Achatz
* @version
* Version 0.1, 29.03.2011<br />
*/
public static function fromString(string $url) {
// the ugly "@" is only introduced to convert the E_WARNING into an exception
$parts = @parse_url($url);
if ($parts === false || !is_array($parts)) {
throw new UrlFormatException('The given url "' . $url . '" cannot be parsed due to semantic errors!');
}
// resolve missing parameters
if (!isset($parts['scheme'])) {
$parts['scheme'] = null;
}
if (!isset($parts['host'])) {
$parts['host'] = null;
}
if (!isset($parts['port'])) {
$parts['port'] = null;
}
if (!isset($parts['path'])) {
$parts['path'] = null;
}
if (!isset($parts['query'])) {
$parts['query'] = null;
}
if (!isset($parts['fragment'])) {
$parts['fragment'] = null;
}
return new Url($parts['scheme'], $parts['host'], $parts['port'], $parts['path'], self::getQueryParams($parts['query']), $parts['fragment']);
}
/**
* Generates a query param array from a given query string.
*
* @param string $query The query params string.
*
* @return array The query params array.
*
* @author Christian Achatz
* @version
* Version 0.1, 29.03.2011<br />
* Version 0.2, 05.09.2015 (ID#258: support nested query parameters)<br />
*/
private static function getQueryParams(string $query = null) {
// reverse resolve encoded ampersands
$query = str_replace('&', '&', $query);
// in case of empty query strings, return empty param list
if (empty($query)) {
return [];
}
parse_str($query, $output);
return $output;
}
/**
* Creates a url representation from the current request url.
*
* @param boolean $absolute True, in case the url should be absolute, false otherwise.
*
* @return Url The current url representation.
* @throws UrlFormatException In case the given string is not a valid url.
*
* @author Christian Achatz
* @version
* Version 0.1, 29.03.2011<br />
* Version 0.2, 09.03.2013 (Now uses standard PHP variables in stead of a Registry value to allow better url input filter manipulation)<br />
*/
public static function fromCurrent(bool $absolute = false) {
return self::getRequestStatic()->getUrl($absolute);
}
/**
* Creates a url representation from the referring url.
*
* @param boolean $absolute True, in case the url should be absolute, false otherwise.
*
* @return Url The current url representation.
* @throws UrlFormatException In case the given referrer is not a valid url.
*
* @author dave
* @version
* Version 0.1, 07.09.2011<br />
*/
public static function fromReferrer(bool $absolute = false) {
return self::getRequestStatic()->getReferrerUrl($absolute);
}
public function getScheme() {
return $this->scheme;
}
/**
* Let's you inject the scheme of the url.
*
* @param string $scheme The url scheme (e.g. http, ftp).
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setScheme(string $scheme = null) {
$this->scheme = $scheme;
return $this;
}
public function getHost() {
return $this->host;
}
/**
* Let's you inject the host of the url.
*
* @param string $host The url' host (e.g. example.com).
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setHost(string $host = null) {
$this->host = $host;
return $this;
}
public function getPort() {
return $this->port;
}
/**
* Let's you inject the port of the url.
*
* @param int|null $port The url's port (e.g. 80, 443).
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setPort(int $port = null) {
$this->port = $port;
return $this;
}
public function getPath() {
return $this->path;
}
/**
* Let's you inject the path of the url.
*
* @param string $path The url's path (e.g. /foo/bar).
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setPath(string $path = null) {
$this->path = $path;
return $this;
}
/**
* Returns the list of registered query parameters.
*
* @return array The query parameters of the url.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function getQuery() {
return $this->query;
}
/**
* Let's you inject the desired amount of request parameters.
*
* @param array $query The query parameters to inject.
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setQuery(array $query = null) {
$this->query = $query;
return $this;
}
public function getAnchor() {
return $this->anchor;
}
/**
* Let's you inject the anchor of the url.
*
* @param string $anchor The anchor (e.g. #top).
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setAnchor(string $anchor = null) {
$this->anchor = $anchor;
return $this;
}
/**
* Let's you query a request parameter.
*
* @param string $name The name of the desired parameter.
* @param string $default The default value to return in case the parameter is not existing.
*
* @return string The value of the parameter or null if it doesn't exist.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function getQueryParameter(string $name, $default = null) {
return isset($this->query[$name]) ? $this->query[$name] : $default;
}
/**
* This method let's you merge a list of parameters into the current url's
* list. Setting a query parameter's value to <em>null</em> indicates to
* delete the parameter within the LinkScheme implementation.
*
* @param array $query An associative array of the query params to merge.
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function mergeQuery(array $query) {
foreach ($query as $name => $value) {
$this->query[$name] = $value;
}
return $this;
}
/**
* This method resets the list of parameters.
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.06.2011<br />
*/
public function resetQuery() {
$this->query = [];
return $this;
}
/**
* This method can be used to set a query parameter. Setting it's value
* to <em>null</em> indicates to delete the parameter within the
* LinkScheme implementation.
*
* @param string $name The name of the parameter.
* @param string $value The value of the parameter.
*
* @return Url This object for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 04.04.2011<br />
*/
public function setQueryParameter(string $name, $value) {
$this->query[$name] = $value;
return $this;
}
}