Няма описание

Response.php 3.7KB

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