[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Nyholm\Psr7; 6 7 use Psr\Http\Message\UriInterface; 8 9 /** 10 * @author Michael Dowling and contributors to guzzlehttp/psr7 11 * @author Tobias Nyholm <[email protected]> 12 * @author Martijn van der Ven <[email protected]> 13 * 14 * @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise 15 */ 16 trait RequestTrait 17 { 18 /** @var string */ 19 private $method; 20 21 /** @var string|null */ 22 private $requestTarget; 23 24 /** @var UriInterface|null */ 25 private $uri; 26 27 public function getRequestTarget(): string 28 { 29 if (null !== $this->requestTarget) { 30 return $this->requestTarget; 31 } 32 33 if ('' === $target = $this->uri->getPath()) { 34 $target = '/'; 35 } 36 if ('' !== $this->uri->getQuery()) { 37 $target .= '?' . $this->uri->getQuery(); 38 } 39 40 return $target; 41 } 42 43 public function withRequestTarget($requestTarget): self 44 { 45 if (\preg_match('#\s#', $requestTarget)) { 46 throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace'); 47 } 48 49 $new = clone $this; 50 $new->requestTarget = $requestTarget; 51 52 return $new; 53 } 54 55 public function getMethod(): string 56 { 57 return $this->method; 58 } 59 60 public function withMethod($method): self 61 { 62 if (!\is_string($method)) { 63 throw new \InvalidArgumentException('Method must be a string'); 64 } 65 66 $new = clone $this; 67 $new->method = $method; 68 69 return $new; 70 } 71 72 public function getUri(): UriInterface 73 { 74 return $this->uri; 75 } 76 77 public function withUri(UriInterface $uri, $preserveHost = false): self 78 { 79 if ($uri === $this->uri) { 80 return $this; 81 } 82 83 $new = clone $this; 84 $new->uri = $uri; 85 86 if (!$preserveHost || !$this->hasHeader('Host')) { 87 $new->updateHostFromUri(); 88 } 89 90 return $new; 91 } 92 93 private function updateHostFromUri(): void 94 { 95 if ('' === $host = $this->uri->getHost()) { 96 return; 97 } 98 99 if (null !== ($port = $this->uri->getPort())) { 100 $host .= ':' . $port; 101 } 102 103 if (isset($this->headerNames['host'])) { 104 $header = $this->headerNames['host']; 105 } else { 106 $this->headerNames['host'] = $header = 'Host'; 107 } 108 109 // Ensure Host is the first header. 110 // See: http://tools.ietf.org/html/rfc7230#section-5.4 111 $this->headers = [$header => [$host]] + $this->headers; 112 } 113 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |