Uri.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\Psr7
  5. *
  6. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\Psr7;
  10. use Grav\Framework\Psr7\Traits\UriDecorationTrait;
  11. use Grav\Framework\Uri\UriFactory;
  12. use GuzzleHttp\Psr7\Uri as GuzzleUri;
  13. use Psr\Http\Message\UriInterface;
  14. /**
  15. * Class Uri
  16. * @package Grav\Framework\Psr7
  17. */
  18. class Uri implements UriInterface
  19. {
  20. use UriDecorationTrait;
  21. public function __construct(string $uri = '')
  22. {
  23. $this->uri = new \Nyholm\Psr7\Uri($uri);
  24. }
  25. /**
  26. * @return array
  27. */
  28. public function getQueryParams(): array
  29. {
  30. return UriFactory::parseQuery($this->getQuery());
  31. }
  32. /**
  33. * @param array $params
  34. * @return UriInterface
  35. */
  36. public function withQueryParams(array $params): UriInterface
  37. {
  38. $query = UriFactory::buildQuery($params);
  39. return $this->withQuery($query);
  40. }
  41. /**
  42. * Whether the URI has the default port of the current scheme.
  43. *
  44. * `$uri->getPort()` may return the standard port. This method can be used for some non-http/https Uri.
  45. *
  46. * @return bool
  47. */
  48. public function isDefaultPort(): bool
  49. {
  50. return $this->getPort() === null || GuzzleUri::isDefaultPort($this);
  51. }
  52. /**
  53. * Whether the URI is absolute, i.e. it has a scheme.
  54. *
  55. * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true
  56. * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative
  57. * to another URI, the base URI. Relative references can be divided into several forms:
  58. * - network-path references, e.g. '//example.com/path'
  59. * - absolute-path references, e.g. '/path'
  60. * - relative-path references, e.g. 'subpath'
  61. *
  62. * @return bool
  63. * @link https://tools.ietf.org/html/rfc3986#section-4
  64. */
  65. public function isAbsolute(): bool
  66. {
  67. return GuzzleUri::isAbsolute($this);
  68. }
  69. /**
  70. * Whether the URI is a network-path reference.
  71. *
  72. * A relative reference that begins with two slash characters is termed an network-path reference.
  73. *
  74. * @return bool
  75. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  76. */
  77. public function isNetworkPathReference(): bool
  78. {
  79. return GuzzleUri::isNetworkPathReference($this);
  80. }
  81. /**
  82. * Whether the URI is a absolute-path reference.
  83. *
  84. * A relative reference that begins with a single slash character is termed an absolute-path reference.
  85. *
  86. * @return bool
  87. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  88. */
  89. public function isAbsolutePathReference(): bool
  90. {
  91. return GuzzleUri::isAbsolutePathReference($this);
  92. }
  93. /**
  94. * Whether the URI is a relative-path reference.
  95. *
  96. * A relative reference that does not begin with a slash character is termed a relative-path reference.
  97. *
  98. * @return bool
  99. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  100. */
  101. public function isRelativePathReference(): bool
  102. {
  103. return GuzzleUri::isRelativePathReference($this);
  104. }
  105. /**
  106. * Whether the URI is a same-document reference.
  107. *
  108. * A same-document reference refers to a URI that is, aside from its fragment
  109. * component, identical to the base URI. When no base URI is given, only an empty
  110. * URI reference (apart from its fragment) is considered a same-document reference.
  111. *
  112. * @param UriInterface|null $base An optional base URI to compare against
  113. * @return bool
  114. * @link https://tools.ietf.org/html/rfc3986#section-4.4
  115. */
  116. public function isSameDocumentReference(UriInterface $base = null): bool
  117. {
  118. return GuzzleUri::isSameDocumentReference($this, $base);
  119. }
  120. }