1 | <?php |
||
18 | class Uri implements UriInterface |
||
19 | { |
||
20 | /** @var Scheme */ |
||
21 | protected $scheme; |
||
22 | /** @var Authority */ |
||
23 | protected $authority; |
||
24 | /** @var UserInfo */ |
||
25 | protected $user_info; |
||
26 | /** @var Host */ |
||
27 | protected $host; |
||
28 | /** @var Port */ |
||
29 | protected $port; |
||
30 | /** @var Path */ |
||
31 | protected $path; |
||
32 | /** @var Query */ |
||
33 | protected $query; |
||
34 | /** @var Fragment */ |
||
35 | protected $fragment; |
||
36 | |||
37 | protected $sub_delims = array( |
||
38 | "!", |
||
39 | "$", |
||
40 | "&", |
||
41 | "'", |
||
42 | "(", |
||
43 | ")", |
||
44 | "*", |
||
45 | "+", |
||
46 | ",", |
||
47 | ";", |
||
48 | "=", |
||
49 | ); |
||
50 | |||
51 | protected $pchar_unencoded = array( |
||
52 | ":", |
||
53 | "@", |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * Uri constructor. Accepts a string representing a URI and parses the string into the URI's component parts. |
||
58 | * |
||
59 | * @throws \InvalidArgumentException Throws an \InvalidArgumentException when its parameter is not a string |
||
60 | * @param string $uri |
||
61 | */ |
||
62 | 59 | public function __construct($uri) |
|
63 | { |
||
64 | 59 | if (!is_string($uri)) { |
|
65 | 6 | throw new \InvalidArgumentException("New Uri objects must be constructed with a string URI"); |
|
66 | } |
||
67 | |||
68 | 53 | $this->explodeUri($uri); |
|
69 | 53 | } |
|
70 | |||
71 | /** |
||
72 | * Retrieve the scheme component of the URI. |
||
73 | * |
||
74 | * If no scheme is present, this method MUST return an empty string. |
||
75 | * |
||
76 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
77 | * Section 3.1. |
||
78 | * |
||
79 | * The trailing ":" character is not part of the scheme and MUST NOT be |
||
80 | * added. |
||
81 | * |
||
82 | * @see https://tools.ietf.org/html/rfc3986#section-3.1 |
||
83 | * @return string The URI scheme. |
||
84 | */ |
||
85 | 25 | public function getScheme() |
|
89 | |||
90 | /** |
||
91 | * Retrieve the authority component of the URI. |
||
92 | * |
||
93 | * If no authority information is present, this method MUST return an empty |
||
94 | * string. |
||
95 | * |
||
96 | * The authority syntax of the URI is: |
||
97 | * |
||
98 | * <pre> |
||
99 | * [user-info@]host[:port] |
||
100 | * </pre> |
||
101 | * |
||
102 | * If the port component is not set or is the standard port for the current |
||
103 | * scheme, it SHOULD NOT be included. |
||
104 | * |
||
105 | * @see https://tools.ietf.org/html/rfc3986#section-3.2 |
||
106 | * @return string The URI authority, in "[user-info@]host[:port]" format. |
||
107 | */ |
||
108 | 31 | public function getAuthority() |
|
112 | |||
113 | /** |
||
114 | * Retrieve the user information component of the URI. |
||
115 | * |
||
116 | * If no user information is present, this method MUST return an empty |
||
117 | * string. |
||
118 | * |
||
119 | * If a user is present in the URI, this will return that value; |
||
120 | * additionally, if the password is also present, it will be appended to the |
||
121 | * user value, with a colon (":") separating the values. |
||
122 | * |
||
123 | * The trailing "@" character is not part of the user information and MUST |
||
124 | * NOT be added. |
||
125 | * |
||
126 | * @return string The URI user information, in "username[:password]" format. |
||
127 | */ |
||
128 | 50 | public function getUserInfo() |
|
132 | |||
133 | /** |
||
134 | * Retrieve the host component of the URI. |
||
135 | * |
||
136 | * If no host is present, this method MUST return an empty string. |
||
137 | * |
||
138 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
139 | * Section 3.2.2. |
||
140 | * |
||
141 | * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
142 | * @return string The URI host. |
||
143 | */ |
||
144 | 26 | public function getHost() |
|
148 | |||
149 | /** |
||
150 | * Retrieve the port component of the URI. |
||
151 | * |
||
152 | * If a port is present, and it is non-standard for the current scheme, |
||
153 | * this method MUST return it as an integer. If the port is the standard port |
||
154 | * used with the current scheme, this method SHOULD return null. |
||
155 | * |
||
156 | * If no port is present, and no scheme is present, this method MUST return |
||
157 | * a null value. |
||
158 | * |
||
159 | * If no port is present, but a scheme is present, this method MAY return |
||
160 | * the standard port for that scheme, but SHOULD return null. |
||
161 | * |
||
162 | * @return null|int The URI port. |
||
163 | */ |
||
164 | 28 | public function getPort() |
|
170 | |||
171 | /** |
||
172 | * Retrieve the path component of the URI. |
||
173 | * |
||
174 | * The path can either be empty or absolute (starting with a slash) or |
||
175 | * rootless (not starting with a slash). Implementations MUST support all |
||
176 | * three syntaxes. |
||
177 | * |
||
178 | * Normally, the empty path "" and absolute path "/" are considered equal as |
||
179 | * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
||
180 | * do this normalization because in contexts with a trimmed base path, e.g. |
||
181 | * the front controller, this difference becomes significant. It's the task |
||
182 | * of the user to handle both "" and "/". |
||
183 | * |
||
184 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
185 | * any characters. To determine what characters to encode, please refer to |
||
186 | * RFC 3986, Sections 2 and 3.3. |
||
187 | * |
||
188 | * As an example, if the value should include a slash ("/") not intended as |
||
189 | * delimiter between path segments, that value MUST be passed in encoded |
||
190 | * form (e.g., "%2F") to the instance. |
||
191 | * |
||
192 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
193 | * @see https://tools.ietf.org/html/rfc3986#section-3.3 |
||
194 | * @return string The URI path. |
||
195 | */ |
||
196 | 30 | public function getPath() |
|
200 | |||
201 | /** |
||
202 | * Retrieve the query string of the URI. |
||
203 | * |
||
204 | * If no query string is present, this method MUST return an empty string. |
||
205 | * |
||
206 | * The leading "?" character is not part of the query and MUST NOT be |
||
207 | * added. |
||
208 | * |
||
209 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
210 | * any characters. To determine what characters to encode, please refer to |
||
211 | * RFC 3986, Sections 2 and 3.4. |
||
212 | * |
||
213 | * As an example, if a value in a key/value pair of the query string should |
||
214 | * include an ampersand ("&") not intended as a delimiter between values, |
||
215 | * that value MUST be passed in encoded form (e.g., "%26") to the instance. |
||
216 | * |
||
217 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
218 | * @see https://tools.ietf.org/html/rfc3986#section-3.4 |
||
219 | * @return string The URI query string. |
||
220 | */ |
||
221 | 24 | public function getQuery() |
|
225 | |||
226 | /** |
||
227 | * Retrieve the fragment component of the URI. |
||
228 | * |
||
229 | * If no fragment is present, this method MUST return an empty string. |
||
230 | * |
||
231 | * The leading "#" character is not part of the fragment and MUST NOT be |
||
232 | * added. |
||
233 | * |
||
234 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
235 | * any characters. To determine what characters to encode, please refer to |
||
236 | * RFC 3986, Sections 2 and 3.5. |
||
237 | * |
||
238 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
239 | * @see https://tools.ietf.org/html/rfc3986#section-3.5 |
||
240 | * @return string The URI fragment. |
||
241 | */ |
||
242 | 24 | public function getFragment() |
|
246 | |||
247 | /** |
||
248 | * Return an instance with the specified scheme. |
||
249 | * |
||
250 | * This method MUST retain the state of the current instance, and return |
||
251 | * an instance that contains the specified scheme. |
||
252 | * |
||
253 | * Implementations MUST support the schemes "http" and "https" case |
||
254 | * insensitively, and MAY accommodate other schemes if required. |
||
255 | * |
||
256 | * An empty scheme is equivalent to removing the scheme. |
||
257 | * |
||
258 | * @param string $scheme The scheme to use with the new instance. |
||
259 | * @return static A new instance with the specified scheme. |
||
260 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
261 | */ |
||
262 | 13 | public function withScheme($scheme) |
|
272 | |||
273 | /** |
||
274 | * Return an instance with the specified authority. |
||
275 | * |
||
276 | * This method MUST retain the state of the current instance, and return |
||
277 | * an instance that contains the specified authority. |
||
278 | * |
||
279 | * Replacing the authority is equivalent to replacing or removing all authority components depending upon the |
||
280 | * composition of the authority. |
||
281 | * |
||
282 | * An empty authority is equivalent to removing the authority and all authority components. |
||
283 | * |
||
284 | * @param string $authority The scheme to use with the new instance. |
||
285 | * @return static A new instance with the specified authority. |
||
286 | * @throws \InvalidArgumentException for invalid authorities. |
||
287 | */ |
||
288 | 46 | public function withAuthority($authority) |
|
302 | |||
303 | /** |
||
304 | * Return an instance with the specified user information. |
||
305 | * |
||
306 | * This method MUST retain the state of the current instance, and return |
||
307 | * an instance that contains the specified user information. |
||
308 | * |
||
309 | * Password is optional, but the user information MUST include the |
||
310 | * user; an empty string for the user is equivalent to removing user |
||
311 | * information. |
||
312 | * |
||
313 | * @param string $user The user name to use for authority. |
||
314 | * @param null|string $password The password associated with $user. |
||
315 | * @return static A new instance with the specified user information. |
||
316 | */ |
||
317 | 8 | public function withUserInfo($user, $password = null) |
|
333 | |||
334 | /** |
||
335 | * Return an instance with the specified host. |
||
336 | * |
||
337 | * This method MUST retain the state of the current instance, and return |
||
338 | * an instance that contains the specified host. |
||
339 | * |
||
340 | * An empty host value is equivalent to removing the host, which removes the entire authority. |
||
341 | * |
||
342 | * @param string $host The hostname to use with the new instance. |
||
343 | * @return static A new instance with the specified host. |
||
344 | * @throws \InvalidArgumentException for invalid hostnames. |
||
345 | */ |
||
346 | 14 | public function withHost($host) |
|
364 | |||
365 | /** |
||
366 | * Return an instance with the specified port. |
||
367 | * |
||
368 | * This method MUST retain the state of the current instance, and return |
||
369 | * an instance that contains the specified port. |
||
370 | * |
||
371 | * Implementations MUST raise an exception for ports outside the |
||
372 | * established TCP and UDP port ranges. |
||
373 | * |
||
374 | * A null value provided for the port is equivalent to removing the port |
||
375 | * information. |
||
376 | * |
||
377 | * @param null|int $port The port to use with the new instance; a null value |
||
378 | * removes the port information. |
||
379 | * @return static A new instance with the specified port. |
||
380 | * @throws \InvalidArgumentException for invalid ports. |
||
381 | */ |
||
382 | 15 | public function withPort($port) |
|
392 | |||
393 | /** |
||
394 | * Return an instance with the specified path. |
||
395 | * |
||
396 | * This method MUST retain the state of the current instance, and return |
||
397 | * an instance that contains the specified path. |
||
398 | * |
||
399 | * The path can either be empty or absolute (starting with a slash) or |
||
400 | * rootless (not starting with a slash). Implementations MUST support all |
||
401 | * three syntaxes. |
||
402 | * |
||
403 | * If the path is intended to be domain-relative rather than path relative then |
||
404 | * it must begin with a slash ("/"). Paths not starting with a slash ("/") |
||
405 | * are assumed to be relative to some base path known to the application or |
||
406 | * consumer. |
||
407 | * |
||
408 | * Users can provide both encoded and decoded path characters. |
||
409 | * Implementations ensure the correct encoding as outlined in getPath(). |
||
410 | * |
||
411 | * @throws \InvalidArgumentException for invalid paths and invalid path/authority combinations. |
||
412 | * |
||
413 | * @param string $path The path to use with the new instance. |
||
414 | * @return static A new instance with the specified path. |
||
415 | */ |
||
416 | 18 | public function withPath($path) |
|
430 | |||
431 | /** |
||
432 | * Return an instance with the specified query string. |
||
433 | * |
||
434 | * This method MUST retain the state of the current instance, and return |
||
435 | * an instance that contains the specified query string. |
||
436 | * |
||
437 | * Users can provide both encoded and decoded query characters. |
||
438 | * Implementations ensure the correct encoding as outlined in getQuery(). |
||
439 | * |
||
440 | * An empty query string value is equivalent to removing the query string. |
||
441 | * |
||
442 | * @param string $query The query string to use with the new instance. |
||
443 | * @return static A new instance with the specified query string. |
||
444 | * @throws \InvalidArgumentException for invalid query strings. |
||
445 | */ |
||
446 | 11 | public function withQuery($query) |
|
456 | |||
457 | /** |
||
458 | * Return an instance with the specified URI fragment. |
||
459 | * |
||
460 | * This method MUST retain the state of the current instance, and return |
||
461 | * an instance that contains the specified URI fragment. |
||
462 | * |
||
463 | * Users can provide both encoded and decoded fragment characters. |
||
464 | * Implementations ensure the correct encoding as outlined in getFragment(). |
||
465 | * |
||
466 | * An empty fragment value is equivalent to removing the fragment. |
||
467 | * |
||
468 | * @param string $fragment The fragment to use with the new instance. |
||
469 | * @return static A new instance with the specified fragment. |
||
470 | */ |
||
471 | 4 | public function withFragment($fragment) |
|
481 | |||
482 | /** |
||
483 | * Return the string representation as a URI reference. |
||
484 | * |
||
485 | * Depending on which components of the URI are present, the resulting |
||
486 | * string is either a full URI or relative reference according to RFC 3986, |
||
487 | * Section 4.1. The method concatenates the various components of the URI, |
||
488 | * using the appropriate delimiters: |
||
489 | * |
||
490 | * - If a scheme is present, it MUST be suffixed by ":". |
||
491 | * - If an authority is present, it MUST be prefixed by "//". |
||
492 | * - The path can be concatenated without delimiters. But there are two |
||
493 | * cases where the path has to be adjusted to make the URI reference |
||
494 | * valid as PHP does not allow to throw an exception in __toString(): |
||
495 | * - If the path is rootless and an authority is present, the path MUST |
||
496 | * be prefixed by "/". |
||
497 | * - If the path is starting with more than one "/" and no authority is |
||
498 | * present, the starting slashes MUST be reduced to one. |
||
499 | * - If a query is present, it MUST be prefixed by "?". |
||
500 | * - If a fragment is present, it MUST be prefixed by "#". |
||
501 | * |
||
502 | * @see http://tools.ietf.org/html/rfc3986#section-4.1 |
||
503 | * @return string |
||
504 | */ |
||
505 | 16 | public function __toString() |
|
521 | |||
522 | /** |
||
523 | * Splits a string URI into its component parts, returning true if the URI string matches a valid URI's syntax |
||
524 | * and false if the URI string does not |
||
525 | * |
||
526 | * @param string $uri The URI string to be decomposed |
||
527 | * @return bool Returns true if the URI string matches a valid URI's syntax |
||
528 | * Returns false otherwise |
||
529 | */ |
||
530 | 53 | private function explodeUri($uri) |
|
552 | |||
553 | /** |
||
554 | * Compiles a regular expression to match URI component parts |
||
555 | * |
||
556 | * @return string The regular expression to match URI component parts |
||
557 | */ |
||
558 | 53 | private function compileUriRegEx() |
|
570 | |||
571 | /** |
||
572 | * Parses the results of exploding URI parts. Creates component objects. |
||
573 | * |
||
574 | * @param string[] $uri_parts An array of strings representing the parts of a uri |
||
575 | * |
||
576 | * @return void |
||
577 | */ |
||
578 | 53 | private function parseExplosions($uri_parts) |
|
589 | } |
||
590 |