[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/nyholm/psr7/src/ -> MessageTrait.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Nyholm\Psr7;
   6  
   7  use Psr\Http\Message\StreamInterface;
   8  
   9  /**
  10   * Trait implementing functionality common to requests and responses.
  11   *
  12   * @author Michael Dowling and contributors to guzzlehttp/psr7
  13   * @author Tobias Nyholm <[email protected]>
  14   * @author Martijn van der Ven <[email protected]>
  15   *
  16   * @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise
  17   */
  18  trait MessageTrait
  19  {
  20      /** @var array Map of all registered headers, as original name => array of values */
  21      private $headers = [];
  22  
  23      /** @var array Map of lowercase header name => original name at registration */
  24      private $headerNames = [];
  25  
  26      /** @var string */
  27      private $protocol = '1.1';
  28  
  29      /** @var StreamInterface|null */
  30      private $stream;
  31  
  32      public function getProtocolVersion(): string
  33      {
  34          return $this->protocol;
  35      }
  36  
  37      public function withProtocolVersion($version): self
  38      {
  39          if ($this->protocol === $version) {
  40              return $this;
  41          }
  42  
  43          $new = clone $this;
  44          $new->protocol = $version;
  45  
  46          return $new;
  47      }
  48  
  49      public function getHeaders(): array
  50      {
  51          return $this->headers;
  52      }
  53  
  54      public function hasHeader($header): bool
  55      {
  56          return isset($this->headerNames[\strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]);
  57      }
  58  
  59      public function getHeader($header): array
  60      {
  61          $header = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  62          if (!isset($this->headerNames[$header])) {
  63              return [];
  64          }
  65  
  66          $header = $this->headerNames[$header];
  67  
  68          return $this->headers[$header];
  69      }
  70  
  71      public function getHeaderLine($header): string
  72      {
  73          return \implode(', ', $this->getHeader($header));
  74      }
  75  
  76      public function withHeader($header, $value): self
  77      {
  78          $value = $this->validateAndTrimHeader($header, $value);
  79          $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  80  
  81          $new = clone $this;
  82          if (isset($new->headerNames[$normalized])) {
  83              unset($new->headers[$new->headerNames[$normalized]]);
  84          }
  85          $new->headerNames[$normalized] = $header;
  86          $new->headers[$header] = $value;
  87  
  88          return $new;
  89      }
  90  
  91      public function withAddedHeader($header, $value): self
  92      {
  93          if (!\is_string($header) || '' === $header) {
  94              throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
  95          }
  96  
  97          $new = clone $this;
  98          $new->setHeaders([$header => $value]);
  99  
 100          return $new;
 101      }
 102  
 103      public function withoutHeader($header): self
 104      {
 105          $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
 106          if (!isset($this->headerNames[$normalized])) {
 107              return $this;
 108          }
 109  
 110          $header = $this->headerNames[$normalized];
 111          $new = clone $this;
 112          unset($new->headers[$header], $new->headerNames[$normalized]);
 113  
 114          return $new;
 115      }
 116  
 117      public function getBody(): StreamInterface
 118      {
 119          if (null === $this->stream) {
 120              $this->stream = Stream::create('');
 121          }
 122  
 123          return $this->stream;
 124      }
 125  
 126      public function withBody(StreamInterface $body): self
 127      {
 128          if ($body === $this->stream) {
 129              return $this;
 130          }
 131  
 132          $new = clone $this;
 133          $new->stream = $body;
 134  
 135          return $new;
 136      }
 137  
 138      private function setHeaders(array $headers): void
 139      {
 140          foreach ($headers as $header => $value) {
 141              if (\is_int($header)) {
 142                  // If a header name was set to a numeric string, PHP will cast the key to an int.
 143                  // We must cast it back to a string in order to comply with validation.
 144                  $header = (string) $header;
 145              }
 146              $value = $this->validateAndTrimHeader($header, $value);
 147              $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
 148              if (isset($this->headerNames[$normalized])) {
 149                  $header = $this->headerNames[$normalized];
 150                  $this->headers[$header] = \array_merge($this->headers[$header], $value);
 151              } else {
 152                  $this->headerNames[$normalized] = $header;
 153                  $this->headers[$header] = $value;
 154              }
 155          }
 156      }
 157  
 158      /**
 159       * Make sure the header complies with RFC 7230.
 160       *
 161       * Header names must be a non-empty string consisting of token characters.
 162       *
 163       * Header values must be strings consisting of visible characters with all optional
 164       * leading and trailing whitespace stripped. This method will always strip such
 165       * optional whitespace. Note that the method does not allow folding whitespace within
 166       * the values as this was deprecated for almost all instances by the RFC.
 167       *
 168       * header-field = field-name ":" OWS field-value OWS
 169       * field-name   = 1*( "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^"
 170       *              / "_" / "`" / "|" / "~" / %x30-39 / ( %x41-5A / %x61-7A ) )
 171       * OWS          = *( SP / HTAB )
 172       * field-value  = *( ( %x21-7E / %x80-FF ) [ 1*( SP / HTAB ) ( %x21-7E / %x80-FF ) ] )
 173       *
 174       * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
 175       */
 176      private function validateAndTrimHeader($header, $values): array
 177      {
 178          if (!\is_string($header) || 1 !== \preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) {
 179              throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
 180          }
 181  
 182          if (!\is_array($values)) {
 183              // This is simple, just one value.
 184              if ((!\is_numeric($values) && !\is_string($values)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) {
 185                  throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
 186              }
 187  
 188              return [\trim((string) $values, " \t")];
 189          }
 190  
 191          if (empty($values)) {
 192              throw new \InvalidArgumentException('Header values must be a string or an array of strings, empty array given.');
 193          }
 194  
 195          // Assert Non empty array
 196          $returnValues = [];
 197          foreach ($values as $v) {
 198              if ((!\is_numeric($v) && !\is_string($v)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) {
 199                  throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
 200              }
 201  
 202              $returnValues[] = \trim((string) $v, " \t");
 203          }
 204  
 205          return $returnValues;
 206      }
 207  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer