UriInterface.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri\Contracts;
  12. use JsonSerializable;
  13. use League\Uri\Exceptions\MissingFeature;
  14. use League\Uri\Exceptions\SyntaxError;
  15. use League\Uri\UriString;
  16. use Stringable;
  17. /**
  18. * @phpstan-import-type ComponentMap from UriString
  19. *
  20. * @method string|null getUsername() returns the user component of the URI.
  21. * @method string|null getPassword() returns the scheme-specific information about how to gain authorization to access the resource.
  22. * @method array toComponents() returns an associative array containing all the URI components.
  23. */
  24. interface UriInterface extends JsonSerializable, Stringable
  25. {
  26. /**
  27. * Returns the string representation as a URI reference.
  28. *
  29. * @see http://tools.ietf.org/html/rfc3986#section-4.1
  30. */
  31. public function __toString(): string;
  32. /**
  33. * Returns the string representation as a URI reference.
  34. *
  35. * @see http://tools.ietf.org/html/rfc3986#section-4.1
  36. */
  37. public function toString(): string;
  38. /**
  39. * Returns the string representation as a URI reference.
  40. *
  41. * @see http://tools.ietf.org/html/rfc3986#section-4.1
  42. * @see ::__toString
  43. */
  44. public function jsonSerialize(): string;
  45. /**
  46. * Retrieve the scheme component of the URI.
  47. *
  48. * If no scheme is present, this method MUST return a null value.
  49. *
  50. * The value returned MUST be normalized to lowercase, per RFC 3986
  51. * Section 3.1.
  52. *
  53. * The trailing ":" character is not part of the scheme and MUST NOT be
  54. * added.
  55. *
  56. * @see https://tools.ietf.org/html/rfc3986#section-3.1
  57. */
  58. public function getScheme(): ?string;
  59. /**
  60. * Retrieve the authority component of the URI.
  61. *
  62. * If no scheme is present, this method MUST return a null value.
  63. *
  64. * If the port component is not set or is the standard port for the current
  65. * scheme, it SHOULD NOT be included.
  66. *
  67. * @see https://tools.ietf.org/html/rfc3986#section-3.2
  68. */
  69. public function getAuthority(): ?string;
  70. /**
  71. * Retrieve the user information component of the URI.
  72. *
  73. * If no scheme is present, this method MUST return a null value.
  74. *
  75. * If a user is present in the URI, this will return that value;
  76. * additionally, if the password is also present, it will be appended to the
  77. * user value, with a colon (":") separating the values.
  78. *
  79. * The trailing "@" character is not part of the user information and MUST
  80. * NOT be added.
  81. */
  82. public function getUserInfo(): ?string;
  83. /**
  84. * Retrieve the host component of the URI.
  85. *
  86. * If no host is present this method MUST return a null value.
  87. *
  88. * The value returned MUST be normalized to lowercase, per RFC 3986
  89. * Section 3.2.2.
  90. *
  91. * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
  92. */
  93. public function getHost(): ?string;
  94. /**
  95. * Retrieve the port component of the URI.
  96. *
  97. * If a port is present, and it is non-standard for the current scheme,
  98. * this method MUST return it as an integer. If the port is the standard port
  99. * used with the current scheme, this method SHOULD return null.
  100. *
  101. * If no port is present, and no scheme is present, this method MUST return
  102. * a null value.
  103. *
  104. * If no port is present, but a scheme is present, this method MAY return
  105. * the standard port for that scheme, but SHOULD return null.
  106. */
  107. public function getPort(): ?int;
  108. /**
  109. * Retrieve the path component of the URI.
  110. *
  111. * The path can either be empty or absolute (starting with a slash) or
  112. * rootless (not starting with a slash). Implementations MUST support all
  113. * three syntaxes.
  114. *
  115. * Normally, the empty path "" and absolute path "/" are considered equal as
  116. * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
  117. * do this normalization because in contexts with a trimmed base path, e.g.
  118. * the front controller, this difference becomes significant. It's the task
  119. * of the user to handle both "" and "/".
  120. *
  121. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  122. * any characters. To determine what characters to encode, please refer to
  123. * RFC 3986, Sections 2 and 3.3.
  124. *
  125. * As an example, if the value should include a slash ("/") not intended as
  126. * delimiter between path segments, that value MUST be passed in encoded
  127. * form (e.g., "%2F") to the instance.
  128. *
  129. * @see https://tools.ietf.org/html/rfc3986#section-2
  130. * @see https://tools.ietf.org/html/rfc3986#section-3.3
  131. */
  132. public function getPath(): string;
  133. /**
  134. * Retrieve the query string of the URI.
  135. *
  136. * If no host is present this method MUST return a null value.
  137. *
  138. * The leading "?" character is not part of the query and MUST NOT be
  139. * added.
  140. *
  141. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  142. * any characters. To determine what characters to encode, please refer to
  143. * RFC 3986, Sections 2 and 3.4.
  144. *
  145. * As an example, if a value in a key/value pair of the query string should
  146. * include an ampersand ("&") not intended as a delimiter between values,
  147. * that value MUST be passed in encoded form (e.g., "%26") to the instance.
  148. *
  149. * @see https://tools.ietf.org/html/rfc3986#section-2
  150. * @see https://tools.ietf.org/html/rfc3986#section-3.4
  151. */
  152. public function getQuery(): ?string;
  153. /**
  154. * Retrieve the fragment component of the URI.
  155. *
  156. * If no host is present this method MUST return a null value.
  157. *
  158. * The leading "#" character is not part of the fragment and MUST NOT be
  159. * added.
  160. *
  161. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  162. * any characters. To determine what characters to encode, please refer to
  163. * RFC 3986, Sections 2 and 3.5.
  164. *
  165. * @see https://tools.ietf.org/html/rfc3986#section-2
  166. * @see https://tools.ietf.org/html/rfc3986#section-3.5
  167. */
  168. public function getFragment(): ?string;
  169. /**
  170. * Returns an associative array containing all the URI components.
  171. *
  172. * The returned array is similar to PHP's parse_url return value with the following
  173. * differences:
  174. *
  175. * <ul>
  176. * <li>All components are present in the returned array</li>
  177. * <li>Empty and undefined component are treated differently. And empty component is
  178. * set to the empty string while an undefined component is set to the `null` value.</li>
  179. * </ul>
  180. *
  181. * @link https://tools.ietf.org/html/rfc3986
  182. *
  183. * @return ComponentMap
  184. */
  185. public function getComponents(): array;
  186. /**
  187. * Return an instance with the specified scheme.
  188. *
  189. * This method MUST retain the state of the current instance, and return
  190. * an instance that contains the specified scheme.
  191. *
  192. * A null value provided for the scheme is equivalent to removing the scheme
  193. * information.
  194. *
  195. * @throws SyntaxError for invalid component or transformations
  196. * that would result in an object in invalid state.
  197. */
  198. public function withScheme(Stringable|string|null $scheme): self;
  199. /**
  200. * Return an instance with the specified user information.
  201. *
  202. * This method MUST retain the state of the current instance, and return
  203. * an instance that contains the specified user information.
  204. *
  205. * Password is optional, but the user information MUST include the
  206. * user; a null value for the user is equivalent to removing user
  207. * information.
  208. *
  209. * @throws SyntaxError for invalid component or transformations
  210. * that would result in an object in invalid state.
  211. */
  212. public function withUserInfo(Stringable|string|null $user, Stringable|string|null $password = null): self;
  213. /**
  214. * Return an instance with the specified host.
  215. *
  216. * This method MUST retain the state of the current instance, and return
  217. * an instance that contains the specified host.
  218. *
  219. * A null value provided for the host is equivalent to removing the host
  220. * information.
  221. *
  222. * @throws SyntaxError for invalid component or transformations
  223. * that would result in an object in invalid state.
  224. * @throws MissingFeature for component or transformations
  225. * requiring IDN support when IDN support is not present
  226. * or misconfigured.
  227. */
  228. public function withHost(Stringable|string|null $host): self;
  229. /**
  230. * Return an instance with the specified port.
  231. *
  232. * This method MUST retain the state of the current instance, and return
  233. * an instance that contains the specified port.
  234. *
  235. * A null value provided for the port is equivalent to removing the port
  236. * information.
  237. *
  238. * @throws SyntaxError for invalid component or transformations
  239. * that would result in an object in invalid state.
  240. */
  241. public function withPort(?int $port): self;
  242. /**
  243. * Return an instance with the specified path.
  244. *
  245. * This method MUST retain the state of the current instance, and return
  246. * an instance that contains the specified path.
  247. *
  248. * The path can either be empty or absolute (starting with a slash) or
  249. * rootless (not starting with a slash). Implementations MUST support all
  250. * three syntaxes.
  251. *
  252. * Users can provide both encoded and decoded path characters.
  253. * Implementations ensure the correct encoding as outlined in getPath().
  254. *
  255. * @throws SyntaxError for invalid component or transformations
  256. * that would result in an object in invalid state.
  257. */
  258. public function withPath(Stringable|string $path): self;
  259. /**
  260. * Return an instance with the specified query string.
  261. *
  262. * This method MUST retain the state of the current instance, and return
  263. * an instance that contains the specified query string.
  264. *
  265. * Users can provide both encoded and decoded query characters.
  266. * Implementations ensure the correct encoding as outlined in getQuery().
  267. *
  268. * A null value provided for the query is equivalent to removing the query
  269. * information.
  270. *
  271. * @throws SyntaxError for invalid component or transformations
  272. * that would result in an object in invalid state.
  273. */
  274. public function withQuery(Stringable|string|null $query): self;
  275. /**
  276. * Return an instance with the specified URI fragment.
  277. *
  278. * This method MUST retain the state of the current instance, and return
  279. * an instance that contains the specified URI fragment.
  280. *
  281. * Users can provide both encoded and decoded fragment characters.
  282. * Implementations ensure the correct encoding as outlined in getFragment().
  283. *
  284. * A null value provided for the fragment is equivalent to removing the fragment
  285. * information.
  286. *
  287. * @throws SyntaxError for invalid component or transformations
  288. * that would result in an object in invalid state.
  289. */
  290. public function withFragment(Stringable|string|null $fragment): self;
  291. }