Skip to content

Commit dd51dcf

Browse files
committed
Created a URI interface
- Based on proposals made to the mailing list; essentially, an immutable value object with methods for retrieving each segment of a URI, methods for creating new instances with updated values, a number of "test" methods for determining what type of URI is present, and a method for casting to a string.
1 parent facd2d7 commit dd51dcf

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed

src/UriInterface.php

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
<?php
2+
namespace Psr\Http\Message;
3+
4+
/**
5+
* Value object representing a URI.
6+
*
7+
* @see http://tools.ietf.org/html/rfc3986 (the URI specification)
8+
* @see http://tools.ietf.org/html/rfc7230#section-2.7 (URIs as used in the HTTP specification)
9+
*/
10+
interface UriInterface
11+
{
12+
/**
13+
* Retrieve the URI scheme.
14+
*
15+
* Generally this will be one of "http" or "https", but implementations may
16+
* allow for other schemes when desired.
17+
*
18+
* If no scheme is present, this method MUST return an empty string.
19+
*
20+
* The string returned MUST strip off the "://" trailing delimiter if
21+
* present.
22+
*
23+
* @return string The scheme of the URI.
24+
*/
25+
public function getScheme();
26+
27+
/**
28+
* Retrieve the authority portion of the URI.
29+
*
30+
* The authority portion of a URI when present, can consist of a username,
31+
* and optionally the password/credentials for that user. The return MUST
32+
* be a string, in the format of "username[:password]", where the colon and
33+
* password are only present if they were provided. (Brackets MUST NOT be
34+
* present; they are used here to indicate that those items are optional)
35+
*
36+
* The string returned MUST strip off the trailing "@" delimiter if
37+
* present.
38+
*
39+
* This method MUST return an empty string if no authority information is
40+
* present.
41+
*
42+
* @return string Authority portion of the URI, in "username[:password]"
43+
* format.
44+
*/
45+
public function getAuthority();
46+
47+
/**
48+
* Retrieve the host segment of the URI.
49+
*
50+
* This method MUST return a string; if no host segment is present, an
51+
* empty string MUST be returned.
52+
*
53+
* @return string Host segment of the URI.
54+
*/
55+
public function getHost();
56+
57+
/**
58+
* Retrieve the port segment of the URI.
59+
*
60+
* If a port is present, and it is non-standard for the current scheme,
61+
* this method MUST return it as an integer. If the port is the standard port
62+
* used with the current scheme, this method SHOULD return null.
63+
*
64+
* If no port is present, and no scheme is present, this method MUST return
65+
* a null value.
66+
*
67+
* If no port is present, but a scheme is present, this method MAY return
68+
* the standard port for that scheme.
69+
*
70+
* @return null|int The port for the URI.
71+
*/
72+
public function getPort();
73+
74+
/**
75+
* Retrieve the path segment of the URI.
76+
*
77+
* This method MUST return a string; if no path is present it MUST return
78+
* an empty string.
79+
*
80+
* @return string The path segment of the URI.
81+
*/
82+
public function getPath();
83+
84+
/**
85+
* Retrieve the query string of the URI.
86+
*
87+
* This method MUST return a string; if no query string is present, it MUST
88+
* return an empty string.
89+
*
90+
* The string returned MUST strip off any leading "?" character.
91+
*
92+
* @return string The URI query string.
93+
*/
94+
public function getQuery();
95+
96+
/**
97+
* Retrieve the fragment segment of the URI.
98+
*
99+
* This method MUST return a string; if no fragment is present, it MUST
100+
* return an empty string.
101+
*
102+
* The string returned MUST strip off any leading "#" character.
103+
*
104+
* @return string The URI fragment.
105+
*/
106+
public function getFragment();
107+
108+
/**
109+
* Create a new instance with the specified scheme.
110+
*
111+
* This method MUST retain the state of the current instance, and return
112+
* a new instance that contains the specified scheme. If the scheme
113+
* provided includes the "://" delimiter, it MUST be removed.
114+
*
115+
* @param string $scheme The scheme to use with the new instance.
116+
* @return UriInterface A new instance with the specified scheme.
117+
* @throws \InvalidArgumentException for invalid or unsupported schemes.
118+
*/
119+
public function withScheme($scheme);
120+
121+
/**
122+
* Create a new instance with the specified authority information.
123+
*
124+
* This method MUST retain the state of the current instance, and return
125+
* a new instance that contains the specified authority information.
126+
*
127+
* Password is optional, but the authority information MUST include the
128+
* user.
129+
*
130+
* @param string $user User name to use for authority.
131+
* @param null|string $password Password associated with $user.
132+
* @return UriInterface A new instance with the specified authority
133+
* information.
134+
*/
135+
public function withAuthority($user, $password = null);
136+
137+
/**
138+
* Create a new instance with the specified host.
139+
*
140+
* This method MUST retain the state of the current instance, and return
141+
* a new instance that contains the specified host.
142+
*
143+
* @param string $host Hostname to use with the new instance.
144+
* @return UriInterface A new instance with the specified host.
145+
* @throws \InvalidArgumentException for invalid hostnames.
146+
*/
147+
public function withHost($host);
148+
149+
/**
150+
* Create a new instance with the specified port.
151+
*
152+
* This method MUST retain the state of the current instance, and return
153+
* a new instance that contains the specified port.
154+
*
155+
* @param int $port Port to use with the new instance.
156+
* @return UriInterface A new instance with the specified port.
157+
* @throws \InvalidArgumentException for invalid ports.
158+
*/
159+
public function withPort($port);
160+
161+
/**
162+
* Create a new instance with the specified path.
163+
*
164+
* This method MUST retain the state of the current instance, and return
165+
* a new instance that contains the specified path.
166+
*
167+
* The path MUST be prefixed with "/"; if not, the implementation MAY
168+
* provide the prefix itself.
169+
*
170+
* @param string $path The path to use with the new instance.
171+
* @return UriInterface A new instance with the specified path.
172+
* @throws \InvalidArgumentException for invalid paths.
173+
*/
174+
public function withPath($path);
175+
176+
/**
177+
* Create a new instance with the specified query string.
178+
*
179+
* This method MUST retain the state of the current instance, and return
180+
* a new instance that contains the specified query string.
181+
*
182+
* If the query string is prefixed by "?", that character MUST be removed.
183+
* Additionally, the query string SHOULD be parseable by parse_str() in
184+
* order to be valid.
185+
*
186+
* @param string $query The query string to use with the new instance.
187+
* @return UriInterface A new instance with the specified query string.
188+
* @throws \InvalidArgumentException for invalid query strings.
189+
*/
190+
public function withQuery($query);
191+
192+
/**
193+
* Create a new instance with the specified URI fragment.
194+
*
195+
* This method MUST retain the state of the current instance, and return
196+
* a new instance that contains the specified URI fragment.
197+
*
198+
* If the fragment is prefixed by "#", that character MUST be removed.
199+
*
200+
* @param string $fragment The URI fragment to use with the new instance.
201+
* @return UriInterface A new instance with the specified URI fragment.
202+
*/
203+
public function withFragment($fragment);
204+
205+
/**
206+
* Indicate whether the URI is in origin-form.
207+
*
208+
* Origin-form is a URI that includes only the path and optionally the
209+
* query string.
210+
*
211+
* @return bool
212+
*/
213+
public function isOriginForm();
214+
215+
/**
216+
* Indicate whether the URI is absolute.
217+
*
218+
* An absolute URI contains minimally the scheme and host.
219+
*
220+
* @return bool
221+
*/
222+
public function isAbsoluteForm();
223+
224+
/**
225+
* Indicate whether the URI is in authority form.
226+
*
227+
* An authority-form URI is an absolute URI that also contains authority
228+
* information.
229+
*
230+
* @return bool
231+
*/
232+
public function isAuthorityForm();
233+
234+
/**
235+
* Indicate whether the URI is an asterix form.
236+
*
237+
* An asterix form URI will have "*" as the path, and no other URI parts.
238+
*
239+
* @return bool
240+
*/
241+
public function isAsterixForm();
242+
243+
/**
244+
* Return the string representation of the URI.
245+
*
246+
* Concatenates the various segments of the URI, using the appropriate
247+
* delimiters:
248+
*
249+
* - If a scheme is present, "://" MUST append the value.
250+
* - If authority information is present, the password MUST be separated
251+
* from the user by a ":", and the full authority string MUST be appended
252+
* with an "@" character.
253+
* - If a port is present, and non-standard for the given scheme, it MUST
254+
* be provided, and MUST be prefixed by a ":" character.
255+
* - If a path is present, it MUST be prefixed by a "/" character.
256+
* - If a query string is present, it MUST be prefixed by a "?" character.
257+
* - If a URI fragment is present, it MUST be prefixed by a "#" character.
258+
*
259+
* @return string
260+
*/
261+
public function __toString();
262+
}

0 commit comments

Comments
 (0)