-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.php
313 lines (275 loc) · 6.95 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* DframeFramework
* Copyright (c) Sławomir Kaleta.
*
* @license https://github.com/dframe/dframe/blob/master/LICENCE (MIT)
*/
namespace Dframe\Router;
/**
* Short Description.
*
* @author Sławomir Kaleta <slaszka@gmail.com>
*/
class Response extends Router
{
/**
* @var array
*/
public static array $code = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
];
/**
* @var int
*/
public int $status = 200;
/**
* @var null|string
*/
protected ?string $body = null;
/**
* @var array
*/
protected array $headers = [];
/**
* Response constructor.
*
* @param mixed $body
*/
public function __construct($body = null)
{
if (isset($body)) {
$this->body = $body;
}
return $this;
}
/**
* @param mixed $body
*
* @return Response
*/
public static function create($body = null): self
{
return new self($body);
}
/**
* @param mixed $body
*
* @return Response
*/
public static function render($body = null): self
{
return new self($body);
}
/**
* @param mixed $body
* @param null|int $status
*
* @return Response
*/
public static function renderJSON($body = null, $status = null): self
{
$body = json_encode($body);
$Response = new self($body);
if (isset($status)) {
$Response->status($status);
}
$Response->headers(['Content-Type' => 'application/json']);
return $Response;
}
/**
* @param $code
*
* @return self
*/
public function status($code): self
{
$this->status = $code;
return $this;
}
/**
* @param array $headers
*
* @return self
*/
public function headers($headers = []): self
{
if (!empty($headers)) {
$this->headers = array_unique(array_merge($this->headers, $headers));
}
return $this;
}
/**
* @param mixed $body
* @param null|int $status
*
* @return Response
*/
public static function renderJSONP($body = null, $status = null): Response
{
$callback = null;
if (isset($_GET['callback'])) {
$callback = $_GET['callback'];
}
$Response = new self($callback . '(' . json_encode($body) . ')');
if (isset($status)) {
$Response->status($status);
}
$Response->headers(['Content-Type' => 'application/jsonp']);
return $Response;
}
/**
* Address redirection.
*
* @param string $url
* @param int $status
* @param array $headers
*
* @return Response
*/
public static function redirect($url = '', $status = 301, $headers = []): Response
{
$Response = new Response();
$Response->status($status);
if (!empty($headers)) {
$Response->headers($headers);
}
$Response->headers(
[
'Location' => ((new Router())->boot())->makeUrl($url),
]
);
return $Response;
}
/**
* @param $json
*
* @return self
*/
public function json($json): self
{
$this->headers(['Content-Type' => 'application/json']);
$this->body = json_encode($json);
return $this;
}
/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}
/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* @param mixed $body
*
* @return self
*/
public function body($body = null): self
{
$this->body = $body;
return $this;
}
/**
* Display string and return int
*
* @return null|string
*/
public function display(): ?string
{
if (!headers_sent()) {
if (PHP_SAPI !== 'cli') {
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1');
$status = (!empty($this->status) ? $this->status : 200);
$string = sprintf('%s %d %s', $protocol, $status, self::$code[$status]);
header($string, true, $status); // Default header
if (!empty($this->headers)) {
foreach ($this->headers as $field => $value) {
if (is_array($value)) {
foreach ($value as $v) {
header("$field" . ': ' . $v, false);
}
} else {
header("$field" . ': ' . $value);
}
}
}
}
}
return print $this->getBody() ? $this->getBody() : null;
}
/**
* @return null|string
*/
public function getBody(): ?string
{
return $this->body;
}
/**
* @return null|string
*/
public function __toString(): string
{
return $this->body ?? "";
}
}