‪TYPO3CMS  9.5
Response.php
Go to the documentation of this file.
1 <?php
2 namespace ‪TYPO3\CMS\Core\Http;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\StreamInterface;
19 
27 class ‪Response extends ‪Message implements ResponseInterface
28 {
33  protected ‪$statusCode;
34 
39  protected ‪$reasonPhrase = '';
40 
45  protected ‪$availableStatusCodes = [
46  // INFORMATIONAL CODES
47  100 => 'Continue',
48  101 => 'Switching Protocols',
49  102 => 'Processing',
50  103 => 'Early Hints',
51  // SUCCESS CODES
52  200 => 'OK',
53  201 => 'Created',
54  202 => 'Accepted',
55  203 => 'Non-Authoritative Information',
56  204 => 'No Content',
57  205 => 'Reset Content',
58  206 => 'Partial Content',
59  207 => 'Multi-Status',
60  208 => 'Already Reported',
61  226 => 'IM Used',
62  // REDIRECTION CODES
63  300 => 'Multiple Choices',
64  301 => 'Moved Permanently',
65  302 => 'Found',
66  303 => 'See Other',
67  304 => 'Not Modified',
68  305 => 'Use Proxy',
69  306 => 'Switch Proxy', // Deprecated
70  307 => 'Temporary Redirect',
71  308 => 'Permanent Redirect',
72  // CLIENT ERROR
73  400 => 'Bad Request',
74  401 => 'Unauthorized',
75  402 => 'Payment Required',
76  403 => 'Forbidden',
77  404 => 'Not Found',
78  405 => 'Method Not Allowed',
79  406 => 'Not Acceptable',
80  407 => 'Proxy Authentication Required',
81  408 => 'Request Timeout',
82  409 => 'Conflict',
83  410 => 'Gone',
84  411 => 'Length Required',
85  412 => 'Precondition Failed',
86  413 => 'Payload Too Large',
87  414 => 'URI Too Long',
88  415 => 'Unsupported Media Type',
89  416 => 'Range Not Satisfiable',
90  417 => 'Expectation Failed',
91  418 => 'I\'m a teapot',
92  421 => 'Misdirected Request',
93  422 => 'Unprocessable Entity',
94  423 => 'Locked',
95  424 => 'Failed Dependency',
96  425 => 'Unordered Collection',
97  426 => 'Upgrade Required',
98  428 => 'Precondition Required',
99  429 => 'Too Many Requests',
100  431 => 'Request Header Fields Too Large',
101  451 => 'Unavailable For Legal Reasons',
102  // SERVER ERROR
103  500 => 'Internal Server Error',
104  501 => 'Not Implemented',
105  502 => 'Bad Gateway',
106  503 => 'Service Unavailable',
107  504 => 'Gateway Timeout',
108  505 => 'HTTP Version Not Supported',
109  506 => 'Variant Also Negotiates',
110  507 => 'Insufficient Storage',
111  508 => 'Loop Detected',
112  509 => 'Bandwidth Limit Exceeded',
113  510 => 'Not Extended',
114  511 => 'Network Authentication Required'
115  ];
116 
125  public function ‪__construct(‪$body = 'php://temp', ‪$statusCode = 200, ‪$headers = [])
126  {
127  // Build a streamable object for the body
128  if (!is_string(‪$body) && !is_resource(‪$body) && !‪$body instanceof StreamInterface) {
129  throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717277);
130  }
131 
132  if (!‪$body instanceof StreamInterface) {
133  ‪$body = new ‪Stream(‪$body, 'rw');
134  }
135  $this->body = ‪$body;
136 
137  if (\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger(‪$statusCode) === false || !array_key_exists((int)‪$statusCode, $this->availableStatusCodes)) {
138  throw new \InvalidArgumentException('The given status code is not a valid HTTP status code.', 1436717278);
139  }
140  $this->statusCode = (int)‪$statusCode;
141 
142  $this->reasonPhrase = $this->availableStatusCodes[‪$this->statusCode];
143  list($this->lowercasedHeaderNames, ‪$headers) = $this->‪filterHeaders(‪$headers);
145  $this->headers = ‪$headers;
146  }
147 
156  public function ‪getStatusCode()
157  {
158  return ‪$this->statusCode;
159  }
160 
182  public function ‪withStatus($code, ‪$reasonPhrase = '')
183  {
184  if (\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($code) === false || !array_key_exists((int)$code, $this->availableStatusCodes)) {
185  throw new \InvalidArgumentException('The given status code is not a valid HTTP status code', 1436717279);
186  }
187  $clonedObject = clone $this;
188  $clonedObject->statusCode = (int)$code;
189  $clonedObject->reasonPhrase = ‪$reasonPhrase !== '' ? ‪$reasonPhrase : $this->availableStatusCodes[$code];
190  return $clonedObject;
191  }
192 
206  public function ‪getReasonPhrase()
207  {
208  return ‪$this->reasonPhrase;
209  }
210 }
‪TYPO3\CMS\Core\Http\Response\getStatusCode
‪int getStatusCode()
Definition: Response.php:153
‪TYPO3\CMS\Core\Http\Response\withStatus
‪static withStatus($code, $reasonPhrase='')
Definition: Response.php:179
‪TYPO3
‪TYPO3\CMS\Core\Http\Message\assertHeaders
‪assertHeaders(array $headers)
Definition: Message.php:313
‪TYPO3\CMS\Core\Http\Response\$statusCode
‪int $statusCode
Definition: Response.php:32
‪TYPO3\CMS\Core\Http\Response\$reasonPhrase
‪string $reasonPhrase
Definition: Response.php:37
‪TYPO3\CMS\Core\Http\Response\getReasonPhrase
‪string getReasonPhrase()
Definition: Response.php:203
‪TYPO3\CMS\Core\Http\Message
Definition: Message.php:29
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Core\Http\Response\$availableStatusCodes
‪array $availableStatusCodes
Definition: Response.php:42
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:28
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:50
‪TYPO3\CMS\Core\Http\Response\__construct
‪__construct($body='php://temp', $statusCode=200, $headers=[])
Definition: Response.php:122
‪TYPO3\CMS\Core\Http\Message\filterHeaders
‪array filterHeaders(array $originalHeaders)
Definition: Message.php:334
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:39
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3