Uri.php 5.2 KB

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