annotate vendor/zendframework/zend-diactoros/src/Response.php @ 0:c75dbcec494b

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