ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Response.php
Go to the documentation of this file.
1<?php
9namespace Slim\Http;
10
11use InvalidArgumentException;
16
27class Response extends Message implements ResponseInterface
28{
35
41 protected $reasonPhrase = '';
42
48 protected static $messages = [
49 //Informational 1xx
50 StatusCode::HTTP_CONTINUE => 'Continue',
51 StatusCode::HTTP_SWITCHING_PROTOCOLS => 'Switching Protocols',
52 StatusCode::HTTP_PROCESSING => 'Processing',
53 //Successful 2xx
54 StatusCode::HTTP_OK => 'OK',
55 StatusCode::HTTP_CREATED => 'Created',
56 StatusCode::HTTP_ACCEPTED => 'Accepted',
57 StatusCode::HTTP_NONAUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
58 StatusCode::HTTP_NO_CONTENT => 'No Content',
59 StatusCode::HTTP_RESET_CONTENT => 'Reset Content',
60 StatusCode::HTTP_PARTIAL_CONTENT => 'Partial Content',
61 StatusCode::HTTP_MULTI_STATUS => 'Multi-Status',
62 StatusCode::HTTP_ALREADY_REPORTED => 'Already Reported',
63 StatusCode::HTTP_IM_USED => 'IM Used',
64 //Redirection 3xx
65 StatusCode::HTTP_MULTIPLE_CHOICES => 'Multiple Choices',
66 StatusCode::HTTP_MOVED_PERMANENTLY => 'Moved Permanently',
67 StatusCode::HTTP_FOUND => 'Found',
68 StatusCode::HTTP_SEE_OTHER => 'See Other',
69 StatusCode::HTTP_NOT_MODIFIED => 'Not Modified',
70 StatusCode::HTTP_USE_PROXY => 'Use Proxy',
71 StatusCode::HTTP_UNUSED => '(Unused)',
72 StatusCode::HTTP_TEMPORARY_REDIRECT => 'Temporary Redirect',
73 StatusCode::HTTP_PERMANENT_REDIRECT => 'Permanent Redirect',
74 //Client Error 4xx
75 StatusCode::HTTP_BAD_REQUEST => 'Bad Request',
76 StatusCode::HTTP_UNAUTHORIZED => 'Unauthorized',
77 StatusCode::HTTP_PAYMENT_REQUIRED => 'Payment Required',
78 StatusCode::HTTP_FORBIDDEN => 'Forbidden',
79 StatusCode::HTTP_NOT_FOUND => 'Not Found',
80 StatusCode::HTTP_METHOD_NOT_ALLOWED => 'Method Not Allowed',
81 StatusCode::HTTP_NOT_ACCEPTABLE => 'Not Acceptable',
82 StatusCode::HTTP_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
83 StatusCode::HTTP_REQUEST_TIMEOUT => 'Request Timeout',
84 StatusCode::HTTP_CONFLICT => 'Conflict',
85 StatusCode::HTTP_GONE => 'Gone',
86 StatusCode::HTTP_LENGTH_REQUIRED => 'Length Required',
87 StatusCode::HTTP_PRECONDITION_FAILED => 'Precondition Failed',
88 StatusCode::HTTP_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
89 StatusCode::HTTP_REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
90 StatusCode::HTTP_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
91 StatusCode::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
92 StatusCode::HTTP_EXPECTATION_FAILED => 'Expectation Failed',
93 StatusCode::HTTP_IM_A_TEAPOT => 'I\'m a teapot',
94 StatusCode::HTTP_MISDIRECTED_REQUEST => 'Misdirected Request',
95 StatusCode::HTTP_UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
96 StatusCode::HTTP_LOCKED => 'Locked',
97 StatusCode::HTTP_FAILED_DEPENDENCY => 'Failed Dependency',
98 StatusCode::HTTP_UPGRADE_REQUIRED => 'Upgrade Required',
99 StatusCode::HTTP_PRECONDITION_REQUIRED => 'Precondition Required',
100 StatusCode::HTTP_TOO_MANY_REQUESTS => 'Too Many Requests',
101 StatusCode::HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
102 StatusCode::HTTP_CONNECTION_CLOSED_WITHOUT_RESPONSE => 'Connection Closed Without Response',
103 StatusCode::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
104 StatusCode::HTTP_CLIENT_CLOSED_REQUEST => 'Client Closed Request',
105 //Server Error 5xx
106 StatusCode::HTTP_INTERNAL_SERVER_ERROR => 'Internal Server Error',
107 StatusCode::HTTP_NOT_IMPLEMENTED => 'Not Implemented',
108 StatusCode::HTTP_BAD_GATEWAY => 'Bad Gateway',
109 StatusCode::HTTP_SERVICE_UNAVAILABLE => 'Service Unavailable',
110 StatusCode::HTTP_GATEWAY_TIMEOUT => 'Gateway Timeout',
111 StatusCode::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
112 StatusCode::HTTP_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
113 StatusCode::HTTP_INSUFFICIENT_STORAGE => 'Insufficient Storage',
114 StatusCode::HTTP_LOOP_DETECTED => 'Loop Detected',
115 StatusCode::HTTP_NOT_EXTENDED => 'Not Extended',
116 StatusCode::HTTP_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
117 StatusCode::HTTP_NETWORK_CONNECTION_TIMEOUT_ERROR => 'Network Connect Timeout Error',
118 ];
119
125 const EOL = "\r\n";
126
134 public function __construct(
138 ) {
139 $this->status = $this->filterStatus($status);
140 $this->headers = $headers ? $headers : new Headers();
141 $this->body = $body ? $body : new Body(fopen('php://temp', 'r+'));
142 }
143
150 public function __clone()
151 {
152 $this->headers = clone $this->headers;
153 }
154
155 /*******************************************************************************
156 * Status
157 ******************************************************************************/
158
167 public function getStatusCode()
168 {
169 return $this->status;
170 }
171
192 public function withStatus($code, $reasonPhrase = '')
193 {
194 $code = $this->filterStatus($code);
195
196 if (!is_string($reasonPhrase) && !method_exists($reasonPhrase, '__toString')) {
197 throw new InvalidArgumentException('ReasonPhrase must be a string');
198 }
199
200 $clone = clone $this;
201 $clone->status = $code;
202 if ($reasonPhrase === '' && isset(static::$messages[$code])) {
204 }
205
206 if ($reasonPhrase === '') {
207 throw new InvalidArgumentException('ReasonPhrase must be supplied for this code');
208 }
209
210 $clone->reasonPhrase = $reasonPhrase;
211
212 return $clone;
213 }
214
222 protected function filterStatus($status)
223 {
224 if (!is_integer($status) ||
227 ) {
228 throw new InvalidArgumentException('Invalid HTTP status code');
229 }
230
231 return $status;
232 }
233
247 public function getReasonPhrase()
248 {
249 if ($this->reasonPhrase) {
250 return $this->reasonPhrase;
251 }
252 if (isset(static::$messages[$this->status])) {
254 }
255 return '';
256 }
257
258 /*******************************************************************************
259 * Headers
260 ******************************************************************************/
261
273 public function withHeader($name, $value)
274 {
275 $clone = clone $this;
276 $clone->headers->set($name, $value);
277
278 if ($clone->getStatusCode() === StatusCode::HTTP_OK && strtolower($name) === 'location') {
279 $clone = $clone->withStatus(StatusCode::HTTP_FOUND);
280 }
281
282 return $clone;
283 }
284
285
286 /*******************************************************************************
287 * Body
288 ******************************************************************************/
289
300 public function write($data)
301 {
302 $this->getBody()->write($data);
303
304 return $this;
305 }
306
307 /*******************************************************************************
308 * Response Helpers
309 ******************************************************************************/
310
323 public function withRedirect($url, $status = null)
324 {
325 $responseWithRedirect = $this->withHeader('Location', (string)$url);
326
327 if (is_null($status) && $this->getStatusCode() === StatusCode::HTTP_OK) {
329 }
330
331 if (!is_null($status)) {
332 return $responseWithRedirect->withStatus($status);
333 }
334
335 return $responseWithRedirect;
336 }
337
352 public function withJson($data, $status = null, $encodingOptions = 0)
353 {
354 $response = $this->withBody(new Body(fopen('php://temp', 'r+')));
355 $response->body->write($json = json_encode($data, $encodingOptions));
356
357 // Ensure that the json encoding passed successfully
358 if ($json === false) {
359 throw new \RuntimeException(json_last_error_msg(), json_last_error());
360 }
361
362 $responseWithJson = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
363 if (isset($status)) {
364 return $responseWithJson->withStatus($status);
365 }
366 return $responseWithJson;
367 }
368
376 public function isEmpty()
377 {
378 return in_array(
379 $this->getStatusCode(),
381 );
382 }
383
391 public function isInformational()
392 {
394 }
395
403 public function isOk()
404 {
405 return $this->getStatusCode() === StatusCode::HTTP_OK;
406 }
407
415 public function isSuccessful()
416 {
417 return $this->getStatusCode() >= StatusCode::HTTP_OK &&
419 }
420
428 public function isRedirect()
429 {
430 return in_array(
431 $this->getStatusCode(),
432 [
438 ]
439 );
440 }
441
449 public function isRedirection()
450 {
453 }
454
463 public function isForbidden()
464 {
465 return $this->getStatusCode() === StatusCode::HTTP_FORBIDDEN;
466 }
467
475 public function isNotFound()
476 {
477 return $this->getStatusCode() === StatusCode::HTTP_NOT_FOUND;
478 }
479
487 public function isClientError()
488 {
489 return $this->getStatusCode() >= StatusCode::HTTP_BAD_REQUEST &&
491 }
492
500 public function isServerError()
501 {
502 return $this->getStatusCode() >= StatusCode::HTTP_INTERNAL_SERVER_ERROR && $this->getStatusCode() < 600;
503 }
504
512 public function __toString()
513 {
514 $output = sprintf(
515 'HTTP/%s %s %s',
516 $this->getProtocolVersion(),
517 $this->getStatusCode(),
518 $this->getReasonPhrase()
519 );
521 foreach ($this->getHeaders() as $name => $values) {
522 $output .= sprintf('%s: %s', $name, $this->getHeaderLine($name)) . Response::EOL;
523 }
525 $output .= (string)$this->getBody();
526
527 return $output;
528 }
529}
An exception for terminatinating execution or to throw for unit testing.
Body.
Definition: Body.php:20
Abstract message (base class for Request and Response)
Definition: Message.php:26
getHeaderLine($name)
Retrieves a comma-separated string of the values for a single header.
Definition: Message.php:198
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: Message.php:80
getHeaders()
Retrieves all message header values.
Definition: Message.php:142
withBody(StreamInterface $body)
Return an instance with the specified message body.
Definition: Message.php:297
getBody()
Gets the body of the message.
Definition: Message.php:279
isClientError()
Is this response a client error?
Definition: Response.php:487
isEmpty()
Is this response empty?
Definition: Response.php:376
getReasonPhrase()
Gets the response reason phrase associated with the status code.
Definition: Response.php:247
isNotFound()
Is this response not Found?
Definition: Response.php:475
isSuccessful()
Is this response successful?
Definition: Response.php:415
withHeader($name, $value)
Return an instance with the provided value replacing the specified header.
Definition: Response.php:273
withJson($data, $status=null, $encodingOptions=0)
Json.
Definition: Response.php:352
__construct( $status=StatusCode::HTTP_OK, HeadersInterface $headers=null, StreamInterface $body=null)
Create new HTTP response.
Definition: Response.php:134
getStatusCode()
Gets the response status code.
Definition: Response.php:167
isInformational()
Is this response informational?
Definition: Response.php:391
__toString()
Convert response to string.
Definition: Response.php:512
isForbidden()
Is this response forbidden?
Definition: Response.php:463
isOk()
Is this response OK?
Definition: Response.php:403
withStatus($code, $reasonPhrase='')
Return an instance with the specified status code and, optionally, reason phrase.
Definition: Response.php:192
isRedirection()
Is this response a redirection?
Definition: Response.php:449
isRedirect()
Is this response a redirect?
Definition: Response.php:428
isServerError()
Is this response a server error?
Definition: Response.php:500
filterStatus($status)
Filter HTTP status code.
Definition: Response.php:222
withRedirect($url, $status=null)
Redirect.
Definition: Response.php:323
write($data)
Write data to the response body.
Definition: Response.php:300
__clone()
This method is applied to the cloned object after PHP performs an initial shallow-copy.
Definition: Response.php:150
const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
Definition: StatusCode.php:66
const HTTP_NETWORK_CONNECTION_TIMEOUT_ERROR
Definition: StatusCode.php:80
const HTTP_NONAUTHORITATIVE_INFORMATION
Definition: StatusCode.php:20
const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
Definition: StatusCode.php:64
const HTTP_REQUEST_URI_TOO_LONG
Definition: StatusCode.php:52
const HTTP_TOO_MANY_REQUESTS
Definition: StatusCode.php:63
const HTTP_EXPECTATION_FAILED
Definition: StatusCode.php:55
const HTTP_PRECONDITION_FAILED
Definition: StatusCode.php:50
const HTTP_PRECONDITION_REQUIRED
Definition: StatusCode.php:62
const HTTP_SWITCHING_PROTOCOLS
Definition: StatusCode.php:14
const HTTP_UNPROCESSABLE_ENTITY
Definition: StatusCode.php:58
const HTTP_PROXY_AUTHENTICATION_REQUIRED
Definition: StatusCode.php:45
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
Definition: StatusCode.php:54
const HTTP_PERMANENT_REDIRECT
Definition: StatusCode.php:36
const HTTP_MISDIRECTED_REQUEST
Definition: StatusCode.php:57
const HTTP_NETWORK_AUTHENTICATION_REQUIRED
Definition: StatusCode.php:79
const HTTP_CONNECTION_CLOSED_WITHOUT_RESPONSE
Definition: StatusCode.php:65
const HTTP_VERSION_NOT_SUPPORTED
Definition: StatusCode.php:74
const HTTP_MOVED_PERMANENTLY
Definition: StatusCode.php:29
const HTTP_INSUFFICIENT_STORAGE
Definition: StatusCode.php:76
const HTTP_UNSUPPORTED_MEDIA_TYPE
Definition: StatusCode.php:53
const HTTP_METHOD_NOT_ALLOWED
Definition: StatusCode.php:43
const HTTP_INTERNAL_SERVER_ERROR
Definition: StatusCode.php:69
const HTTP_SERVICE_UNAVAILABLE
Definition: StatusCode.php:72
const HTTP_VARIANT_ALSO_NEGOTIATES
Definition: StatusCode.php:75
const HTTP_TEMPORARY_REDIRECT
Definition: StatusCode.php:35
const HTTP_REQUEST_ENTITY_TOO_LARGE
Definition: StatusCode.php:51
const HTTP_FAILED_DEPENDENCY
Definition: StatusCode.php:60
const HTTP_CLIENT_CLOSED_REQUEST
Definition: StatusCode.php:67
$messages
Definition: en.php:5
$code
Definition: example_050.php:99
Representation of an outgoing, server-side response.
Describes a data stream.
Value object representing a URI.
Slim Framework (https://slimframework.com)
Definition: Body.php:9
$url
$response
$values
$data
Definition: bench.php:6