ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Response.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\HTTP;
4 
12 class Response extends Message implements ResponseInterface {
13 
19  static $statusCodes = [
20  100 => 'Continue',
21  101 => 'Switching Protocols',
22  102 => 'Processing',
23  200 => 'OK',
24  201 => 'Created',
25  202 => 'Accepted',
26  203 => 'Non-Authorative Information',
27  204 => 'No Content',
28  205 => 'Reset Content',
29  206 => 'Partial Content',
30  207 => 'Multi-Status', // RFC 4918
31  208 => 'Already Reported', // RFC 5842
32  226 => 'IM Used', // RFC 3229
33  300 => 'Multiple Choices',
34  301 => 'Moved Permanently',
35  302 => 'Found',
36  303 => 'See Other',
37  304 => 'Not Modified',
38  305 => 'Use Proxy',
39  307 => 'Temporary Redirect',
40  308 => 'Permanent Redirect',
41  400 => 'Bad Request',
42  401 => 'Unauthorized',
43  402 => 'Payment Required',
44  403 => 'Forbidden',
45  404 => 'Not Found',
46  405 => 'Method Not Allowed',
47  406 => 'Not Acceptable',
48  407 => 'Proxy Authentication Required',
49  408 => 'Request Timeout',
50  409 => 'Conflict',
51  410 => 'Gone',
52  411 => 'Length Required',
53  412 => 'Precondition failed',
54  413 => 'Request Entity Too Large',
55  414 => 'Request-URI Too Long',
56  415 => 'Unsupported Media Type',
57  416 => 'Requested Range Not Satisfiable',
58  417 => 'Expectation Failed',
59  418 => 'I\'m a teapot', // RFC 2324
60  421 => 'Misdirected Request', // RFC7540 (HTTP/2)
61  422 => 'Unprocessable Entity', // RFC 4918
62  423 => 'Locked', // RFC 4918
63  424 => 'Failed Dependency', // RFC 4918
64  426 => 'Upgrade Required',
65  428 => 'Precondition Required', // RFC 6585
66  429 => 'Too Many Requests', // RFC 6585
67  431 => 'Request Header Fields Too Large', // RFC 6585
68  451 => 'Unavailable For Legal Reasons', // draft-tbray-http-legally-restricted-status
69  500 => 'Internal Server Error',
70  501 => 'Not Implemented',
71  502 => 'Bad Gateway',
72  503 => 'Service Unavailable',
73  504 => 'Gateway Timeout',
74  505 => 'HTTP Version not supported',
75  506 => 'Variant Also Negotiates',
76  507 => 'Insufficient Storage', // RFC 4918
77  508 => 'Loop Detected', // RFC 5842
78  509 => 'Bandwidth Limit Exceeded', // non-standard
79  510 => 'Not extended',
80  511 => 'Network Authentication Required', // RFC 6585
81  ];
82 
88  protected $status;
89 
95  protected $statusText;
96 
104  function __construct($status = null, array $headers = null, $body = null) {
105 
106  if (!is_null($status)) $this->setStatus($status);
107  if (!is_null($headers)) $this->setHeaders($headers);
108  if (!is_null($body)) $this->setBody($body);
109 
110  }
111 
112 
118  function getStatus() {
119 
120  return $this->status;
121 
122  }
123 
131  function getStatusText() {
132 
133  return $this->statusText;
134 
135  }
136 
150  function setStatus($status) {
151 
152  if (ctype_digit($status) || is_int($status)) {
153 
154  $statusCode = $status;
155  $statusText = isset(self::$statusCodes[$status]) ? self::$statusCodes[$status] : 'Unknown';
156 
157  } else {
158  list(
159  $statusCode,
161  ) = explode(' ', $status, 2);
162  }
163  if ($statusCode < 100 || $statusCode > 999) {
164  throw new \InvalidArgumentException('The HTTP status code must be exactly 3 digits');
165  }
166 
167  $this->status = $statusCode;
168  $this->statusText = $statusText;
169 
170  }
171 
179  function __toString() {
180 
181  $str = 'HTTP/' . $this->httpVersion . ' ' . $this->getStatus() . ' ' . $this->getStatusText() . "\r\n";
182  foreach ($this->getHeaders() as $key => $value) {
183  foreach ($value as $v) {
184  $str .= $key . ": " . $v . "\r\n";
185  }
186  }
187  $str .= "\r\n";
188  $str .= $this->getBodyAsString();
189  return $str;
190 
191  }
192 
193 }
This interface represents a HTTP response.
This is the abstract base class for both the Request and Response objects.
Definition: Message.php:14
getStatus()
Returns the current HTTP status code.
Definition: Response.php:118
setStatus($status)
Sets the HTTP status code.
Definition: Response.php:150
setBody($body)
Replaces the body resource with a new stream or string.
Definition: Message.php:103
This class represents a single HTTP response.
Definition: Response.php:12
getHeaders()
Returns all the HTTP headers as an array.
Definition: Message.php:116
__construct($status=null, array $headers=null, $body=null)
Creates the response object.
Definition: Response.php:104
getBodyAsString()
Returns the body as a string.
Definition: Message.php:68
getStatusText()
Returns the human-readable status string.
Definition: Response.php:131
setHeaders(array $headers)
Sets a new set of HTTP headers.
Definition: Message.php:216
__toString()
Serializes the response object as a string.
Definition: Response.php:179
$key
Definition: croninfo.php:18