vendor/mpdf/psr-http-message-shim/src/Request.php line 44

Open in your IDE?
  1. <?php
  2. namespace Mpdf\PsrHttpMessageShim;
  3. use Psr\Http\Message\StreamInterface;
  4. use Psr\Http\Message\UriInterface;
  5. /**
  6.  * PSR-7 URI implementation ported from nyholm/psr7 and adapted for PHP 5.6
  7.  *
  8.  * @link https://github.com/Nyholm/psr7/blob/master/src/Uri.php
  9.  */
  10. class Request implements \Psr\Http\Message\RequestInterface
  11. {
  12.     /** @var string */
  13.     private $method;
  14.     /** @var null|string */
  15.     private $requestTarget;
  16.     /** @var null|UriInterface */
  17.     private $uri;
  18.     /** @var array Map of all registered headers, as original name => array of values */
  19.     private $headers = [];
  20.     /** @var array Map of lowercase header name => original name at registration */
  21.     private $headerNames = [];
  22.     /** @var string */
  23.     private $protocol;
  24.     /** @var StreamInterface */
  25.     private $stream;
  26.     /**
  27.      * @param string                               $method  HTTP method
  28.      * @param string|UriInterface                  $uri     URI
  29.      * @param array                                $headers Request headers
  30.      * @param string|null|resource|StreamInterface $body    Request body
  31.      * @param string                               $version Protocol version
  32.      */
  33.     public function __construct(
  34.         $method,
  35.         $uri,
  36.         array $headers = [],
  37.         $body null,
  38.         $version '1.1'
  39.     ) {
  40.         if (!($uri instanceof UriInterface)) {
  41.             $uri = new Uri($uri);
  42.         }
  43.         $this->method $method;
  44.         $this->uri $uri;
  45.         $this->setHeaders($headers);
  46.         $this->protocol $version;
  47.         if (!$this->hasHeader('Host')) {
  48.             $this->updateHostFromUri();
  49.         }
  50.         if ($body !== '' && $body !== null) {
  51.             $this->stream Stream::create($body);
  52.         }
  53.     }
  54.     public function getRequestTarget()
  55.     {
  56.         if ($this->requestTarget !== null) {
  57.             return $this->requestTarget;
  58.         }
  59.         $target $this->uri->getPath();
  60.         if ($target == '') {
  61.             $target '/';
  62.         }
  63.         if ($this->uri->getQuery() != '') {
  64.             $target .= '?'.$this->uri->getQuery();
  65.         }
  66.         return $target;
  67.     }
  68.     public function withRequestTarget($requestTarget)
  69.     {
  70.         if (preg_match('#\s#'$requestTarget)) {
  71.             throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
  72.         }
  73.         $new = clone $this;
  74.         $new->requestTarget $requestTarget;
  75.         return $new;
  76.     }
  77.     public function getMethod()
  78.     {
  79.         return $this->method;
  80.     }
  81.     public function withMethod($method)
  82.     {
  83.         $new = clone $this;
  84.         $new->method $method;
  85.         return $new;
  86.     }
  87.     public function getUri()
  88.     {
  89.         return $this->uri;
  90.     }
  91.     public function withUri(UriInterface $uri$preserveHost false)
  92.     {
  93.         if ($uri === $this->uri) {
  94.             return $this;
  95.         }
  96.         $new = clone $this;
  97.         $new->uri $uri;
  98.         if (!$preserveHost || !$this->hasHeader('Host')) {
  99.             $new->updateHostFromUri();
  100.         }
  101.         return $new;
  102.     }
  103.     private function updateHostFromUri()
  104.     {
  105.         $host $this->uri->getHost();
  106.         if ($host == '') {
  107.             return;
  108.         }
  109.         if (($port $this->uri->getPort()) !== null) {
  110.             $host .= ':'.$port;
  111.         }
  112.         if (isset($this->headerNames['host'])) {
  113.             $header $this->headerNames['host'];
  114.         } else {
  115.             $header 'Host';
  116.             $this->headerNames['host'] = 'Host';
  117.         }
  118.         // Ensure Host is the first header.
  119.         // See: http://tools.ietf.org/html/rfc7230#section-5.4
  120.         $this->headers = [$header => [$host]] + $this->headers;
  121.     }
  122.     public function getProtocolVersion()
  123.     {
  124.         return $this->protocol;
  125.     }
  126.     public function withProtocolVersion($version)
  127.     {
  128.         if ($this->protocol === $version) {
  129.             return $this;
  130.         }
  131.         $new = clone $this;
  132.         $new->protocol $version;
  133.         return $new;
  134.     }
  135.     public function getHeaders()
  136.     {
  137.         return $this->headers;
  138.     }
  139.     public function hasHeader($header)
  140.     {
  141.         return isset($this->headerNames[strtolower($header)]);
  142.     }
  143.     public function getHeader($header)
  144.     {
  145.         $header strtolower($header);
  146.         if (!isset($this->headerNames[$header])) {
  147.             return [];
  148.         }
  149.         $header $this->headerNames[$header];
  150.         return $this->headers[$header];
  151.     }
  152.     public function getHeaderLine($header)
  153.     {
  154.         return implode(', '$this->getHeader($header));
  155.     }
  156.     public function withHeader($header$value)
  157.     {
  158.         if (!is_array($value)) {
  159.             $value = [$value];
  160.         }
  161.         $value $this->trimHeaderValues($value);
  162.         $normalized strtolower($header);
  163.         $new = clone $this;
  164.         if (isset($new->headerNames[$normalized])) {
  165.             unset($new->headers[$new->headerNames[$normalized]]);
  166.         }
  167.         $new->headerNames[$normalized] = $header;
  168.         $new->headers[$header] = $value;
  169.         return $new;
  170.     }
  171.     public function withAddedHeader($header$value)
  172.     {
  173.         if (!is_array($value)) {
  174.             $value = [$value];
  175.         }
  176.         $value $this->trimHeaderValues($value);
  177.         $normalized strtolower($header);
  178.         $new = clone $this;
  179.         if (isset($new->headerNames[$normalized])) {
  180.             $header $this->headerNames[$normalized];
  181.             $new->headers[$header] = array_merge($this->headers[$header], $value);
  182.         } else {
  183.             $new->headerNames[$normalized] = $header;
  184.             $new->headers[$header] = $value;
  185.         }
  186.         return $new;
  187.     }
  188.     public function withoutHeader($header)
  189.     {
  190.         $normalized strtolower($header);
  191.         if (!isset($this->headerNames[$normalized])) {
  192.             return $this;
  193.         }
  194.         $header $this->headerNames[$normalized];
  195.         $new = clone $this;
  196.         unset($new->headers[$header], $new->headerNames[$normalized]);
  197.         return $new;
  198.     }
  199.     public function getBody()
  200.     {
  201.         if (!$this->stream) {
  202.             $this->stream Stream::create('');
  203.             $this->stream->rewind();
  204.         }
  205.         return $this->stream;
  206.     }
  207.     public function withBody(StreamInterface $body)
  208.     {
  209.         if ($body === $this->stream) {
  210.             return $this;
  211.         }
  212.         $new = clone $this;
  213.         $new->stream $body;
  214.         return $new;
  215.     }
  216.     private function setHeaders(array $headers)
  217.     {
  218.         $this->headerNames $this->headers = [];
  219.         foreach ($headers as $header => $value) {
  220.             if (!is_array($value)) {
  221.                 $value = [$value];
  222.             }
  223.             $value $this->trimHeaderValues($value);
  224.             $normalized strtolower($header);
  225.             if (isset($this->headerNames[$normalized])) {
  226.                 $header $this->headerNames[$normalized];
  227.                 $this->headers[$header] = array_merge($this->headers[$header], $value);
  228.             } else {
  229.                 $this->headerNames[$normalized] = $header;
  230.                 $this->headers[$header] = $value;
  231.             }
  232.         }
  233.     }
  234.     /**
  235.      * Trims whitespace from the header values.
  236.      *
  237.      * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
  238.      *
  239.      * header-field = field-name ":" OWS field-value OWS
  240.      * OWS          = *( SP / HTAB )
  241.      *
  242.      * @param string[] $values Header values
  243.      *
  244.      * @return string[] Trimmed header values
  245.      *
  246.      * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
  247.      */
  248.     private function trimHeaderValues(array $values)
  249.     {
  250.         return array_map(function ($value) {
  251.             return trim($value" \t");
  252.         }, $values);
  253.     }
  254. }