1 | <?php |
||
19 | class Uri implements UriInterface |
||
20 | { |
||
21 | protected $uri_parts = array( //TODO: Probably don't need this anymore; refactor tests and remove |
||
|
|||
22 | "scheme" => "", |
||
23 | "hier_part" => "", |
||
24 | "authority" => "", |
||
25 | "user_info" => "", |
||
26 | "host" => "", |
||
27 | "port" => null, |
||
28 | "path" => "", |
||
29 | "query" => "", |
||
30 | "fragment" => "", |
||
31 | ); |
||
32 | |||
33 | /** @var Scheme */ |
||
34 | protected $scheme; |
||
35 | /** @var Authority */ |
||
36 | protected $authority; |
||
37 | /** @var UserInfo */ |
||
38 | protected $user_info; |
||
39 | /** @var Host */ |
||
40 | protected $host; |
||
41 | /** @var Port */ |
||
42 | protected $port; |
||
43 | /** @var Path */ |
||
44 | protected $path; |
||
45 | /** @var Query */ |
||
46 | protected $query; |
||
47 | /** @var Fragment */ |
||
48 | protected $fragment; |
||
49 | |||
50 | protected $sub_delims = array( |
||
51 | "!", |
||
52 | "$", |
||
53 | "&", |
||
54 | "'", |
||
55 | "(", |
||
56 | ")", |
||
57 | "*", |
||
58 | "+", |
||
59 | ",", |
||
60 | ";", |
||
61 | "=", |
||
62 | ); |
||
63 | |||
64 | protected $pchar_unencoded = array( |
||
65 | ":", |
||
66 | "@", |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * Uri constructor. Accepts a string representing a URI and parses the string into the URI's component parts. |
||
71 | * |
||
72 | * @throws \InvalidArgumentException Throws an \InvalidArgumentException when its parameter is not a string |
||
73 | * @param string $uri |
||
74 | */ |
||
75 | 78 | public function __construct($uri) |
|
83 | |||
84 | /** |
||
85 | * Retrieve the parsed components of the URI string. |
||
86 | * |
||
87 | * If the class was provided an invalid or empty URI string, URI components will be empty strings, except port, |
||
88 | * which will be null |
||
89 | * |
||
90 | * @return mixed[] |
||
91 | */ |
||
92 | 43 | public function getParsedUri() |
|
93 | { |
||
94 | 43 | return $this->uri_parts; |
|
95 | 10 | } |
|
96 | |||
97 | /** |
||
98 | * Retrieve the scheme component of the URI. |
||
99 | * |
||
100 | * If no scheme is present, this method MUST return an empty string. |
||
101 | * |
||
102 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
103 | * Section 3.1. |
||
104 | * |
||
105 | * The trailing ":" character is not part of the scheme and MUST NOT be |
||
106 | * added. |
||
107 | * |
||
108 | * @see https://tools.ietf.org/html/rfc3986#section-3.1 |
||
109 | * @return string The URI scheme. |
||
110 | */ |
||
111 | 3 | public function getScheme() |
|
115 | |||
116 | /** |
||
117 | * Retrieve the authority component of the URI. |
||
118 | * |
||
119 | * If no authority information is present, this method MUST return an empty |
||
120 | * string. |
||
121 | * |
||
122 | * The authority syntax of the URI is: |
||
123 | * |
||
124 | * <pre> |
||
125 | * [user-info@]host[:port] |
||
126 | * </pre> |
||
127 | * |
||
128 | * If the port component is not set or is the standard port for the current |
||
129 | * scheme, it SHOULD NOT be included. |
||
130 | * |
||
131 | * @see https://tools.ietf.org/html/rfc3986#section-3.2 |
||
132 | * @return string The URI authority, in "[user-info@]host[:port]" format. |
||
133 | */ |
||
134 | 7 | public function getAuthority() |
|
138 | |||
139 | /** |
||
140 | * Retrieve the user information component of the URI. |
||
141 | * |
||
142 | * If no user information is present, this method MUST return an empty |
||
143 | * string. |
||
144 | * |
||
145 | * If a user is present in the URI, this will return that value; |
||
146 | * additionally, if the password is also present, it will be appended to the |
||
147 | * user value, with a colon (":") separating the values. |
||
148 | * |
||
149 | * The trailing "@" character is not part of the user information and MUST |
||
150 | * NOT be added. |
||
151 | * |
||
152 | * @return string The URI user information, in "username[:password]" format. |
||
153 | */ |
||
154 | 5 | public function getUserInfo() |
|
158 | |||
159 | /** |
||
160 | * Retrieve the host component of the URI. |
||
161 | * |
||
162 | * If no host is present, this method MUST return an empty string. |
||
163 | * |
||
164 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
165 | * Section 3.2.2. |
||
166 | * |
||
167 | * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
168 | * @return string The URI host. |
||
169 | */ |
||
170 | 5 | public function getHost() |
|
174 | |||
175 | /** |
||
176 | * Retrieve the port component of the URI. |
||
177 | * |
||
178 | * If a port is present, and it is non-standard for the current scheme, |
||
179 | * this method MUST return it as an integer. If the port is the standard port |
||
180 | * used with the current scheme, this method SHOULD return null. |
||
181 | * |
||
182 | * If no port is present, and no scheme is present, this method MUST return |
||
183 | * a null value. |
||
184 | * |
||
185 | * If no port is present, but a scheme is present, this method MAY return |
||
186 | * the standard port for that scheme, but SHOULD return null. |
||
187 | * |
||
188 | * @return null|int The URI port. |
||
189 | */ |
||
190 | 7 | public function getPort() |
|
196 | |||
197 | /** |
||
198 | * Retrieve the path component of the URI. |
||
199 | * |
||
200 | * The path can either be empty or absolute (starting with a slash) or |
||
201 | * rootless (not starting with a slash). Implementations MUST support all |
||
202 | * three syntaxes. |
||
203 | * |
||
204 | * Normally, the empty path "" and absolute path "/" are considered equal as |
||
205 | * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
||
206 | * do this normalization because in contexts with a trimmed base path, e.g. |
||
207 | * the front controller, this difference becomes significant. It's the task |
||
208 | * of the user to handle both "" and "/". |
||
209 | * |
||
210 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
211 | * any characters. To determine what characters to encode, please refer to |
||
212 | * RFC 3986, Sections 2 and 3.3. |
||
213 | * |
||
214 | * As an example, if the value should include a slash ("/") not intended as |
||
215 | * delimiter between path segments, that value MUST be passed in encoded |
||
216 | * form (e.g., "%2F") to the instance. |
||
217 | * |
||
218 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
219 | * @see https://tools.ietf.org/html/rfc3986#section-3.3 |
||
220 | * @return string The URI path. |
||
221 | */ |
||
222 | 8 | public function getPath() |
|
226 | |||
227 | /** |
||
228 | * Retrieve the query string of the URI. |
||
229 | * |
||
230 | * If no query string is present, this method MUST return an empty string. |
||
231 | * |
||
232 | * The leading "?" character is not part of the query and MUST NOT be |
||
233 | * added. |
||
234 | * |
||
235 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
236 | * any characters. To determine what characters to encode, please refer to |
||
237 | * RFC 3986, Sections 2 and 3.4. |
||
238 | * |
||
239 | * As an example, if a value in a key/value pair of the query string should |
||
240 | * include an ampersand ("&") not intended as a delimiter between values, |
||
241 | * that value MUST be passed in encoded form (e.g., "%26") to the instance. |
||
242 | * |
||
243 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
244 | * @see https://tools.ietf.org/html/rfc3986#section-3.4 |
||
245 | * @return string The URI query string. |
||
246 | */ |
||
247 | 4 | public function getQuery() |
|
251 | |||
252 | /** |
||
253 | * Retrieve the fragment component of the URI. |
||
254 | * |
||
255 | * If no fragment is present, this method MUST return an empty string. |
||
256 | * |
||
257 | * The leading "#" character is not part of the fragment and MUST NOT be |
||
258 | * added. |
||
259 | * |
||
260 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
261 | * any characters. To determine what characters to encode, please refer to |
||
262 | * RFC 3986, Sections 2 and 3.5. |
||
263 | * |
||
264 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
265 | * @see https://tools.ietf.org/html/rfc3986#section-3.5 |
||
266 | * @return string The URI fragment. |
||
267 | */ |
||
268 | 4 | public function getFragment() |
|
272 | |||
273 | /** |
||
274 | * Return an instance with the specified scheme. |
||
275 | * |
||
276 | * This method MUST retain the state of the current instance, and return |
||
277 | * an instance that contains the specified scheme. |
||
278 | * |
||
279 | * Implementations MUST support the schemes "http" and "https" case |
||
280 | * insensitively, and MAY accommodate other schemes if required. |
||
281 | * |
||
282 | * An empty scheme is equivalent to removing the scheme. |
||
283 | * |
||
284 | * @param string $scheme The scheme to use with the new instance. |
||
285 | * @return static A new instance with the specified scheme. |
||
286 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
287 | */ |
||
288 | public function withScheme($scheme) |
||
292 | |||
293 | /** |
||
294 | * Return an instance with the specified authority. |
||
295 | * |
||
296 | * This method MUST retain the state of the current instance, and return |
||
297 | * an instance that contains the specified authority. |
||
298 | * |
||
299 | * Replacing the authority is equivalent to replacing or removing all authority components depending upon the |
||
300 | * composition of the authority. |
||
301 | * |
||
302 | * An empty authority is equivalent to removing the authority and all authority components. |
||
303 | * |
||
304 | * @param string $authority The scheme to use with the new instance. |
||
305 | * @return static A new instance with the specified authority. |
||
306 | * @throws \InvalidArgumentException for invalid authorities. |
||
307 | */ |
||
308 | 21 | public function withAuthority($authority) |
|
322 | |||
323 | /** |
||
324 | * Return an instance with the specified user information. |
||
325 | * |
||
326 | * This method MUST retain the state of the current instance, and return |
||
327 | * an instance that contains the specified user information. |
||
328 | * |
||
329 | * Password is optional, but the user information MUST include the |
||
330 | * user; an empty string for the user is equivalent to removing user |
||
331 | * information. |
||
332 | * |
||
333 | * @param string $user The user name to use for authority. |
||
334 | * @param null|string $password The password associated with $user. |
||
335 | * @return static A new instance with the specified user information. |
||
336 | */ |
||
337 | public function withUserInfo($user, $password = null) |
||
341 | |||
342 | /** |
||
343 | * Return an instance with the specified host. |
||
344 | * |
||
345 | * This method MUST retain the state of the current instance, and return |
||
346 | * an instance that contains the specified host. |
||
347 | * |
||
348 | * An empty host value is equivalent to removing the host. |
||
349 | * |
||
350 | * @param string $host The hostname to use with the new instance. |
||
351 | * @return static A new instance with the specified host. |
||
352 | * @throws \InvalidArgumentException for invalid hostnames. |
||
353 | */ |
||
354 | public function withHost($host) |
||
358 | |||
359 | /** |
||
360 | * Return an instance with the specified port. |
||
361 | * |
||
362 | * This method MUST retain the state of the current instance, and return |
||
363 | * an instance that contains the specified port. |
||
364 | * |
||
365 | * Implementations MUST raise an exception for ports outside the |
||
366 | * established TCP and UDP port ranges. |
||
367 | * |
||
368 | * A null value provided for the port is equivalent to removing the port |
||
369 | * information. |
||
370 | * |
||
371 | * @param null|int $port The port to use with the new instance; a null value |
||
372 | * removes the port information. |
||
373 | * @return static A new instance with the specified port. |
||
374 | * @throws \InvalidArgumentException for invalid ports. |
||
375 | */ |
||
376 | public function withPort($port) |
||
380 | |||
381 | /** |
||
382 | * Return an instance with the specified path. |
||
383 | * |
||
384 | * This method MUST retain the state of the current instance, and return |
||
385 | * an instance that contains the specified path. |
||
386 | * |
||
387 | * The path can either be empty or absolute (starting with a slash) or |
||
388 | * rootless (not starting with a slash). Implementations MUST support all |
||
389 | * three syntaxes. |
||
390 | * |
||
391 | * If the path is intended to be domain-relative rather than path relative then |
||
392 | * it must begin with a slash ("/"). Paths not starting with a slash ("/") |
||
393 | * are assumed to be relative to some base path known to the application or |
||
394 | * consumer. |
||
395 | * |
||
396 | * Users can provide both encoded and decoded path characters. |
||
397 | * Implementations ensure the correct encoding as outlined in getPath(). |
||
398 | * |
||
399 | * @throws \InvalidArgumentException for invalid paths and invalid path/authority combinations. |
||
400 | * |
||
401 | * @param string $path The path to use with the new instance. |
||
402 | * @return static A new instance with the specified path. |
||
403 | */ |
||
404 | 18 | public function withPath($path) |
|
420 | |||
421 | /** |
||
422 | * Return an instance with the specified query string. |
||
423 | * |
||
424 | * This method MUST retain the state of the current instance, and return |
||
425 | * an instance that contains the specified query string. |
||
426 | * |
||
427 | * Users can provide both encoded and decoded query characters. |
||
428 | * Implementations ensure the correct encoding as outlined in getQuery(). |
||
429 | * |
||
430 | * An empty query string value is equivalent to removing the query string. |
||
431 | * |
||
432 | * @param string $query The query string to use with the new instance. |
||
433 | * @return static A new instance with the specified query string. |
||
434 | * @throws \InvalidArgumentException for invalid query strings. |
||
435 | */ |
||
436 | public function withQuery($query) |
||
440 | |||
441 | /** |
||
442 | * Return an instance with the specified URI fragment. |
||
443 | * |
||
444 | * This method MUST retain the state of the current instance, and return |
||
445 | * an instance that contains the specified URI fragment. |
||
446 | * |
||
447 | * Users can provide both encoded and decoded fragment characters. |
||
448 | * Implementations ensure the correct encoding as outlined in getFragment(). |
||
449 | * |
||
450 | * An empty fragment value is equivalent to removing the fragment. |
||
451 | * |
||
452 | * @param string $fragment The fragment to use with the new instance. |
||
453 | * @return static A new instance with the specified fragment. |
||
454 | */ |
||
455 | public function withFragment($fragment) |
||
459 | |||
460 | /** |
||
461 | * Return the string representation as a URI reference. |
||
462 | * |
||
463 | * Depending on which components of the URI are present, the resulting |
||
464 | * string is either a full URI or relative reference according to RFC 3986, |
||
465 | * Section 4.1. The method concatenates the various components of the URI, |
||
466 | * using the appropriate delimiters: |
||
467 | * |
||
468 | * - If a scheme is present, it MUST be suffixed by ":". |
||
469 | * - If an authority is present, it MUST be prefixed by "//". |
||
470 | * - The path can be concatenated without delimiters. But there are two |
||
471 | * cases where the path has to be adjusted to make the URI reference |
||
472 | * valid as PHP does not allow to throw an exception in __toString(): |
||
473 | * - If the path is rootless and an authority is present, the path MUST |
||
474 | * be prefixed by "/". |
||
475 | * - If the path is starting with more than one "/" and no authority is |
||
476 | * present, the starting slashes MUST be reduced to one. |
||
477 | * - If a query is present, it MUST be prefixed by "?". |
||
478 | * - If a fragment is present, it MUST be prefixed by "#". |
||
479 | * |
||
480 | * @see http://tools.ietf.org/html/rfc3986#section-4.1 |
||
481 | * @return string |
||
482 | */ |
||
483 | 9 | public function __toString() |
|
499 | |||
500 | /** |
||
501 | * Splits a string URI into its component parts, returning true if the URI string matches a valid URI's syntax |
||
502 | * and false if the URI string does not |
||
503 | * |
||
504 | * @param string $uri The URI string to be decomposed |
||
505 | * @return bool Returns true if the URI string matches a valid URI's syntax |
||
506 | * Returns false otherwise |
||
507 | */ |
||
508 | 72 | private function explodeUri($uri) |
|
531 | |||
532 | /** |
||
533 | * Splits URI hierarchy data into authority and path data. |
||
534 | * |
||
535 | * @param string $hier_part The hierarchy part of a URI to be decomposed |
||
536 | * @return void |
||
537 | */ |
||
538 | 72 | private function explodeHierParts($hier_part) |
|
558 | |||
559 | /** |
||
560 | * Parses the results of exploding URI parts. Creates component objects and fills in uri_parts array with |
||
561 | * sub-component data. |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | 72 | private function parseExplosions() |
|
580 | |||
581 | /** |
||
582 | * Sanitizes the URI component array by removing redundant key/value pairs |
||
583 | * |
||
584 | * @return void |
||
585 | */ |
||
586 | 72 | private function sanitizeUriPartsArray() |
|
592 | } |
||
593 |