Uri.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @package Grav\Framework\Uri
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Uri;
  9. use Grav\Framework\Psr7\AbstractUri;
  10. use GuzzleHttp\Psr7\Uri as GuzzleUri;
  11. use InvalidArgumentException;
  12. use Psr\Http\Message\UriInterface;
  13. /**
  14. * Implements PSR-7 UriInterface.
  15. *
  16. * @package Grav\Framework\Uri
  17. */
  18. class Uri extends AbstractUri
  19. {
  20. /** @var array Array of Uri query. */
  21. private $queryParams;
  22. /**
  23. * You can use `UriFactory` functions to create new `Uri` objects.
  24. *
  25. * @param array $parts
  26. * @return void
  27. * @throws InvalidArgumentException
  28. */
  29. public function __construct(array $parts = [])
  30. {
  31. $this->initParts($parts);
  32. }
  33. /**
  34. * @return string
  35. */
  36. public function getUser()
  37. {
  38. return parent::getUser();
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function getPassword()
  44. {
  45. return parent::getPassword();
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function getParts()
  51. {
  52. return parent::getParts();
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function getUrl()
  58. {
  59. return parent::getUrl();
  60. }
  61. /**
  62. * @return string
  63. */
  64. public function getBaseUrl()
  65. {
  66. return parent::getBaseUrl();
  67. }
  68. /**
  69. * @param string $key
  70. * @return string|null
  71. */
  72. public function getQueryParam($key)
  73. {
  74. $queryParams = $this->getQueryParams();
  75. return $queryParams[$key] ?? null;
  76. }
  77. /**
  78. * @param string $key
  79. * @return UriInterface
  80. */
  81. public function withoutQueryParam($key)
  82. {
  83. return GuzzleUri::withoutQueryValue($this, $key);
  84. }
  85. /**
  86. * @param string $key
  87. * @param string|null $value
  88. * @return UriInterface
  89. */
  90. public function withQueryParam($key, $value)
  91. {
  92. return GuzzleUri::withQueryValue($this, $key, $value);
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function getQueryParams()
  98. {
  99. if ($this->queryParams === null) {
  100. $this->queryParams = UriFactory::parseQuery($this->getQuery());
  101. }
  102. return $this->queryParams;
  103. }
  104. /**
  105. * @param array $params
  106. * @return UriInterface
  107. */
  108. public function withQueryParams(array $params)
  109. {
  110. $query = UriFactory::buildQuery($params);
  111. return $this->withQuery($query);
  112. }
  113. /**
  114. * Whether the URI has the default port of the current scheme.
  115. *
  116. * `$uri->getPort()` may return the standard port. This method can be used for some non-http/https Uri.
  117. *
  118. * @return bool
  119. */
  120. public function isDefaultPort()
  121. {
  122. return $this->getPort() === null || GuzzleUri::isDefaultPort($this);
  123. }
  124. /**
  125. * Whether the URI is absolute, i.e. it has a scheme.
  126. *
  127. * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true
  128. * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative
  129. * to another URI, the base URI. Relative references can be divided into several forms:
  130. * - network-path references, e.g. '//example.com/path'
  131. * - absolute-path references, e.g. '/path'
  132. * - relative-path references, e.g. 'subpath'
  133. *
  134. * @return bool
  135. * @link https://tools.ietf.org/html/rfc3986#section-4
  136. */
  137. public function isAbsolute()
  138. {
  139. return GuzzleUri::isAbsolute($this);
  140. }
  141. /**
  142. * Whether the URI is a network-path reference.
  143. *
  144. * A relative reference that begins with two slash characters is termed an network-path reference.
  145. *
  146. * @return bool
  147. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  148. */
  149. public function isNetworkPathReference()
  150. {
  151. return GuzzleUri::isNetworkPathReference($this);
  152. }
  153. /**
  154. * Whether the URI is a absolute-path reference.
  155. *
  156. * A relative reference that begins with a single slash character is termed an absolute-path reference.
  157. *
  158. * @return bool
  159. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  160. */
  161. public function isAbsolutePathReference()
  162. {
  163. return GuzzleUri::isAbsolutePathReference($this);
  164. }
  165. /**
  166. * Whether the URI is a relative-path reference.
  167. *
  168. * A relative reference that does not begin with a slash character is termed a relative-path reference.
  169. *
  170. * @return bool
  171. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  172. */
  173. public function isRelativePathReference()
  174. {
  175. return GuzzleUri::isRelativePathReference($this);
  176. }
  177. /**
  178. * Whether the URI is a same-document reference.
  179. *
  180. * A same-document reference refers to a URI that is, aside from its fragment
  181. * component, identical to the base URI. When no base URI is given, only an empty
  182. * URI reference (apart from its fragment) is considered a same-document reference.
  183. *
  184. * @param UriInterface|null $base An optional base URI to compare against
  185. * @return bool
  186. * @link https://tools.ietf.org/html/rfc3986#section-4.4
  187. */
  188. public function isSameDocumentReference(UriInterface $base = null)
  189. {
  190. return GuzzleUri::isSameDocumentReference($this, $base);
  191. }
  192. }