‪TYPO3CMS  ‪main
Response.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\StreamInterface;
23 
31 class ‪Response extends ‪Message implements ResponseInterface
32 {
36  protected int ‪$statusCode;
37 
41  protected string ‪$reasonPhrase = '';
42 
47  protected array ‪$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 
125  public function ‪__construct(‪$body = 'php://temp', int ‪$statusCode = 200, array ‪$headers = [], string ‪$reasonPhrase = '')
126  {
127  // Build a streamable object for the body
128  if (‪$body !== null && !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 !== null && !‪$body instanceof StreamInterface) {
133  ‪$body = new ‪Stream(‪$body, 'rw');
134  }
135  $this->body = ‪$body;
136 
137  if (‪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 = ‪$statusCode;
141 
142  $this->reasonPhrase = ‪$reasonPhrase === '' ? $this->availableStatusCodes[‪$this->statusCode] : ‪$reasonPhrase;
145  $this->headers = ‪$headers;
146  }
147 
156  public function ‪getStatusCode(): int
157  {
158  return ‪$this->statusCode;
159  }
160 
182  public function ‪withStatus(int $code, string ‪$reasonPhrase = ''): ResponseInterface
183  {
184  if (!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(): string
207  {
208  return ‪$this->reasonPhrase;
209  }
210 }
‪TYPO3\CMS\Core\Http\Response\getStatusCode
‪int getStatusCode()
Definition: Response.php:156
‪TYPO3\CMS\Core\Http\Response\withStatus
‪static withStatus(int $code, string $reasonPhrase='')
Definition: Response.php:182
‪TYPO3\CMS\Core\Http\Message\assertHeaders
‪assertHeaders(array $headers)
Definition: Message.php:319
‪TYPO3\CMS\Core\Http\Response\$statusCode
‪int $statusCode
Definition: Response.php:36
‪TYPO3\CMS\Core\Http\Response\$reasonPhrase
‪string $reasonPhrase
Definition: Response.php:41
‪TYPO3\CMS\Core\Http\Response\getReasonPhrase
‪string getReasonPhrase()
Definition: Response.php:206
‪TYPO3\CMS\Core\Http\Message
Definition: Message.php:32
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Core\Http\Response\$availableStatusCodes
‪array $availableStatusCodes
Definition: Response.php:47
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:53
‪TYPO3\CMS\Core\Http\Response\__construct
‪__construct($body='php://temp', int $statusCode=200, array $headers=[], string $reasonPhrase='')
Definition: Response.php:125
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Http\Message\$lowercasedHeaderNames
‪array $lowercasedHeaderNames
Definition: Message.php:48
‪TYPO3\CMS\Core\Http\Message\filterHeaders
‪array filterHeaders(array $originalHeaders)
Definition: Message.php:340
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:42
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18