UriInterface.php 12 KB

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