File indexing completed on 2024-12-29 05:27:23

0001 <?php
0002 namespace GuzzleHttp\Psr7;
0003 
0004 use Psr\Http\Message\ResponseInterface;
0005 use Psr\Http\Message\StreamInterface;
0006 
0007 /**
0008  * PSR-7 response implementation.
0009  */
0010 class Response implements ResponseInterface
0011 {
0012     use MessageTrait;
0013 
0014     /** @var array Map of standard HTTP status code/reason phrases */
0015     private static $phrases = [
0016         100 => 'Continue',
0017         101 => 'Switching Protocols',
0018         102 => 'Processing',
0019         200 => 'OK',
0020         201 => 'Created',
0021         202 => 'Accepted',
0022         203 => 'Non-Authoritative Information',
0023         204 => 'No Content',
0024         205 => 'Reset Content',
0025         206 => 'Partial Content',
0026         207 => 'Multi-status',
0027         208 => 'Already Reported',
0028         300 => 'Multiple Choices',
0029         301 => 'Moved Permanently',
0030         302 => 'Found',
0031         303 => 'See Other',
0032         304 => 'Not Modified',
0033         305 => 'Use Proxy',
0034         306 => 'Switch Proxy',
0035         307 => 'Temporary Redirect',
0036         400 => 'Bad Request',
0037         401 => 'Unauthorized',
0038         402 => 'Payment Required',
0039         403 => 'Forbidden',
0040         404 => 'Not Found',
0041         405 => 'Method Not Allowed',
0042         406 => 'Not Acceptable',
0043         407 => 'Proxy Authentication Required',
0044         408 => 'Request Time-out',
0045         409 => 'Conflict',
0046         410 => 'Gone',
0047         411 => 'Length Required',
0048         412 => 'Precondition Failed',
0049         413 => 'Request Entity Too Large',
0050         414 => 'Request-URI Too Large',
0051         415 => 'Unsupported Media Type',
0052         416 => 'Requested range not satisfiable',
0053         417 => 'Expectation Failed',
0054         418 => 'I\'m a teapot',
0055         422 => 'Unprocessable Entity',
0056         423 => 'Locked',
0057         424 => 'Failed Dependency',
0058         425 => 'Unordered Collection',
0059         426 => 'Upgrade Required',
0060         428 => 'Precondition Required',
0061         429 => 'Too Many Requests',
0062         431 => 'Request Header Fields Too Large',
0063         451 => 'Unavailable For Legal Reasons',
0064         500 => 'Internal Server Error',
0065         501 => 'Not Implemented',
0066         502 => 'Bad Gateway',
0067         503 => 'Service Unavailable',
0068         504 => 'Gateway Time-out',
0069         505 => 'HTTP Version not supported',
0070         506 => 'Variant Also Negotiates',
0071         507 => 'Insufficient Storage',
0072         508 => 'Loop Detected',
0073         511 => 'Network Authentication Required',
0074     ];
0075 
0076     /** @var string */
0077     private $reasonPhrase = '';
0078 
0079     /** @var int */
0080     private $statusCode = 200;
0081 
0082     /**
0083      * @param int                                  $status  Status code
0084      * @param array                                $headers Response headers
0085      * @param string|null|resource|StreamInterface $body    Response body
0086      * @param string                               $version Protocol version
0087      * @param string|null                          $reason  Reason phrase (when empty a default will be used based on the status code)
0088      */
0089     public function __construct(
0090         $status = 200,
0091         array $headers = [],
0092         $body = null,
0093         $version = '1.1',
0094         $reason = null
0095     ) {
0096         $this->statusCode = (int) $status;
0097 
0098         if ($body !== '' && $body !== null) {
0099             $this->stream = stream_for($body);
0100         }
0101 
0102         $this->setHeaders($headers);
0103         if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
0104             $this->reasonPhrase = self::$phrases[$this->statusCode];
0105         } else {
0106             $this->reasonPhrase = (string) $reason;
0107         }
0108 
0109         $this->protocol = $version;
0110     }
0111 
0112     public function getStatusCode()
0113     {
0114         return $this->statusCode;
0115     }
0116 
0117     public function getReasonPhrase()
0118     {
0119         return $this->reasonPhrase;
0120     }
0121 
0122     public function withStatus($code, $reasonPhrase = '')
0123     {
0124         $new = clone $this;
0125         $new->statusCode = (int) $code;
0126         if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
0127             $reasonPhrase = self::$phrases[$new->statusCode];
0128         }
0129         $new->reasonPhrase = $reasonPhrase;
0130         return $new;
0131     }
0132 }