1 | <?php |
||
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 | public function setStatusCode(int $value = null) |
||
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 | public function getStatusCode() |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Set headers. |
||
54 | * |
||
55 | * @param string $header type of header to set |
||
56 | * |
||
57 | * @return self |
||
58 | */ |
||
59 | public function addHeader($header) |
||
64 | |||
65 | |||
66 | |||
67 | /** |
||
68 | * Send headers. |
||
69 | * |
||
70 | * @return self |
||
71 | */ |
||
72 | public function sendHeaders() |
||
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * Set the body. |
||
93 | * |
||
94 | * @param callable|string $body either a string or a callable that |
||
95 | * can generate the body. |
||
96 | * |
||
97 | * @return self |
||
98 | */ |
||
99 | public function setBody($body) |
||
114 | |||
115 | |||
116 | |||
117 | /** |
||
118 | * Get the body. |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getBody() |
||
126 | |||
127 | |||
128 | |||
129 | /** |
||
130 | * Send response supporting several ways of receiving response $data. |
||
131 | * |
||
132 | * @param mixed $data to use as optional base for creating response. |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | public function send($data = null) |
||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * Send JSON response with an optional statuscode. |
||
170 | * |
||
171 | * @param mixed $data to be encoded as json. |
||
172 | * @param integer $statusCode optional statuscode to send. |
||
173 | * |
||
174 | * @return self |
||
175 | */ |
||
176 | public function sendJson($data, $statusCode = null) |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * Set body with JSON data. |
||
187 | * |
||
188 | * @param mixed $data to be encoded as json. |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | public function setJsonBody($data) |
||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * Redirect to another page. |
||
203 | * |
||
204 | * @param string $url to redirect to |
||
205 | * |
||
206 | * @return void |
||
207 | * |
||
208 | * @SuppressWarnings(PHPMD.ExitExpression) |
||
209 | */ |
||
210 | public function redirect($url) |
||
217 | } |
||
218 |