1
|
|
|
<?php |
2
|
|
|
namespace Subreality\Dilmun\Anshar\Http; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\UriInterface; |
5
|
|
|
use Subreality\Dilmun\Anshar\Utils\ArrayHelper; |
6
|
|
|
use Subreality\Dilmun\Anshar\Utils\StringHelper; |
7
|
|
|
|
8
|
|
|
class Uri implements UriInterface |
9
|
|
|
{ |
10
|
|
|
use SchemePortsTrait; |
11
|
|
|
|
12
|
|
|
protected $uri_parts = array( |
13
|
|
|
"scheme" => "", |
14
|
|
|
"hier_part" => "", |
15
|
|
|
"authority" => "", |
16
|
|
|
"user_info" => "", |
17
|
|
|
"host" => "", |
18
|
|
|
"port" => null, |
19
|
|
|
"path" => "", |
20
|
|
|
"query" => "", |
21
|
|
|
"fragment" => "", |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Uri constructor. Accepts a string representing a URI and parses the string into the URI's component parts. |
26
|
|
|
* |
27
|
|
|
* @throws \InvalidArgumentException Throws an \InvalidArgumentException when its parameter is not a string |
28
|
|
|
* @param string $uri |
29
|
|
|
*/ |
30
|
34 |
|
public function __construct($uri) |
31
|
|
|
{ |
32
|
34 |
|
if (!is_string($uri)) { |
33
|
6 |
|
throw new \InvalidArgumentException("New Uri objects must be constructed with a string URI"); |
34
|
|
|
} |
35
|
|
|
|
36
|
28 |
|
$this->explodeUri($uri); |
37
|
28 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieve the parsed components of the URI string. |
41
|
|
|
* |
42
|
|
|
* If the class was provided an invalid URI string, URI components will be empty strings, except port, which will |
43
|
|
|
* be null |
44
|
|
|
* |
45
|
|
|
* @return mixed[] |
46
|
|
|
*/ |
47
|
11 |
|
public function getParsedUri() |
48
|
|
|
{ |
49
|
11 |
|
return $this->uri_parts; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieve the scheme component of the URI. |
54
|
|
|
* |
55
|
|
|
* If no scheme is present, this method MUST return an empty string. |
56
|
|
|
* |
57
|
|
|
* The value returned MUST be normalized to lowercase, per RFC 3986 |
58
|
|
|
* Section 3.1. |
59
|
|
|
* |
60
|
|
|
* The trailing ":" character is not part of the scheme and MUST NOT be |
61
|
|
|
* added. |
62
|
|
|
* |
63
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.1 |
64
|
|
|
* @return string The URI scheme. |
65
|
|
|
*/ |
66
|
3 |
|
public function getScheme() |
67
|
|
|
{ |
68
|
3 |
|
return strtolower($this->uri_parts["scheme"]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve the authority component of the URI. |
73
|
|
|
* |
74
|
|
|
* If no authority information is present, this method MUST return an empty |
75
|
|
|
* string. |
76
|
|
|
* |
77
|
|
|
* The authority syntax of the URI is: |
78
|
|
|
* |
79
|
|
|
* <pre> |
80
|
|
|
* [user-info@]host[:port] |
81
|
|
|
* </pre> |
82
|
|
|
* |
83
|
|
|
* If the port component is not set or is the standard port for the current |
84
|
|
|
* scheme, it SHOULD NOT be included. |
85
|
|
|
* |
86
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.2 |
87
|
|
|
* @return string The URI authority, in "[user-info@]host[:port]" format. |
88
|
|
|
*/ |
89
|
4 |
|
public function getAuthority() |
90
|
|
|
{ |
91
|
4 |
|
$normalized_authority = $this->uri_parts["host"]; |
92
|
|
|
|
93
|
4 |
|
if (!empty($this->uri_parts["user_info"])) { |
94
|
4 |
|
$normalized_authority = $this->uri_parts["user_info"] . "@" . $normalized_authority; |
95
|
4 |
|
} |
96
|
|
|
|
97
|
4 |
|
$normalized_port = $this->normalizePort(); |
98
|
|
|
|
99
|
4 |
|
if (!is_null($normalized_port)) { |
100
|
2 |
|
$normalized_authority = $normalized_authority . ":" . $normalized_port; |
101
|
2 |
|
} |
102
|
|
|
|
103
|
4 |
|
return $normalized_authority; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retrieve the user information component of the URI. |
108
|
|
|
* |
109
|
|
|
* If no user information is present, this method MUST return an empty |
110
|
|
|
* string. |
111
|
|
|
* |
112
|
|
|
* If a user is present in the URI, this will return that value; |
113
|
|
|
* additionally, if the password is also present, it will be appended to the |
114
|
|
|
* user value, with a colon (":") separating the values. |
115
|
|
|
* |
116
|
|
|
* The trailing "@" character is not part of the user information and MUST |
117
|
|
|
* NOT be added. |
118
|
|
|
* |
119
|
|
|
* @return string The URI user information, in "username[:password]" format. |
120
|
|
|
*/ |
121
|
2 |
|
public function getUserInfo() |
122
|
|
|
{ |
123
|
2 |
|
return $this->uri_parts["user_info"]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieve the host component of the URI. |
128
|
|
|
* |
129
|
|
|
* If no host is present, this method MUST return an empty string. |
130
|
|
|
* |
131
|
|
|
* The value returned MUST be normalized to lowercase, per RFC 3986 |
132
|
|
|
* Section 3.2.2. |
133
|
|
|
* |
134
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
135
|
|
|
* @return string The URI host. |
136
|
|
|
*/ |
137
|
3 |
|
public function getHost() |
138
|
|
|
{ |
139
|
3 |
|
return strtolower($this->uri_parts["host"]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieve the port component of the URI. |
144
|
|
|
* |
145
|
|
|
* If a port is present, and it is non-standard for the current scheme, |
146
|
|
|
* this method MUST return it as an integer. If the port is the standard port |
147
|
|
|
* used with the current scheme, this method SHOULD return null. |
148
|
|
|
* |
149
|
|
|
* If no port is present, and no scheme is present, this method MUST return |
150
|
|
|
* a null value. |
151
|
|
|
* |
152
|
|
|
* If no port is present, but a scheme is present, this method MAY return |
153
|
|
|
* the standard port for that scheme, but SHOULD return null. |
154
|
|
|
* |
155
|
|
|
* @return null|int The URI port. |
156
|
|
|
*/ |
157
|
4 |
|
public function getPort() |
158
|
|
|
{ |
159
|
4 |
|
$normalized_port = $this->normalizePort(); |
160
|
|
|
|
161
|
4 |
|
return $normalized_port; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Retrieve the path component of the URI. |
166
|
|
|
* |
167
|
|
|
* The path can either be empty or absolute (starting with a slash) or |
168
|
|
|
* rootless (not starting with a slash). Implementations MUST support all |
169
|
|
|
* three syntaxes. |
170
|
|
|
* |
171
|
|
|
* Normally, the empty path "" and absolute path "/" are considered equal as |
172
|
|
|
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
173
|
|
|
* do this normalization because in contexts with a trimmed base path, e.g. |
174
|
|
|
* the front controller, this difference becomes significant. It's the task |
175
|
|
|
* of the user to handle both "" and "/". |
176
|
|
|
* |
177
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
178
|
|
|
* any characters. To determine what characters to encode, please refer to |
179
|
|
|
* RFC 3986, Sections 2 and 3.3. |
180
|
|
|
* |
181
|
|
|
* As an example, if the value should include a slash ("/") not intended as |
182
|
|
|
* delimiter between path segments, that value MUST be passed in encoded |
183
|
|
|
* form (e.g., "%2F") to the instance. |
184
|
|
|
* |
185
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
186
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.3 |
187
|
|
|
* @return string The URI path. |
188
|
|
|
*/ |
189
|
4 |
View Code Duplication |
public function getPath() |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$unencoded = array( |
192
|
4 |
|
"/", |
193
|
4 |
|
":", |
194
|
4 |
|
"@", |
195
|
4 |
|
"!", |
196
|
4 |
|
"$", |
197
|
4 |
|
"&", |
198
|
4 |
|
"'", |
199
|
4 |
|
"(", |
200
|
4 |
|
")", |
201
|
4 |
|
"*", |
202
|
4 |
|
"+", |
203
|
4 |
|
",", |
204
|
4 |
|
";", |
205
|
4 |
|
"=", |
206
|
4 |
|
); |
207
|
4 |
|
$string_helper = new StringHelper($this->uri_parts["path"]); |
208
|
4 |
|
$encoded_string = $string_helper->affectChunks("rawurlencode", ...$unencoded); |
209
|
|
|
|
210
|
4 |
|
return $encoded_string; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Retrieve the query string of the URI. |
215
|
|
|
* |
216
|
|
|
* If no query string is present, this method MUST return an empty string. |
217
|
|
|
* |
218
|
|
|
* The leading "?" character is not part of the query and MUST NOT be |
219
|
|
|
* added. |
220
|
|
|
* |
221
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
222
|
|
|
* any characters. To determine what characters to encode, please refer to |
223
|
|
|
* RFC 3986, Sections 2 and 3.4. |
224
|
|
|
* |
225
|
|
|
* As an example, if a value in a key/value pair of the query string should |
226
|
|
|
* include an ampersand ("&") not intended as a delimiter between values, |
227
|
|
|
* that value MUST be passed in encoded form (e.g., "%26") to the instance. |
228
|
|
|
* |
229
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
230
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.4 |
231
|
|
|
* @return string The URI query string. |
232
|
|
|
*/ |
233
|
4 |
View Code Duplication |
public function getQuery() |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$unencoded = array( |
236
|
4 |
|
"/", |
237
|
4 |
|
":", |
238
|
4 |
|
"@", |
239
|
4 |
|
"!", |
240
|
4 |
|
"$", |
241
|
4 |
|
"&", |
242
|
4 |
|
"'", |
243
|
4 |
|
"(", |
244
|
4 |
|
")", |
245
|
4 |
|
"*", |
246
|
4 |
|
"+", |
247
|
4 |
|
",", |
248
|
4 |
|
";", |
249
|
4 |
|
"=", |
250
|
4 |
|
"?", |
251
|
4 |
|
); |
252
|
4 |
|
$string_helper = new StringHelper($this->uri_parts["query"]); |
253
|
4 |
|
$encoded_string = $string_helper->affectChunks("rawurlencode", ...$unencoded); |
254
|
|
|
|
255
|
4 |
|
return $encoded_string; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Retrieve the fragment component of the URI. |
260
|
|
|
* |
261
|
|
|
* If no fragment is present, this method MUST return an empty string. |
262
|
|
|
* |
263
|
|
|
* The leading "#" character is not part of the fragment and MUST NOT be |
264
|
|
|
* added. |
265
|
|
|
* |
266
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
267
|
|
|
* any characters. To determine what characters to encode, please refer to |
268
|
|
|
* RFC 3986, Sections 2 and 3.5. |
269
|
|
|
* |
270
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
271
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.5 |
272
|
|
|
* @return string The URI fragment. |
273
|
|
|
*/ |
274
|
|
|
public function getFragment() |
275
|
|
|
{ |
276
|
|
|
return $this->uri_parts["fragment"]; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Return an instance with the specified scheme. |
281
|
|
|
* |
282
|
|
|
* This method MUST retain the state of the current instance, and return |
283
|
|
|
* an instance that contains the specified scheme. |
284
|
|
|
* |
285
|
|
|
* Implementations MUST support the schemes "http" and "https" case |
286
|
|
|
* insensitively, and MAY accommodate other schemes if required. |
287
|
|
|
* |
288
|
|
|
* An empty scheme is equivalent to removing the scheme. |
289
|
|
|
* |
290
|
|
|
* @param string $scheme The scheme to use with the new instance. |
291
|
|
|
* @return static A new instance with the specified scheme. |
|
|
|
|
292
|
|
|
* @throws \InvalidArgumentException for invalid or unsupported schemes. |
293
|
|
|
*/ |
294
|
|
|
public function withScheme($scheme) |
295
|
|
|
{ |
296
|
|
|
// TODO: Implement withScheme() method. |
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Return an instance with the specified user information. |
301
|
|
|
* |
302
|
|
|
* This method MUST retain the state of the current instance, and return |
303
|
|
|
* an instance that contains the specified user information. |
304
|
|
|
* |
305
|
|
|
* Password is optional, but the user information MUST include the |
306
|
|
|
* user; an empty string for the user is equivalent to removing user |
307
|
|
|
* information. |
308
|
|
|
* |
309
|
|
|
* @param string $user The user name to use for authority. |
310
|
|
|
* @param null|string $password The password associated with $user. |
311
|
|
|
* @return static A new instance with the specified user information. |
|
|
|
|
312
|
|
|
*/ |
313
|
|
|
public function withUserInfo($user, $password = null) |
314
|
|
|
{ |
315
|
|
|
// TODO: Implement withUserInfo() method. |
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Return an instance with the specified host. |
320
|
|
|
* |
321
|
|
|
* This method MUST retain the state of the current instance, and return |
322
|
|
|
* an instance that contains the specified host. |
323
|
|
|
* |
324
|
|
|
* An empty host value is equivalent to removing the host. |
325
|
|
|
* |
326
|
|
|
* @param string $host The hostname to use with the new instance. |
327
|
|
|
* @return static A new instance with the specified host. |
|
|
|
|
328
|
|
|
* @throws \InvalidArgumentException for invalid hostnames. |
329
|
|
|
*/ |
330
|
|
|
public function withHost($host) |
331
|
|
|
{ |
332
|
|
|
// TODO: Implement withHost() method. |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Return an instance with the specified port. |
337
|
|
|
* |
338
|
|
|
* This method MUST retain the state of the current instance, and return |
339
|
|
|
* an instance that contains the specified port. |
340
|
|
|
* |
341
|
|
|
* Implementations MUST raise an exception for ports outside the |
342
|
|
|
* established TCP and UDP port ranges. |
343
|
|
|
* |
344
|
|
|
* A null value provided for the port is equivalent to removing the port |
345
|
|
|
* information. |
346
|
|
|
* |
347
|
|
|
* @param null|int $port The port to use with the new instance; a null value |
348
|
|
|
* removes the port information. |
349
|
|
|
* @return static A new instance with the specified port. |
|
|
|
|
350
|
|
|
* @throws \InvalidArgumentException for invalid ports. |
351
|
|
|
*/ |
352
|
|
|
public function withPort($port) |
353
|
|
|
{ |
354
|
|
|
// TODO: Implement withPort() method. |
|
|
|
|
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Return an instance with the specified path. |
359
|
|
|
* |
360
|
|
|
* This method MUST retain the state of the current instance, and return |
361
|
|
|
* an instance that contains the specified path. |
362
|
|
|
* |
363
|
|
|
* The path can either be empty or absolute (starting with a slash) or |
364
|
|
|
* rootless (not starting with a slash). Implementations MUST support all |
365
|
|
|
* three syntaxes. |
366
|
|
|
* |
367
|
|
|
* If the path is intended to be domain-relative rather than path relative then |
368
|
|
|
* it must begin with a slash ("/"). Paths not starting with a slash ("/") |
369
|
|
|
* are assumed to be relative to some base path known to the application or |
370
|
|
|
* consumer. |
371
|
|
|
* |
372
|
|
|
* Users can provide both encoded and decoded path characters. |
373
|
|
|
* Implementations ensure the correct encoding as outlined in getPath(). |
374
|
|
|
* |
375
|
|
|
* @param string $path The path to use with the new instance. |
376
|
|
|
* @return static A new instance with the specified path. |
|
|
|
|
377
|
|
|
* @throws \InvalidArgumentException for invalid paths. |
378
|
|
|
*/ |
379
|
|
|
public function withPath($path) |
380
|
|
|
{ |
381
|
|
|
// TODO: Implement withPath() method. |
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Return an instance with the specified query string. |
386
|
|
|
* |
387
|
|
|
* This method MUST retain the state of the current instance, and return |
388
|
|
|
* an instance that contains the specified query string. |
389
|
|
|
* |
390
|
|
|
* Users can provide both encoded and decoded query characters. |
391
|
|
|
* Implementations ensure the correct encoding as outlined in getQuery(). |
392
|
|
|
* |
393
|
|
|
* An empty query string value is equivalent to removing the query string. |
394
|
|
|
* |
395
|
|
|
* @param string $query The query string to use with the new instance. |
396
|
|
|
* @return static A new instance with the specified query string. |
|
|
|
|
397
|
|
|
* @throws \InvalidArgumentException for invalid query strings. |
398
|
|
|
*/ |
399
|
|
|
public function withQuery($query) |
400
|
|
|
{ |
401
|
|
|
// TODO: Implement withQuery() method. |
|
|
|
|
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Return an instance with the specified URI fragment. |
406
|
|
|
* |
407
|
|
|
* This method MUST retain the state of the current instance, and return |
408
|
|
|
* an instance that contains the specified URI fragment. |
409
|
|
|
* |
410
|
|
|
* Users can provide both encoded and decoded fragment characters. |
411
|
|
|
* Implementations ensure the correct encoding as outlined in getFragment(). |
412
|
|
|
* |
413
|
|
|
* An empty fragment value is equivalent to removing the fragment. |
414
|
|
|
* |
415
|
|
|
* @param string $fragment The fragment to use with the new instance. |
416
|
|
|
* @return static A new instance with the specified fragment. |
|
|
|
|
417
|
|
|
*/ |
418
|
|
|
public function withFragment($fragment) |
419
|
|
|
{ |
420
|
|
|
// TODO: Implement withFragment() method. |
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Return the string representation as a URI reference. |
425
|
|
|
* |
426
|
|
|
* Depending on which components of the URI are present, the resulting |
427
|
|
|
* string is either a full URI or relative reference according to RFC 3986, |
428
|
|
|
* Section 4.1. The method concatenates the various components of the URI, |
429
|
|
|
* using the appropriate delimiters: |
430
|
|
|
* |
431
|
|
|
* - If a scheme is present, it MUST be suffixed by ":". |
432
|
|
|
* - If an authority is present, it MUST be prefixed by "//". |
433
|
|
|
* - The path can be concatenated without delimiters. But there are two |
434
|
|
|
* cases where the path has to be adjusted to make the URI reference |
435
|
|
|
* valid as PHP does not allow to throw an exception in __toString(): |
436
|
|
|
* - If the path is rootless and an authority is present, the path MUST |
437
|
|
|
* be prefixed by "/". |
438
|
|
|
* - If the path is starting with more than one "/" and no authority is |
439
|
|
|
* present, the starting slashes MUST be reduced to one. |
440
|
|
|
* - If a query is present, it MUST be prefixed by "?". |
441
|
|
|
* - If a fragment is present, it MUST be prefixed by "#". |
442
|
|
|
* |
443
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-4.1 |
444
|
|
|
* @return string |
|
|
|
|
445
|
|
|
*/ |
446
|
|
|
public function __toString() |
447
|
|
|
{ |
448
|
|
|
// TODO: Implement __toString() method. |
|
|
|
|
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Splits a string URI into its component parts, returning true if the URI string matches a valid URI's syntax |
453
|
|
|
* and false if the URI string does not |
454
|
|
|
* |
455
|
|
|
* @param string $uri The URI string to be decomposed |
456
|
|
|
* @return bool Returns true if the URI string matches a valid URI's syntax |
457
|
|
|
* Returns false otherwise |
458
|
|
|
*/ |
459
|
28 |
|
private function explodeUri($uri) |
460
|
|
|
{ |
461
|
28 |
|
$reg_start = '/^'; |
462
|
28 |
|
$scheme_part = '(?P<scheme>.[^:]+)'; |
463
|
28 |
|
$scheme_separator = ':'; |
464
|
28 |
|
$hier_part = '(?<hier_part>.[^\?#]+)'; |
465
|
28 |
|
$query_part = '(?:\?(?P<query>.[^#]+))?'; |
466
|
28 |
|
$fragment_part = '(?:#(?P<fragment>.+))?'; |
467
|
28 |
|
$reg_end = '/'; |
468
|
|
|
|
469
|
28 |
|
$uri_syntax = $reg_start . $scheme_part . $scheme_separator . $hier_part . $query_part . $fragment_part . |
470
|
28 |
|
$reg_end; |
471
|
|
|
|
472
|
28 |
|
$uri_valid = preg_match($uri_syntax, $uri, $parts); |
473
|
|
|
|
474
|
28 |
|
$this->uri_parts = array_merge($this->uri_parts, $parts); //overwriting default values with matches |
475
|
|
|
|
476
|
28 |
|
$this->explodeHierParts($this->uri_parts["hier_part"]); |
477
|
|
|
|
478
|
28 |
|
$this->sanitizeUriPartsArray(); |
479
|
|
|
|
480
|
28 |
|
return (bool) $uri_valid; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Splits URI hierarchy data into authority and path data. |
485
|
|
|
* |
486
|
|
|
* @param string $hier_part The hierarchy part of a URI to be decomposed |
487
|
|
|
* @return void |
488
|
|
|
*/ |
489
|
28 |
|
private function explodeHierParts($hier_part) |
490
|
|
|
{ |
491
|
28 |
|
$authority_parts = array(); |
492
|
|
|
|
493
|
28 |
|
$reg_start = '/^'; |
494
|
28 |
|
$authority_part = '(?:(?:\/\/)(?P<authority>.[^\/]+))?'; |
495
|
28 |
|
$path_part = '(?P<path>.+)?'; |
496
|
28 |
|
$reg_end = '/'; |
497
|
|
|
|
498
|
28 |
|
$hier_part_syntax = $reg_start . $authority_part . $path_part . $reg_end; |
499
|
|
|
|
500
|
28 |
|
preg_match($hier_part_syntax, $hier_part, $hier_parts); |
501
|
|
|
|
502
|
28 |
|
if (isset($hier_parts["authority"])) { |
503
|
24 |
|
$authority_parts = $this->explodeAuthority($hier_parts["authority"]); |
504
|
24 |
|
} |
505
|
|
|
|
506
|
28 |
|
$hier_parts = array_merge($hier_parts, $authority_parts); |
507
|
|
|
|
508
|
28 |
|
$this->uri_parts = array_merge($this->uri_parts, $hier_parts); |
509
|
28 |
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Splits URI authority data into user info, host, and port data, returning an array with named keys. |
513
|
|
|
* |
514
|
|
|
* For the host component, it will capture everything within brackets to support ipv6 or match all characters until |
515
|
|
|
* it finds a colon indicating the start of the port component. |
516
|
|
|
* |
517
|
|
|
* @param string $authority The authority part of a URI to be decomposed |
518
|
|
|
* @return mixed[] An array with named keys containing the component parts of the supplied |
|
|
|
|
519
|
|
|
* authority |
520
|
|
|
*/ |
521
|
24 |
|
private function explodeAuthority($authority) |
522
|
|
|
{ |
523
|
24 |
|
$reg_start = '/^'; |
524
|
24 |
|
$user_info_part = '(?:(?P<user_info>.+)@)?'; |
525
|
24 |
|
$host_part = '(?P<host>\[.+\]|.[^:]+)'; |
526
|
24 |
|
$port_part = '(?::(?P<port>[0-9]+))?'; |
527
|
24 |
|
$reg_end = '/'; |
528
|
|
|
|
529
|
24 |
|
$authority_syntax = $reg_start . $user_info_part . $host_part . $port_part . $reg_end; |
530
|
|
|
|
531
|
24 |
|
preg_match($authority_syntax, $authority, $authority_parts); |
532
|
|
|
|
533
|
24 |
|
if (isset($authority_parts["port"])) { |
534
|
10 |
|
$authority_parts["port"] = (int) $authority_parts["port"]; |
535
|
10 |
|
} |
536
|
|
|
|
537
|
24 |
|
return $authority_parts; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Normalizes a port string based on whether the URI's port is standard for its scheme |
542
|
|
|
* |
543
|
|
|
* @return int|null Returns null if the port is standard for the scheme |
544
|
|
|
* Returns the port prepended with a colon if the port is not standard for the scheme |
545
|
|
|
*/ |
546
|
8 |
|
private function normalizePort() |
547
|
|
|
{ |
548
|
8 |
|
$scheme_port_array = new ArrayHelper($this->scheme_ports); |
549
|
|
|
|
550
|
8 |
|
$standard_port = $scheme_port_array->valueLookup($this->uri_parts["scheme"]); |
551
|
|
|
|
552
|
8 |
|
if ($this->uri_parts["port"] == $standard_port) { |
553
|
5 |
|
$normalized_port = null; |
554
|
5 |
|
} else { |
555
|
3 |
|
$normalized_port = $this->uri_parts["port"]; |
556
|
|
|
} |
557
|
|
|
|
558
|
8 |
|
return $normalized_port; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Sanitizes the URI component array by removing redundant key/value pairs |
563
|
|
|
* |
564
|
|
|
* @return void |
565
|
|
|
*/ |
566
|
28 |
|
private function sanitizeUriPartsArray() |
567
|
|
|
{ |
568
|
28 |
|
$uri_part_array = new ArrayHelper($this->uri_parts); |
569
|
|
|
|
570
|
28 |
|
$this->uri_parts = $uri_part_array->removeNumericKeys(); |
571
|
28 |
|
} |
572
|
|
|
} |
573
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.