1 | <?php |
||
9 | class Uri implements UriInterface |
||
10 | { |
||
11 | /** @var string */ |
||
12 | private $uri; |
||
13 | |||
14 | /** @var string */ |
||
15 | private $scheme; |
||
16 | |||
17 | /** @var string */ |
||
18 | private $host; |
||
19 | |||
20 | /** @var string */ |
||
21 | private $port; |
||
22 | |||
23 | /** @var string */ |
||
24 | private $user; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $path; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $query; |
||
31 | |||
32 | /** @var string */ |
||
33 | private $fragment; |
||
34 | |||
35 | public function __construct(string $uri) |
||
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 | public function getScheme() |
||
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 | public function getAuthority() |
||
93 | |||
94 | /** |
||
95 | * Retrieve the user information component of the URI. |
||
96 | * |
||
97 | * If no user information is present, this method MUST return an empty |
||
98 | * string. |
||
99 | * |
||
100 | * If a user is present in the URI, this will return that value; |
||
101 | * additionally, if the password is also present, it will be appended to the |
||
102 | * user value, with a colon (":") separating the values. |
||
103 | * |
||
104 | * The trailing "@" character is not part of the user information and MUST |
||
105 | * NOT be added. |
||
106 | * |
||
107 | * @return string The URI user information, in "username[:password]" format. |
||
108 | */ |
||
109 | public function getUserInfo() |
||
113 | |||
114 | /** |
||
115 | * Retrieve the host component of the URI. |
||
116 | * |
||
117 | * If no host is present, this method MUST return an empty string. |
||
118 | * |
||
119 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
120 | * Section 3.2.2. |
||
121 | * |
||
122 | * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
123 | * @return string The URI host. |
||
124 | */ |
||
125 | public function getHost() |
||
129 | |||
130 | /** |
||
131 | * Retrieve the port component of the URI. |
||
132 | * |
||
133 | * If a port is present, and it is non-standard for the current scheme, |
||
134 | * this method MUST return it as an integer. If the port is the standard port |
||
135 | * used with the current scheme, this method SHOULD return null. |
||
136 | * |
||
137 | * If no port is present, and no scheme is present, this method MUST return |
||
138 | * a null value. |
||
139 | * |
||
140 | * If no port is present, but a scheme is present, this method MAY return |
||
141 | * the standard port for that scheme, but SHOULD return null. |
||
142 | * |
||
143 | * @return null|int The URI port. |
||
144 | */ |
||
145 | public function getPort() |
||
149 | |||
150 | /** |
||
151 | * Retrieve the path component of the URI. |
||
152 | * |
||
153 | * The path can either be empty or absolute (starting with a slash) or |
||
154 | * rootless (not starting with a slash). Implementations MUST support all |
||
155 | * three syntaxes. |
||
156 | * |
||
157 | * Normally, the empty path "" and absolute path "/" are considered equal as |
||
158 | * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
||
159 | * do this normalization because in contexts with a trimmed base path, e.g. |
||
160 | * the front controller, this difference becomes significant. It's the task |
||
161 | * of the user to handle both "" and "/". |
||
162 | * |
||
163 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
164 | * any characters. To determine what characters to encode, please refer to |
||
165 | * RFC 3986, Sections 2 and 3.3. |
||
166 | * |
||
167 | * As an example, if the value should include a slash ("/") not intended as |
||
168 | * delimiter between path segments, that value MUST be passed in encoded |
||
169 | * form (e.g., "%2F") to the instance. |
||
170 | * |
||
171 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
172 | * @see https://tools.ietf.org/html/rfc3986#section-3.3 |
||
173 | * @return string The URI path. |
||
174 | */ |
||
175 | public function getPath() |
||
179 | |||
180 | /** |
||
181 | * Retrieve the query string of the URI. |
||
182 | * |
||
183 | * If no query string is present, this method MUST return an empty string. |
||
184 | * |
||
185 | * The leading "?" character is not part of the query and MUST NOT be |
||
186 | * added. |
||
187 | * |
||
188 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
189 | * any characters. To determine what characters to encode, please refer to |
||
190 | * RFC 3986, Sections 2 and 3.4. |
||
191 | * |
||
192 | * As an example, if a value in a key/value pair of the query string should |
||
193 | * include an ampersand ("&") not intended as a delimiter between values, |
||
194 | * that value MUST be passed in encoded form (e.g., "%26") to the instance. |
||
195 | * |
||
196 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
197 | * @see https://tools.ietf.org/html/rfc3986#section-3.4 |
||
198 | * @return string The URI query string. |
||
199 | */ |
||
200 | public function getQuery() |
||
204 | |||
205 | /** |
||
206 | * Retrieve the fragment component of the URI. |
||
207 | * |
||
208 | * If no fragment is present, this method MUST return an empty string. |
||
209 | * |
||
210 | * The leading "#" character is not part of the fragment and MUST NOT be |
||
211 | * added. |
||
212 | * |
||
213 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
214 | * any characters. To determine what characters to encode, please refer to |
||
215 | * RFC 3986, Sections 2 and 3.5. |
||
216 | * |
||
217 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
218 | * @see https://tools.ietf.org/html/rfc3986#section-3.5 |
||
219 | * @return string The URI fragment. |
||
220 | */ |
||
221 | public function getFragment() |
||
225 | |||
226 | /** |
||
227 | * Return an instance with the specified scheme. |
||
228 | * |
||
229 | * This method MUST retain the state of the current instance, and return |
||
230 | * an instance that contains the specified scheme. |
||
231 | * |
||
232 | * Implementations MUST support the schemes "http" and "https" case |
||
233 | * insensitively, and MAY accommodate other schemes if required. |
||
234 | * |
||
235 | * An empty scheme is equivalent to removing the scheme. |
||
236 | * |
||
237 | * @param string $scheme The scheme to use with the new instance. |
||
238 | * @return static A new instance with the specified scheme. |
||
239 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
240 | */ |
||
241 | public function withScheme($scheme) |
||
245 | |||
246 | /** |
||
247 | * Return an instance with the specified user information. |
||
248 | * |
||
249 | * This method MUST retain the state of the current instance, and return |
||
250 | * an instance that contains the specified user information. |
||
251 | * |
||
252 | * Password is optional, but the user information MUST include the |
||
253 | * user; an empty string for the user is equivalent to removing user |
||
254 | * information. |
||
255 | * |
||
256 | * @param string $user The user name to use for authority. |
||
257 | * @param null|string $password The password associated with $user. |
||
258 | * @return static A new instance with the specified user information. |
||
259 | */ |
||
260 | public function withUserInfo($user, $password = null) |
||
264 | |||
265 | /** |
||
266 | * Return an instance with the specified host. |
||
267 | * |
||
268 | * This method MUST retain the state of the current instance, and return |
||
269 | * an instance that contains the specified host. |
||
270 | * |
||
271 | * An empty host value is equivalent to removing the host. |
||
272 | * |
||
273 | * @param string $host The hostname to use with the new instance. |
||
274 | * @return static A new instance with the specified host. |
||
275 | * @throws \InvalidArgumentException for invalid hostnames. |
||
276 | */ |
||
277 | public function withHost($host) |
||
281 | |||
282 | /** |
||
283 | * Return an instance with the specified port. |
||
284 | * |
||
285 | * This method MUST retain the state of the current instance, and return |
||
286 | * an instance that contains the specified port. |
||
287 | * |
||
288 | * Implementations MUST raise an exception for ports outside the |
||
289 | * established TCP and UDP port ranges. |
||
290 | * |
||
291 | * A null value provided for the port is equivalent to removing the port |
||
292 | * information. |
||
293 | * |
||
294 | * @param null|int $port The port to use with the new instance; a null value |
||
295 | * removes the port information. |
||
296 | * @return static A new instance with the specified port. |
||
297 | * @throws \InvalidArgumentException for invalid ports. |
||
298 | */ |
||
299 | public function withPort($port) |
||
303 | |||
304 | /** |
||
305 | * Return an instance with the specified path. |
||
306 | * |
||
307 | * This method MUST retain the state of the current instance, and return |
||
308 | * an instance that contains the specified path. |
||
309 | * |
||
310 | * The path can either be empty or absolute (starting with a slash) or |
||
311 | * rootless (not starting with a slash). Implementations MUST support all |
||
312 | * three syntaxes. |
||
313 | * |
||
314 | * If the path is intended to be domain-relative rather than path relative then |
||
315 | * it must begin with a slash ("/"). Paths not starting with a slash ("/") |
||
316 | * are assumed to be relative to some base path known to the application or |
||
317 | * consumer. |
||
318 | * |
||
319 | * Users can provide both encoded and decoded path characters. |
||
320 | * Implementations ensure the correct encoding as outlined in getPath(). |
||
321 | * |
||
322 | * @param string $path The path to use with the new instance. |
||
323 | * @return static A new instance with the specified path. |
||
324 | * @throws \InvalidArgumentException for invalid paths. |
||
325 | */ |
||
326 | public function withPath($path) |
||
330 | |||
331 | /** |
||
332 | * Return an instance with the specified query string. |
||
333 | * |
||
334 | * This method MUST retain the state of the current instance, and return |
||
335 | * an instance that contains the specified query string. |
||
336 | * |
||
337 | * Users can provide both encoded and decoded query characters. |
||
338 | * Implementations ensure the correct encoding as outlined in getQuery(). |
||
339 | * |
||
340 | * An empty query string value is equivalent to removing the query string. |
||
341 | * |
||
342 | * @param string $query The query string to use with the new instance. |
||
343 | * @return static A new instance with the specified query string. |
||
344 | * @throws \InvalidArgumentException for invalid query strings. |
||
345 | */ |
||
346 | public function withQuery($query) |
||
350 | |||
351 | /** |
||
352 | * Return an instance with the specified URI fragment. |
||
353 | * |
||
354 | * This method MUST retain the state of the current instance, and return |
||
355 | * an instance that contains the specified URI fragment. |
||
356 | * |
||
357 | * Users can provide both encoded and decoded fragment characters. |
||
358 | * Implementations ensure the correct encoding as outlined in getFragment(). |
||
359 | * |
||
360 | * An empty fragment value is equivalent to removing the fragment. |
||
361 | * |
||
362 | * @param string $fragment The fragment to use with the new instance. |
||
363 | * @return static A new instance with the specified fragment. |
||
364 | */ |
||
365 | public function withFragment($fragment) |
||
369 | |||
370 | /** |
||
371 | * Return the string representation as a URI reference. |
||
372 | * |
||
373 | * Depending on which components of the URI are present, the resulting |
||
374 | * string is either a full URI or relative reference according to RFC 3986, |
||
375 | * Section 4.1. The method concatenates the various components of the URI, |
||
376 | * using the appropriate delimiters: |
||
377 | * |
||
378 | * - If a scheme is present, it MUST be suffixed by ":". |
||
379 | * - If an authority is present, it MUST be prefixed by "//". |
||
380 | * - The path can be concatenated without delimiters. But there are two |
||
381 | * cases where the path has to be adjusted to make the URI reference |
||
382 | * valid as PHP does not allow to throw an exception in __toString(): |
||
383 | * - If the path is rootless and an authority is present, the path MUST |
||
384 | * be prefixed by "/". |
||
385 | * - If the path is starting with more than one "/" and no authority is |
||
386 | * present, the starting slashes MUST be reduced to one. |
||
387 | * - If a query is present, it MUST be prefixed by "?". |
||
388 | * - If a fragment is present, it MUST be prefixed by "#". |
||
389 | * |
||
390 | * @see http://tools.ietf.org/html/rfc3986#section-4.1 |
||
391 | * @return string |
||
392 | */ |
||
393 | public function __toString() |
||
397 | } |
||
398 |