You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
4.7 KiB

  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\StreamInterface;
  5. /**
  6. * PSR-7 response implementation.
  7. */
  8. class Response implements ResponseInterface
  9. {
  10. use MessageTrait;
  11. /** @var array Map of standard HTTP status code/reason phrases */
  12. private static $phrases = [
  13. 100 => 'Continue',
  14. 101 => 'Switching Protocols',
  15. 102 => 'Processing',
  16. 200 => 'OK',
  17. 201 => 'Created',
  18. 202 => 'Accepted',
  19. 203 => 'Non-Authoritative Information',
  20. 204 => 'No Content',
  21. 205 => 'Reset Content',
  22. 206 => 'Partial Content',
  23. 207 => 'Multi-status',
  24. 208 => 'Already Reported',
  25. 300 => 'Multiple Choices',
  26. 301 => 'Moved Permanently',
  27. 302 => 'Found',
  28. 303 => 'See Other',
  29. 304 => 'Not Modified',
  30. 305 => 'Use Proxy',
  31. 306 => 'Switch Proxy',
  32. 307 => 'Temporary Redirect',
  33. 400 => 'Bad Request',
  34. 401 => 'Unauthorized',
  35. 402 => 'Payment Required',
  36. 403 => 'Forbidden',
  37. 404 => 'Not Found',
  38. 405 => 'Method Not Allowed',
  39. 406 => 'Not Acceptable',
  40. 407 => 'Proxy Authentication Required',
  41. 408 => 'Request Time-out',
  42. 409 => 'Conflict',
  43. 410 => 'Gone',
  44. 411 => 'Length Required',
  45. 412 => 'Precondition Failed',
  46. 413 => 'Request Entity Too Large',
  47. 414 => 'Request-URI Too Large',
  48. 415 => 'Unsupported Media Type',
  49. 416 => 'Requested range not satisfiable',
  50. 417 => 'Expectation Failed',
  51. 418 => 'I\'m a teapot',
  52. 422 => 'Unprocessable Entity',
  53. 423 => 'Locked',
  54. 424 => 'Failed Dependency',
  55. 425 => 'Unordered Collection',
  56. 426 => 'Upgrade Required',
  57. 428 => 'Precondition Required',
  58. 429 => 'Too Many Requests',
  59. 431 => 'Request Header Fields Too Large',
  60. 451 => 'Unavailable For Legal Reasons',
  61. 500 => 'Internal Server Error',
  62. 501 => 'Not Implemented',
  63. 502 => 'Bad Gateway',
  64. 503 => 'Service Unavailable',
  65. 504 => 'Gateway Time-out',
  66. 505 => 'HTTP Version not supported',
  67. 506 => 'Variant Also Negotiates',
  68. 507 => 'Insufficient Storage',
  69. 508 => 'Loop Detected',
  70. 511 => 'Network Authentication Required',
  71. ];
  72. /** @var string */
  73. private $reasonPhrase = '';
  74. /** @var int */
  75. private $statusCode = 200;
  76. /**
  77. * @param int $status Status code
  78. * @param array $headers Response headers
  79. * @param string|null|resource|StreamInterface $body Response body
  80. * @param string $version Protocol version
  81. * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
  82. */
  83. public function __construct(
  84. $status = 200,
  85. array $headers = [],
  86. $body = null,
  87. $version = '1.1',
  88. $reason = null
  89. ) {
  90. $this->assertStatusCodeIsInteger($status);
  91. $status = (int) $status;
  92. $this->assertStatusCodeRange($status);
  93. $this->statusCode = $status;
  94. if ($body !== '' && $body !== null) {
  95. $this->stream = stream_for($body);
  96. }
  97. $this->setHeaders($headers);
  98. if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
  99. $this->reasonPhrase = self::$phrases[$this->statusCode];
  100. } else {
  101. $this->reasonPhrase = (string) $reason;
  102. }
  103. $this->protocol = $version;
  104. }
  105. public function getStatusCode()
  106. {
  107. return $this->statusCode;
  108. }
  109. public function getReasonPhrase()
  110. {
  111. return $this->reasonPhrase;
  112. }
  113. public function withStatus($code, $reasonPhrase = '')
  114. {
  115. $this->assertStatusCodeIsInteger($code);
  116. $code = (int) $code;
  117. $this->assertStatusCodeRange($code);
  118. $new = clone $this;
  119. $new->statusCode = $code;
  120. if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
  121. $reasonPhrase = self::$phrases[$new->statusCode];
  122. }
  123. $new->reasonPhrase = $reasonPhrase;
  124. return $new;
  125. }
  126. private function assertStatusCodeIsInteger($statusCode)
  127. {
  128. if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) {
  129. throw new \InvalidArgumentException('Status code must be an integer value.');
  130. }
  131. }
  132. private function assertStatusCodeRange($statusCode)
  133. {
  134. if ($statusCode < 100 || $statusCode >= 600) {
  135. throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
  136. }
  137. }
  138. }