Http.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * League.Uri (https://uri.thephpleague.com) |
| 5 | * |
| 6 | * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | declare (strict_types=1); |
| 12 | namespace IAWP_SCOPED\League\Uri; |
| 13 | |
| 14 | use IAWP_SCOPED\League\Uri\Contracts\UriInterface; |
| 15 | use IAWP_SCOPED\League\Uri\Exceptions\SyntaxError; |
| 16 | use IAWP_SCOPED\Psr\Http\Message\UriInterface as Psr7UriInterface; |
| 17 | use function is_object; |
| 18 | use function is_scalar; |
| 19 | use function method_exists; |
| 20 | use function sprintf; |
| 21 | final class Http implements Psr7UriInterface, \JsonSerializable |
| 22 | { |
| 23 | /** |
| 24 | * @var UriInterface |
| 25 | */ |
| 26 | private $uri; |
| 27 | /** |
| 28 | * New instance. |
| 29 | */ |
| 30 | private function __construct(UriInterface $uri) |
| 31 | { |
| 32 | $this->validate($uri); |
| 33 | $this->uri = $uri; |
| 34 | } |
| 35 | /** |
| 36 | * Validate the submitted uri against PSR-7 UriInterface. |
| 37 | * |
| 38 | * @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules |
| 39 | */ |
| 40 | private function validate(UriInterface $uri) : void |
| 41 | { |
| 42 | $scheme = $uri->getScheme(); |
| 43 | if (null === $scheme && '' === $uri->getHost()) { |
| 44 | throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri)); |
| 45 | } |
| 46 | $port = $uri->getPort(); |
| 47 | if (null !== $port && ($port < 0 || $port > 65535)) { |
| 48 | throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort())); |
| 49 | } |
| 50 | } |
| 51 | /** |
| 52 | * Static method called by PHP's var export. |
| 53 | */ |
| 54 | public static function __set_state(array $components) : self |
| 55 | { |
| 56 | return new self($components['uri']); |
| 57 | } |
| 58 | /** |
| 59 | * Create a new instance from a string. |
| 60 | * |
| 61 | * @param string|mixed $uri |
| 62 | */ |
| 63 | public static function createFromString($uri = '') : self |
| 64 | { |
| 65 | return new self(Uri::createFromString($uri)); |
| 66 | } |
| 67 | /** |
| 68 | * Create a new instance from a hash of parse_url parts. |
| 69 | * |
| 70 | * @param array $components a hash representation of the URI similar |
| 71 | * to PHP parse_url function result |
| 72 | */ |
| 73 | public static function createFromComponents(array $components) : self |
| 74 | { |
| 75 | return new self(Uri::createFromComponents($components)); |
| 76 | } |
| 77 | /** |
| 78 | * Create a new instance from the environment. |
| 79 | */ |
| 80 | public static function createFromServer(array $server) : self |
| 81 | { |
| 82 | return new self(Uri::createFromServer($server)); |
| 83 | } |
| 84 | /** |
| 85 | * Create a new instance from a URI and a Base URI. |
| 86 | * |
| 87 | * The returned URI must be absolute. |
| 88 | * |
| 89 | * @param mixed $uri the input URI to create |
| 90 | * @param mixed $base_uri the base URI used for reference |
| 91 | */ |
| 92 | public static function createFromBaseUri($uri, $base_uri = null) : self |
| 93 | { |
| 94 | return new self(Uri::createFromBaseUri($uri, $base_uri)); |
| 95 | } |
| 96 | /** |
| 97 | * Create a new instance from a URI object. |
| 98 | * |
| 99 | * @param Psr7UriInterface|UriInterface $uri the input URI to create |
| 100 | */ |
| 101 | public static function createFromUri($uri) : self |
| 102 | { |
| 103 | if ($uri instanceof UriInterface) { |
| 104 | return new self($uri); |
| 105 | } |
| 106 | return new self(Uri::createFromUri($uri)); |
| 107 | } |
| 108 | /** |
| 109 | * {@inheritDoc} |
| 110 | */ |
| 111 | public function getScheme() : string |
| 112 | { |
| 113 | return (string) $this->uri->getScheme(); |
| 114 | } |
| 115 | /** |
| 116 | * {@inheritDoc} |
| 117 | */ |
| 118 | public function getAuthority() : string |
| 119 | { |
| 120 | return (string) $this->uri->getAuthority(); |
| 121 | } |
| 122 | /** |
| 123 | * {@inheritDoc} |
| 124 | */ |
| 125 | public function getUserInfo() : string |
| 126 | { |
| 127 | return (string) $this->uri->getUserInfo(); |
| 128 | } |
| 129 | /** |
| 130 | * {@inheritDoc} |
| 131 | */ |
| 132 | public function getHost() : string |
| 133 | { |
| 134 | return (string) $this->uri->getHost(); |
| 135 | } |
| 136 | /** |
| 137 | * {@inheritDoc} |
| 138 | */ |
| 139 | public function getPort() : ?int |
| 140 | { |
| 141 | return $this->uri->getPort(); |
| 142 | } |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | */ |
| 146 | public function getPath() : string |
| 147 | { |
| 148 | return $this->uri->getPath(); |
| 149 | } |
| 150 | /** |
| 151 | * {@inheritDoc} |
| 152 | */ |
| 153 | public function getQuery() : string |
| 154 | { |
| 155 | return (string) $this->uri->getQuery(); |
| 156 | } |
| 157 | /** |
| 158 | * {@inheritDoc} |
| 159 | */ |
| 160 | public function getFragment() : string |
| 161 | { |
| 162 | return (string) $this->uri->getFragment(); |
| 163 | } |
| 164 | /** |
| 165 | * {@inheritDoc} |
| 166 | */ |
| 167 | public function withScheme($scheme) : self |
| 168 | { |
| 169 | $scheme = $this->filterInput($scheme); |
| 170 | if ('' === $scheme) { |
| 171 | $scheme = null; |
| 172 | } |
| 173 | $uri = $this->uri->withScheme($scheme); |
| 174 | if ($uri->getScheme() === $this->uri->getScheme()) { |
| 175 | return $this; |
| 176 | } |
| 177 | return new self($uri); |
| 178 | } |
| 179 | /** |
| 180 | * Safely stringify input when possible. |
| 181 | * |
| 182 | * @param mixed $str the value to evaluate as a string |
| 183 | * |
| 184 | * @throws SyntaxError if the submitted data can not be converted to string |
| 185 | * |
| 186 | * @return string|mixed |
| 187 | */ |
| 188 | private function filterInput($str) |
| 189 | { |
| 190 | if (is_scalar($str) || is_object($str) && method_exists($str, '__toString')) { |
| 191 | return (string) $str; |
| 192 | } |
| 193 | return $str; |
| 194 | } |
| 195 | /** |
| 196 | * {@inheritDoc} |
| 197 | */ |
| 198 | public function withUserInfo($user, $password = null) : self |
| 199 | { |
| 200 | $user = $this->filterInput($user); |
| 201 | if ('' === $user) { |
| 202 | $user = null; |
| 203 | } |
| 204 | $uri = $this->uri->withUserInfo($user, $password); |
| 205 | if ($uri->getUserInfo() === $this->uri->getUserInfo()) { |
| 206 | return $this; |
| 207 | } |
| 208 | return new self($uri); |
| 209 | } |
| 210 | /** |
| 211 | * {@inheritDoc} |
| 212 | */ |
| 213 | public function withHost($host) : self |
| 214 | { |
| 215 | $host = $this->filterInput($host); |
| 216 | if ('' === $host) { |
| 217 | $host = null; |
| 218 | } |
| 219 | $uri = $this->uri->withHost($host); |
| 220 | if ($uri->getHost() === $this->uri->getHost()) { |
| 221 | return $this; |
| 222 | } |
| 223 | return new self($uri); |
| 224 | } |
| 225 | /** |
| 226 | * {@inheritDoc} |
| 227 | */ |
| 228 | public function withPort($port) : self |
| 229 | { |
| 230 | $uri = $this->uri->withPort($port); |
| 231 | if ($uri->getPort() === $this->uri->getPort()) { |
| 232 | return $this; |
| 233 | } |
| 234 | return new self($uri); |
| 235 | } |
| 236 | /** |
| 237 | * {@inheritDoc} |
| 238 | */ |
| 239 | public function withPath($path) : self |
| 240 | { |
| 241 | $uri = $this->uri->withPath($path); |
| 242 | if ($uri->getPath() === $this->uri->getPath()) { |
| 243 | return $this; |
| 244 | } |
| 245 | return new self($uri); |
| 246 | } |
| 247 | /** |
| 248 | * {@inheritDoc} |
| 249 | */ |
| 250 | public function withQuery($query) : self |
| 251 | { |
| 252 | $query = $this->filterInput($query); |
| 253 | if ('' === $query) { |
| 254 | $query = null; |
| 255 | } |
| 256 | $uri = $this->uri->withQuery($query); |
| 257 | if ($uri->getQuery() === $this->uri->getQuery()) { |
| 258 | return $this; |
| 259 | } |
| 260 | return new self($uri); |
| 261 | } |
| 262 | /** |
| 263 | * {@inheritDoc} |
| 264 | */ |
| 265 | public function withFragment($fragment) : self |
| 266 | { |
| 267 | $fragment = $this->filterInput($fragment); |
| 268 | if ('' === $fragment) { |
| 269 | $fragment = null; |
| 270 | } |
| 271 | $uri = $this->uri->withFragment($fragment); |
| 272 | if ($uri->getFragment() === $this->uri->getFragment()) { |
| 273 | return $this; |
| 274 | } |
| 275 | return new self($uri); |
| 276 | } |
| 277 | /** |
| 278 | * {@inheritDoc} |
| 279 | */ |
| 280 | public function __toString() : string |
| 281 | { |
| 282 | return $this->uri->__toString(); |
| 283 | } |
| 284 | /** |
| 285 | * {@inheritDoc} |
| 286 | */ |
| 287 | public function jsonSerialize() : string |
| 288 | { |
| 289 | return $this->uri->__toString(); |
| 290 | } |
| 291 | } |
| 292 |