[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/third-party/Nyholm/Psr7/ -> MessageTrait.php (source)

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


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref