[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/ -> Response.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros;
  12  
  13  use Psr\Http\Message\ResponseInterface;
  14  use Psr\Http\Message\StreamInterface;
  15  
  16  use function gettype;
  17  use function is_float;
  18  use function is_numeric;
  19  use function is_scalar;
  20  use function sprintf;
  21  
  22  /**
  23   * HTTP response encapsulation.
  24   *
  25   * Responses are considered immutable; all methods that might change state are
  26   * implemented such that they retain the internal state of the current
  27   * message and return a new instance that contains the changed state.
  28   */
  29  class Response implements ResponseInterface
  30  {
  31      use MessageTrait;
  32  
  33      const MIN_STATUS_CODE_VALUE = 100;
  34      const MAX_STATUS_CODE_VALUE = 599;
  35  
  36      /**
  37       * Map of standard HTTP status code/reason phrases
  38       *
  39       * @var array
  40       */
  41      private $phrases = [
  42          // INFORMATIONAL CODES
  43          100 => 'Continue',
  44          101 => 'Switching Protocols',
  45          102 => 'Processing',
  46          103 => 'Early Hints',
  47          // SUCCESS CODES
  48          200 => 'OK',
  49          201 => 'Created',
  50          202 => 'Accepted',
  51          203 => 'Non-Authoritative Information',
  52          204 => 'No Content',
  53          205 => 'Reset Content',
  54          206 => 'Partial Content',
  55          207 => 'Multi-Status',
  56          208 => 'Already Reported',
  57          226 => 'IM Used',
  58          // REDIRECTION CODES
  59          300 => 'Multiple Choices',
  60          301 => 'Moved Permanently',
  61          302 => 'Found',
  62          303 => 'See Other',
  63          304 => 'Not Modified',
  64          305 => 'Use Proxy',
  65          306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
  66          307 => 'Temporary Redirect',
  67          308 => 'Permanent Redirect',
  68          // CLIENT ERROR
  69          400 => 'Bad Request',
  70          401 => 'Unauthorized',
  71          402 => 'Payment Required',
  72          403 => 'Forbidden',
  73          404 => 'Not Found',
  74          405 => 'Method Not Allowed',
  75          406 => 'Not Acceptable',
  76          407 => 'Proxy Authentication Required',
  77          408 => 'Request Timeout',
  78          409 => 'Conflict',
  79          410 => 'Gone',
  80          411 => 'Length Required',
  81          412 => 'Precondition Failed',
  82          413 => 'Payload Too Large',
  83          414 => 'URI Too Long',
  84          415 => 'Unsupported Media Type',
  85          416 => 'Range Not Satisfiable',
  86          417 => 'Expectation Failed',
  87          418 => 'I\'m a teapot',
  88          421 => 'Misdirected Request',
  89          422 => 'Unprocessable Entity',
  90          423 => 'Locked',
  91          424 => 'Failed Dependency',
  92          425 => 'Too Early',
  93          426 => 'Upgrade Required',
  94          428 => 'Precondition Required',
  95          429 => 'Too Many Requests',
  96          431 => 'Request Header Fields Too Large',
  97          444 => 'Connection Closed Without Response',
  98          451 => 'Unavailable For Legal Reasons',
  99          // SERVER ERROR
 100          499 => 'Client Closed Request',
 101          500 => 'Internal Server Error',
 102          501 => 'Not Implemented',
 103          502 => 'Bad Gateway',
 104          503 => 'Service Unavailable',
 105          504 => 'Gateway Timeout',
 106          505 => 'HTTP Version Not Supported',
 107          506 => 'Variant Also Negotiates',
 108          507 => 'Insufficient Storage',
 109          508 => 'Loop Detected',
 110          510 => 'Not Extended',
 111          511 => 'Network Authentication Required',
 112          599 => 'Network Connect Timeout Error',
 113      ];
 114  
 115      /**
 116       * @var string
 117       */
 118      private $reasonPhrase;
 119  
 120      /**
 121       * @var int
 122       */
 123      private $statusCode;
 124  
 125      /**
 126       * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
 127       * @param int $status Status code for the response, if any.
 128       * @param array $headers Headers for the response, if any.
 129       * @throws Exception\InvalidArgumentException on any invalid element.
 130       */
 131      public function __construct($body = 'php://memory', int $status = 200, array $headers = [])
 132      {
 133          $this->setStatusCode($status);
 134          $this->stream = $this->getStream($body, 'wb+');
 135          $this->setHeaders($headers);
 136      }
 137  
 138      /**
 139       * {@inheritdoc}
 140       */
 141      public function getStatusCode() : int
 142      {
 143          return $this->statusCode;
 144      }
 145  
 146      /**
 147       * {@inheritdoc}
 148       */
 149      public function getReasonPhrase() : string
 150      {
 151          return $this->reasonPhrase;
 152      }
 153  
 154      /**
 155       * {@inheritdoc}
 156       */
 157      public function withStatus($code, $reasonPhrase = '') : Response
 158      {
 159          $new = clone $this;
 160          $new->setStatusCode($code, $reasonPhrase);
 161          return $new;
 162      }
 163  
 164      /**
 165       * Set a valid status code.
 166       *
 167       * @param int $code
 168       * @param string $reasonPhrase
 169       * @throws Exception\InvalidArgumentException on an invalid status code.
 170       */
 171      private function setStatusCode($code, $reasonPhrase = '') : void
 172      {
 173          if (! is_numeric($code)
 174              || is_float($code)
 175              || $code < static::MIN_STATUS_CODE_VALUE
 176              || $code > static::MAX_STATUS_CODE_VALUE
 177          ) {
 178              throw new Exception\InvalidArgumentException(sprintf(
 179                  'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
 180                  is_scalar($code) ? $code : gettype($code),
 181                  static::MIN_STATUS_CODE_VALUE,
 182                  static::MAX_STATUS_CODE_VALUE
 183              ));
 184          }
 185  
 186          if (! is_string($reasonPhrase)) {
 187              throw new Exception\InvalidArgumentException(sprintf(
 188                  'Unsupported response reason phrase; must be a string, received %s',
 189                  is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
 190              ));
 191          }
 192  
 193          if ($reasonPhrase === '' && isset($this->phrases[$code])) {
 194              $reasonPhrase = $this->phrases[$code];
 195          }
 196  
 197          $this->reasonPhrase = $reasonPhrase;
 198          $this->statusCode = (int) $code;
 199      }
 200  }


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