1: <?php
2:
3: namespace Psr\Http\Message;
4:
5: /**
6: * Value object representing a URI.
7: *
8: * This interface is meant to represent URIs according to RFC 3986 and to
9: * provide methods for most common operations. Additional functionality for
10: * working with URIs can be provided on top of the interface or externally.
11: * Its primary use is for HTTP requests, but may also be used in other
12: * contexts.
13: *
14: * Instances of this interface are considered immutable; all methods that
15: * might change state MUST be implemented such that they retain the internal
16: * state of the current instance and return an instance that contains the
17: * changed state.
18: *
19: * Typically the Host header will be also be present in the request message.
20: * For server-side requests, the scheme will typically be discoverable in the
21: * server parameters.
22: *
23: * @see http://tools.ietf.org/html/rfc3986 (the URI specification)
24: */
25: interface UriInterface
26: {
27: /**
28: * Retrieve the scheme component of the URI.
29: *
30: * If no scheme is present, this method MUST return an empty string.
31: *
32: * The value returned MUST be normalized to lowercase, per RFC 3986
33: * Section 3.1.
34: *
35: * The trailing ":" character is not part of the scheme and MUST NOT be
36: * added.
37: *
38: * @see https://tools.ietf.org/html/rfc3986#section-3.1
39: *
40: * @return string the URI scheme
41: */
42: public function getScheme();
43:
44: /**
45: * Retrieve the authority component of the URI.
46: *
47: * If no authority information is present, this method MUST return an empty
48: * string.
49: *
50: * The authority syntax of the URI is:
51: *
52: * <pre>
53: * [user-info@]host[:port]
54: * </pre>
55: *
56: * If the port component is not set or is the standard port for the current
57: * scheme, it SHOULD NOT be included.
58: *
59: * @see https://tools.ietf.org/html/rfc3986#section-3.2
60: *
61: * @return string the URI authority, in "[user-info@]host[:port]" format
62: */
63: public function getAuthority();
64:
65: /**
66: * Retrieve the user information component of the URI.
67: *
68: * If no user information is present, this method MUST return an empty
69: * string.
70: *
71: * If a user is present in the URI, this will return that value;
72: * additionally, if the password is also present, it will be appended to the
73: * user value, with a colon (":") separating the values.
74: *
75: * The trailing "@" character is not part of the user information and MUST
76: * NOT be added.
77: *
78: * @return string the URI user information, in "username[:password]" format
79: */
80: public function getUserInfo();
81:
82: /**
83: * Retrieve the host component of the URI.
84: *
85: * If no host is present, this method MUST return an empty string.
86: *
87: * The value returned MUST be normalized to lowercase, per RFC 3986
88: * Section 3.2.2.
89: *
90: * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
91: *
92: * @return string the URI host
93: */
94: public function getHost();
95:
96: /**
97: * Retrieve the port component of the URI.
98: *
99: * If a port is present, and it is non-standard for the current scheme,
100: * this method MUST return it as an integer. If the port is the standard port
101: * used with the current scheme, this method SHOULD return null.
102: *
103: * If no port is present, and no scheme is present, this method MUST return
104: * a null value.
105: *
106: * If no port is present, but a scheme is present, this method MAY return
107: * the standard port for that scheme, but SHOULD return null.
108: *
109: * @return null|int the URI port
110: */
111: public function getPort();
112:
113: /**
114: * Retrieve the path component of the URI.
115: *
116: * The path can either be empty or absolute (starting with a slash) or
117: * rootless (not starting with a slash). Implementations MUST support all
118: * three syntaxes.
119: *
120: * Normally, the empty path "" and absolute path "/" are considered equal as
121: * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
122: * do this normalization because in contexts with a trimmed base path, e.g.
123: * the front controller, this difference becomes significant. It's the task
124: * of the user to handle both "" and "/".
125: *
126: * The value returned MUST be percent-encoded, but MUST NOT double-encode
127: * any characters. To determine what characters to encode, please refer to
128: * RFC 3986, Sections 2 and 3.3.
129: *
130: * As an example, if the value should include a slash ("/") not intended as
131: * delimiter between path segments, that value MUST be passed in encoded
132: * form (e.g., "%2F") to the instance.
133: *
134: * @see https://tools.ietf.org/html/rfc3986#section-2
135: * @see https://tools.ietf.org/html/rfc3986#section-3.3
136: *
137: * @return string the URI path
138: */
139: public function getPath();
140:
141: /**
142: * Retrieve the query string of the URI.
143: *
144: * If no query string is present, this method MUST return an empty string.
145: *
146: * The leading "?" character is not part of the query and MUST NOT be
147: * added.
148: *
149: * The value returned MUST be percent-encoded, but MUST NOT double-encode
150: * any characters. To determine what characters to encode, please refer to
151: * RFC 3986, Sections 2 and 3.4.
152: *
153: * As an example, if a value in a key/value pair of the query string should
154: * include an ampersand ("&") not intended as a delimiter between values,
155: * that value MUST be passed in encoded form (e.g., "%26") to the instance.
156: *
157: * @see https://tools.ietf.org/html/rfc3986#section-2
158: * @see https://tools.ietf.org/html/rfc3986#section-3.4
159: *
160: * @return string the URI query string
161: */
162: public function getQuery();
163:
164: /**
165: * Retrieve the fragment component of the URI.
166: *
167: * If no fragment is present, this method MUST return an empty string.
168: *
169: * The leading "#" character is not part of the fragment and MUST NOT be
170: * added.
171: *
172: * The value returned MUST be percent-encoded, but MUST NOT double-encode
173: * any characters. To determine what characters to encode, please refer to
174: * RFC 3986, Sections 2 and 3.5.
175: *
176: * @see https://tools.ietf.org/html/rfc3986#section-2
177: * @see https://tools.ietf.org/html/rfc3986#section-3.5
178: *
179: * @return string the URI fragment
180: */
181: public function getFragment();
182:
183: /**
184: * Return an instance with the specified scheme.
185: *
186: * This method MUST retain the state of the current instance, and return
187: * an instance that contains the specified scheme.
188: *
189: * Implementations MUST support the schemes "http" and "https" case
190: * insensitively, and MAY accommodate other schemes if required.
191: *
192: * An empty scheme is equivalent to removing the scheme.
193: *
194: * @param string $scheme the scheme to use with the new instance
195: *
196: * @throws \InvalidArgumentException for invalid or unsupported schemes
197: *
198: * @return self a new instance with the specified scheme
199: */
200: public function withScheme($scheme);
201:
202: /**
203: * Return an instance with the specified user information.
204: *
205: * This method MUST retain the state of the current instance, and return
206: * an instance that contains the specified user information.
207: *
208: * Password is optional, but the user information MUST include the
209: * user; an empty string for the user is equivalent to removing user
210: * information.
211: *
212: * @param string $user the user name to use for authority
213: * @param null|string $password the password associated with $user
214: *
215: * @return self a new instance with the specified user information
216: */
217: public function withUserInfo($user, $password = null);
218:
219: /**
220: * Return an instance with the specified host.
221: *
222: * This method MUST retain the state of the current instance, and return
223: * an instance that contains the specified host.
224: *
225: * An empty host value is equivalent to removing the host.
226: *
227: * @param string $host the hostname to use with the new instance
228: *
229: * @throws \InvalidArgumentException for invalid hostnames
230: *
231: * @return self a new instance with the specified host
232: */
233: public function withHost($host);
234:
235: /**
236: * Return an instance with the specified port.
237: *
238: * This method MUST retain the state of the current instance, and return
239: * an instance that contains the specified port.
240: *
241: * Implementations MUST raise an exception for ports outside the
242: * established TCP and UDP port ranges.
243: *
244: * A null value provided for the port is equivalent to removing the port
245: * information.
246: *
247: * removes the port information.
248: *
249: * @param null|int $port The port to use with the new instance; a null value
250: *
251: * @throws \InvalidArgumentException for invalid ports
252: *
253: * @return self a new instance with the specified port
254: */
255: public function withPort($port);
256:
257: /**
258: * Return an instance with the specified path.
259: *
260: * This method MUST retain the state of the current instance, and return
261: * an instance that contains the specified path.
262: *
263: * The path can either be empty or absolute (starting with a slash) or
264: * rootless (not starting with a slash). Implementations MUST support all
265: * three syntaxes.
266: *
267: * If the path is intended to be domain-relative rather than path relative then
268: * it must begin with a slash ("/"). Paths not starting with a slash ("/")
269: * are assumed to be relative to some base path known to the application or
270: * consumer.
271: *
272: * Users can provide both encoded and decoded path characters.
273: * Implementations ensure the correct encoding as outlined in getPath().
274: *
275: * @param string $path the path to use with the new instance
276: *
277: * @throws \InvalidArgumentException for invalid paths
278: *
279: * @return self a new instance with the specified path
280: */
281: public function withPath($path);
282:
283: /**
284: * Return an instance with the specified query string.
285: *
286: * This method MUST retain the state of the current instance, and return
287: * an instance that contains the specified query string.
288: *
289: * Users can provide both encoded and decoded query characters.
290: * Implementations ensure the correct encoding as outlined in getQuery().
291: *
292: * An empty query string value is equivalent to removing the query string.
293: *
294: * @param string $query the query string to use with the new instance
295: *
296: * @throws \InvalidArgumentException for invalid query strings
297: *
298: * @return self a new instance with the specified query string
299: */
300: public function withQuery($query);
301:
302: /**
303: * Return an instance with the specified URI fragment.
304: *
305: * This method MUST retain the state of the current instance, and return
306: * an instance that contains the specified URI fragment.
307: *
308: * Users can provide both encoded and decoded fragment characters.
309: * Implementations ensure the correct encoding as outlined in getFragment().
310: *
311: * An empty fragment value is equivalent to removing the fragment.
312: *
313: * @param string $fragment the fragment to use with the new instance
314: *
315: * @return self a new instance with the specified fragment
316: */
317: public function withFragment($fragment);
318:
319: /**
320: * Return the string representation as a URI reference.
321: *
322: * Depending on which components of the URI are present, the resulting
323: * string is either a full URI or relative reference according to RFC 3986,
324: * Section 4.1. The method concatenates the various components of the URI,
325: * using the appropriate delimiters:
326: *
327: * - If a scheme is present, it MUST be suffixed by ":".
328: * - If an authority is present, it MUST be prefixed by "//".
329: * - The path can be concatenated without delimiters. But there are two
330: * cases where the path has to be adjusted to make the URI reference
331: * valid as PHP does not allow to throw an exception in __toString():
332: * - If the path is rootless and an authority is present, the path MUST
333: * be prefixed by "/".
334: * - If the path is starting with more than one "/" and no authority is
335: * present, the starting slashes MUST be reduced to one.
336: * - If a query is present, it MUST be prefixed by "?".
337: * - If a fragment is present, it MUST be prefixed by "#".
338: *
339: * @see http://tools.ietf.org/html/rfc3986#section-4.1
340: *
341: * @return string
342: */
343: public function __toString();
344: }
345: