UriInterface.php 12 KB

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