1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Response; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Handling a response. |
7
|
|
|
*/ |
8
|
|
|
class Response |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array $headers set all headers to send. |
12
|
|
|
* @var array $statusCode set statuscode to use. |
13
|
|
|
* @var array $body body to send with response. |
14
|
|
|
*/ |
15
|
|
|
private $headers = []; |
16
|
|
|
private $statusCode = null; |
17
|
|
|
private $body; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set status code to be sent as part of headers. |
23
|
|
|
* |
24
|
|
|
* @param int $value of response status code |
25
|
|
|
* |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
14 |
|
public function setStatusCode(int $value = null) |
29
|
|
|
{ |
30
|
14 |
|
if (is_null($value)) { |
31
|
6 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
$this->statusCode = $value; |
35
|
8 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get status code to be sent as part of headers. |
42
|
|
|
* |
43
|
|
|
* @return integer value as status code or null if not set. |
44
|
|
|
*/ |
45
|
7 |
|
public function getStatusCode() |
46
|
|
|
{ |
47
|
7 |
|
return $this->statusCode; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set headers. |
54
|
|
|
* |
55
|
|
|
* @param string $header type of header to set |
56
|
|
|
* |
57
|
|
|
* @return self |
58
|
|
|
*/ |
59
|
7 |
|
public function addHeader($header) |
60
|
|
|
{ |
61
|
7 |
|
$this->headers[] = $header; |
62
|
7 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all headers. |
69
|
|
|
* |
70
|
|
|
* @return array with headers |
71
|
|
|
*/ |
72
|
1 |
|
public function getHeaders() : array |
73
|
|
|
{ |
74
|
1 |
|
return $this->headers; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Send headers. |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
1 |
|
public function sendHeaders() |
85
|
|
|
{ |
86
|
1 |
|
if (php_sapi_name() !== "cli" && headers_sent($file, $line)) { |
87
|
|
|
throw new Exception("Try to send headers but headers already sent, output started at $file line $line."); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
http_response_code($this->statusCode); |
91
|
|
|
|
92
|
1 |
|
foreach ($this->headers as $header) { |
93
|
1 |
|
if (php_sapi_name() !== "cli") { |
94
|
1 |
|
header($header); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the body. |
105
|
|
|
* |
106
|
|
|
* @param callable|string $body either a string or a callable that |
107
|
|
|
* can generate the body. |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
*/ |
111
|
8 |
|
public function setBody($body) |
112
|
|
|
{ |
113
|
8 |
|
if (is_string($body)) { |
114
|
7 |
|
$this->body = $body; |
115
|
3 |
|
} elseif (is_array($body)) { |
116
|
2 |
|
$this->setJsonBody($body); |
117
|
1 |
|
} elseif (is_callable($body)) { |
118
|
1 |
|
ob_start(); |
119
|
1 |
|
$res1 = call_user_func($body); |
120
|
1 |
|
$res2 = ob_get_contents(); |
121
|
1 |
|
$this->body = $res2 . $res1; |
122
|
1 |
|
ob_end_clean(); |
123
|
|
|
} |
124
|
8 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the body. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
9 |
|
public function getBody() |
135
|
|
|
{ |
136
|
9 |
|
return $this->body; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Send response supporting several ways of receiving response $data. |
143
|
|
|
* |
144
|
|
|
* @param mixed $data to use as optional base for creating response. |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
6 |
|
public function send($data = null) |
149
|
|
|
{ |
150
|
6 |
|
$statusCode = null; |
151
|
|
|
|
152
|
6 |
|
if ($data instanceof self) { |
153
|
1 |
|
return $data->send(); |
154
|
|
|
} |
155
|
|
|
|
156
|
6 |
|
if (is_string($data)) { |
157
|
1 |
|
$this->setBody($data); |
158
|
|
|
} |
159
|
|
|
|
160
|
6 |
|
if (is_array($data) && isset($data[0])) { |
161
|
2 |
|
$this->setBody($data[0]); |
162
|
|
|
} |
163
|
|
|
|
164
|
6 |
|
if (is_array($data) && isset($data[1]) && is_numeric($data[1])) { |
165
|
1 |
|
$statusCode = $data[1]; |
166
|
|
|
} |
167
|
|
|
|
168
|
6 |
|
$this->setStatusCode($statusCode); |
169
|
|
|
|
170
|
6 |
|
if (!headers_sent()) { |
171
|
|
|
$this->sendHeaders(); |
172
|
|
|
} |
173
|
|
|
|
174
|
6 |
|
echo $this->getBody(); |
175
|
6 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Send JSON response with an optional statuscode. |
182
|
|
|
* |
183
|
|
|
* @param mixed $data to be encoded as json. |
184
|
|
|
* @param integer $statusCode optional statuscode to send. |
185
|
|
|
* |
186
|
|
|
* @return self |
187
|
|
|
*/ |
188
|
1 |
|
public function sendJson($data, $statusCode = null) |
189
|
|
|
{ |
190
|
1 |
|
return $this->setStatusCode($statusCode) |
191
|
1 |
|
->setJsonBody($data) |
192
|
1 |
|
->send(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set body with JSON data. |
199
|
|
|
* |
200
|
|
|
* @param mixed $data to be encoded as json. |
201
|
|
|
* |
202
|
|
|
* @return self |
203
|
|
|
*/ |
204
|
3 |
|
public function setJsonBody($data) |
205
|
|
|
{ |
206
|
3 |
|
$this->addHeader("Content-Type: application/json; charset=utf8"); |
207
|
3 |
|
$this->setBody(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
208
|
3 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Redirect to another page. |
215
|
|
|
* |
216
|
|
|
* @param string $url to redirect to |
217
|
|
|
* |
218
|
|
|
* @return self |
219
|
|
|
*/ |
220
|
2 |
|
public function redirect(string $url) : object |
221
|
|
|
{ |
222
|
2 |
|
$this->addHeader("Location: " . $url); |
223
|
2 |
|
$this->body = null; |
224
|
2 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|