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 | 14 | 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 | 7 | 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 | 7 | public function addHeader($header) |
|
64 | |||
65 | |||
66 | |||
67 | /** |
||
68 | * Get all headers. |
||
69 | * |
||
70 | * @return array with headers |
||
71 | */ |
||
72 | 1 | public function getHeaders() : array |
|
76 | |||
77 | |||
78 | |||
79 | /** |
||
80 | * Send headers. |
||
81 | * |
||
82 | * @return self |
||
83 | */ |
||
84 | 1 | public function sendHeaders() |
|
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) |
|
126 | |||
127 | |||
128 | |||
129 | /** |
||
130 | * Get the body. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 9 | public function getBody() |
|
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) |
|
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) |
|
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) |
|
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 |
|
226 | } |
||
227 |