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