Http.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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;
  12. use Deprecated;
  13. use JsonSerializable;
  14. use League\Uri\Contracts\UriException;
  15. use League\Uri\Contracts\UriInterface;
  16. use League\Uri\Exceptions\SyntaxError;
  17. use League\Uri\UriTemplate\TemplateCanNotBeExpanded;
  18. use Psr\Http\Message\UriInterface as Psr7UriInterface;
  19. use Stringable;
  20. /**
  21. * @phpstan-import-type InputComponentMap from UriString
  22. */
  23. final class Http implements Stringable, Psr7UriInterface, JsonSerializable
  24. {
  25. private readonly UriInterface $uri;
  26. private function __construct(UriInterface $uri)
  27. {
  28. if (null === $uri->getScheme() && '' === $uri->getHost()) {
  29. throw new SyntaxError('An URI without scheme cannot contain an empty host string according to PSR-7: '.$uri);
  30. }
  31. $port = $uri->getPort();
  32. if (null !== $port && ($port < 0 || $port > 65535)) {
  33. throw new SyntaxError('The URI port is outside the established TCP and UDP port ranges: '.$uri);
  34. }
  35. $this->uri = $this->normalizePsr7Uri($uri);
  36. }
  37. /**
  38. * PSR-7 UriInterface makes the following normalization.
  39. *
  40. * Safely stringify input when possible for League UriInterface compatibility.
  41. *
  42. * Query, Fragment and User Info when undefined are normalized to the empty string
  43. */
  44. private function normalizePsr7Uri(UriInterface $uri): UriInterface
  45. {
  46. $components = [];
  47. if ('' === $uri->getFragment()) {
  48. $components['fragment'] = null;
  49. }
  50. if ('' === $uri->getQuery()) {
  51. $components['query'] = null;
  52. }
  53. if ('' === $uri->getUserInfo()) {
  54. $components['user'] = null;
  55. $components['pass'] = null;
  56. }
  57. return match ($components) {
  58. [] => $uri,
  59. default => Uri::fromComponents([...$uri->toComponents(), ...$components]),
  60. };
  61. }
  62. /**
  63. * Create a new instance from a string or a stringable object.
  64. */
  65. public static function new(Stringable|string $uri = ''): self
  66. {
  67. return self::fromComponents(UriString::parse($uri));
  68. }
  69. /**
  70. * Create a new instance from a hash of parse_url parts.
  71. *
  72. * @param InputComponentMap $components a hash representation of the URI similar
  73. * to PHP parse_url function result
  74. */
  75. public static function fromComponents(array $components): self
  76. {
  77. $components += [
  78. 'scheme' => null, 'user' => null, 'pass' => null, 'host' => null,
  79. 'port' => null, 'path' => '', 'query' => null, 'fragment' => null,
  80. ];
  81. if ('' === $components['user']) {
  82. $components['user'] = null;
  83. }
  84. if ('' === $components['pass']) {
  85. $components['pass'] = null;
  86. }
  87. if ('' === $components['query']) {
  88. $components['query'] = null;
  89. }
  90. if ('' === $components['fragment']) {
  91. $components['fragment'] = null;
  92. }
  93. return new self(Uri::fromComponents($components));
  94. }
  95. /**
  96. * Create a new instance from the environment.
  97. */
  98. public static function fromServer(array $server): self
  99. {
  100. return new self(Uri::fromServer($server));
  101. }
  102. /**
  103. * Create a new instance from a URI and a Base URI.
  104. *
  105. * The returned URI must be absolute.
  106. */
  107. public static function fromBaseUri(Stringable|string $uri, Stringable|string|null $baseUri = null): self
  108. {
  109. return new self(Uri::fromBaseUri($uri, $baseUri));
  110. }
  111. /**
  112. * Creates a new instance from a template.
  113. *
  114. * @throws TemplateCanNotBeExpanded if the variables are invalid or missing
  115. * @throws UriException if the variables are invalid or missing
  116. */
  117. public static function fromTemplate(Stringable|string $template, iterable $variables = []): self
  118. {
  119. return new self(Uri::fromTemplate($template, $variables));
  120. }
  121. public function getScheme(): string
  122. {
  123. return $this->uri->getScheme() ?? '';
  124. }
  125. public function getAuthority(): string
  126. {
  127. return $this->uri->getAuthority() ?? '';
  128. }
  129. public function getUserInfo(): string
  130. {
  131. return $this->uri->getUserInfo() ?? '';
  132. }
  133. public function getHost(): string
  134. {
  135. return $this->uri->getHost() ?? '';
  136. }
  137. public function getPort(): ?int
  138. {
  139. return $this->uri->getPort();
  140. }
  141. public function getPath(): string
  142. {
  143. return $this->uri->getPath();
  144. }
  145. public function getQuery(): string
  146. {
  147. return $this->uri->getQuery() ?? '';
  148. }
  149. public function getFragment(): string
  150. {
  151. return $this->uri->getFragment() ?? '';
  152. }
  153. public function __toString(): string
  154. {
  155. return $this->uri->toString();
  156. }
  157. public function jsonSerialize(): string
  158. {
  159. return $this->uri->toString();
  160. }
  161. /**
  162. * Safely stringify input when possible for League UriInterface compatibility.
  163. */
  164. private function filterInput(string $str): ?string
  165. {
  166. return match ('') {
  167. $str => null,
  168. default => $str,
  169. };
  170. }
  171. private function newInstance(UriInterface $uri): self
  172. {
  173. return match ($this->uri->toString()) {
  174. $uri->toString() => $this,
  175. default => new self($uri),
  176. };
  177. }
  178. public function withScheme(string $scheme): self
  179. {
  180. return $this->newInstance($this->uri->withScheme($this->filterInput($scheme)));
  181. }
  182. public function withUserInfo(string $user, ?string $password = null): self
  183. {
  184. return $this->newInstance($this->uri->withUserInfo($this->filterInput($user), $password));
  185. }
  186. public function withHost(string $host): self
  187. {
  188. return $this->newInstance($this->uri->withHost($this->filterInput($host)));
  189. }
  190. public function withPort(?int $port): self
  191. {
  192. return $this->newInstance($this->uri->withPort($port));
  193. }
  194. public function withPath(string $path): self
  195. {
  196. return $this->newInstance($this->uri->withPath($path));
  197. }
  198. public function withQuery(string $query): self
  199. {
  200. return $this->newInstance($this->uri->withQuery($this->filterInput($query)));
  201. }
  202. public function withFragment(string $fragment): self
  203. {
  204. return $this->newInstance($this->uri->withFragment($this->filterInput($fragment)));
  205. }
  206. /**
  207. * DEPRECATION WARNING! This method will be removed in the next major point release.
  208. *
  209. * @deprecated Since version 7.0.0
  210. * @codeCoverageIgnore
  211. * @see Http::new()
  212. *
  213. * Create a new instance from a string.
  214. */
  215. #[Deprecated(message:'use League\Uri\Http::new() instead', since:'league/uri:7.0.0')]
  216. public static function createFromString(Stringable|string $uri = ''): self
  217. {
  218. return self::new($uri);
  219. }
  220. /**
  221. * DEPRECATION WARNING! This method will be removed in the next major point release.
  222. *
  223. * @deprecated Since version 7.0.0
  224. * @codeCoverageIgnore
  225. * @see Http::fromComponents()
  226. *
  227. * Create a new instance from a hash of parse_url parts.
  228. *
  229. * @param InputComponentMap $components a hash representation of the URI similar
  230. * to PHP parse_url function result
  231. */
  232. #[Deprecated(message:'use League\Uri\Http::fromComponents() instead', since:'league/uri:7.0.0')]
  233. public static function createFromComponents(array $components): self
  234. {
  235. return self::fromComponents($components);
  236. }
  237. /**
  238. * DEPRECATION WARNING! This method will be removed in the next major point release.
  239. *
  240. * @deprecated Since version 7.0.0
  241. * @codeCoverageIgnore
  242. * @see Http::fromServer()
  243. *
  244. * Create a new instance from the environment.
  245. */
  246. #[Deprecated(message:'use League\Uri\Http::fromServer() instead', since:'league/uri:7.0.0')]
  247. public static function createFromServer(array $server): self
  248. {
  249. return self::fromServer($server);
  250. }
  251. /**
  252. * DEPRECATION WARNING! This method will be removed in the next major point release.
  253. *
  254. * @deprecated Since version 7.0.0
  255. * @codeCoverageIgnore
  256. * @see Http::new()
  257. *
  258. * Create a new instance from a URI object.
  259. */
  260. #[Deprecated(message:'use League\Uri\Http::new() instead', since:'league/uri:7.0.0')]
  261. public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
  262. {
  263. return self::new($uri);
  264. }
  265. /**
  266. * DEPRECATION WARNING! This method will be removed in the next major point release.
  267. *
  268. * @deprecated Since version 7.0.0
  269. * @codeCoverageIgnore
  270. * @see Http::fromBaseUri()
  271. *
  272. * Create a new instance from a URI and a Base URI.
  273. *
  274. * The returned URI must be absolute.
  275. */
  276. #[Deprecated(message:'use League\Uri\Http::fromBaseUri() instead', since:'league/uri:7.0.0')]
  277. public static function createFromBaseUri(Stringable|string $uri, Stringable|string|null $baseUri = null): self
  278. {
  279. return self::fromBaseUri($uri, $baseUri);
  280. }
  281. }