1
|
|
|
<?php |
2
|
|
|
namespace Nkey\Caribu\Mvc\Controller; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* The response is encapsulated in this class |
6
|
|
|
* |
7
|
|
|
* @author Maik Greubel <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This file is part of Caribu MVC package |
10
|
|
|
*/ |
11
|
|
|
class Response |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Response code |
16
|
|
|
* |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $code = 200; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Page title |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $title = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Response content |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $body = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Response content type |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $type = 'text/html'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Response encoding |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $encoding = 'utf-8'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Additional headers to be send to client |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $additionalHeaders = array(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieve the response body as string |
58
|
|
|
* |
59
|
|
|
* @return string The response content |
60
|
|
|
*/ |
61
|
20 |
|
public function __toString() |
62
|
|
|
{ |
63
|
20 |
|
return $this->body; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrieve the response code |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
13 |
|
public function getCode() |
72
|
|
|
{ |
73
|
13 |
|
return $this->code; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the response code |
78
|
|
|
* |
79
|
|
|
* @param int $code |
80
|
|
|
* |
81
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Response The response |
82
|
|
|
*/ |
83
|
5 |
|
public function setCode($code) |
84
|
|
|
{ |
85
|
5 |
|
$this->code = $code; |
86
|
5 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieve response body |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
20 |
|
public function getBody() |
95
|
|
|
{ |
96
|
20 |
|
return $this->body; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the response body content |
101
|
|
|
* |
102
|
|
|
* @param string $body |
103
|
|
|
* The content body |
104
|
|
|
* |
105
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Response The response |
106
|
|
|
*/ |
107
|
20 |
|
public function setBody($body) |
108
|
|
|
{ |
109
|
20 |
|
$this->body = $body; |
110
|
20 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Append some content to existing body |
115
|
|
|
* |
116
|
|
|
* @param string $body |
117
|
|
|
* The content body to append to existing content |
118
|
|
|
* |
119
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Response |
120
|
|
|
*/ |
121
|
20 |
|
public function appendBody($body) |
122
|
|
|
{ |
123
|
20 |
|
return $this->setBody(sprintf("%s%s", $this->getBody(), $body)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the reponse as http status code string |
128
|
|
|
* |
129
|
|
|
* @return string The status code |
130
|
|
|
*/ |
131
|
20 |
|
public function getHttpCode() |
132
|
|
|
{ |
133
|
20 |
|
return sprintf("HTTP/%s", strval(new \Generics\Client\HttpStatus($this->code))); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieve the content type |
138
|
|
|
* |
139
|
|
|
* @return string The mimetype of response |
140
|
|
|
*/ |
141
|
20 |
|
public function getType() |
142
|
|
|
{ |
143
|
20 |
|
return $this->type; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the response type |
148
|
|
|
* |
149
|
|
|
* @param string $type |
150
|
|
|
* The mimetype of response |
151
|
|
|
* |
152
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Response The response |
153
|
|
|
*/ |
154
|
2 |
|
public function setType($type) |
155
|
|
|
{ |
156
|
2 |
|
$this->type = $type; |
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the page title |
162
|
|
|
* |
163
|
|
|
* @param string $title |
164
|
|
|
* The page title |
165
|
|
|
* |
166
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Response The response |
167
|
|
|
*/ |
168
|
29 |
|
public function setTitle($title) |
169
|
|
|
{ |
170
|
29 |
|
$this->title = $title; |
171
|
29 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Retrieve page title |
176
|
|
|
* |
177
|
|
|
* @return string The page title |
178
|
|
|
*/ |
179
|
18 |
|
public function getTitle() |
180
|
|
|
{ |
181
|
18 |
|
return $this->title; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Append an additional header to list |
186
|
|
|
* |
187
|
|
|
* @param string $headerName The name of the header |
188
|
|
|
* @param string $headerValue The value of the header |
189
|
|
|
* |
190
|
|
|
* @return Response The current response |
191
|
|
|
*/ |
192
|
|
|
public function addHeader($headerName, $headerValue) |
193
|
|
|
{ |
194
|
|
|
$this->additionalHeaders[$headerName] = $headerValue; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Retrieve the list of additional headers |
200
|
|
|
* |
201
|
|
|
* @return array The list of headers |
202
|
|
|
*/ |
203
|
|
|
public function getAdditionalHeaders() |
204
|
|
|
{ |
205
|
|
|
return $this->additionalHeaders; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the response content encoding |
210
|
|
|
* |
211
|
|
|
* @param string $encoding |
212
|
|
|
* |
213
|
|
|
* @return Response The current response |
214
|
|
|
*/ |
215
|
|
|
public function setEncoding($encoding) |
216
|
|
|
{ |
217
|
|
|
$this->encoding = $encoding; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Retrieve the content encoding |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
20 |
|
public function getEncoding() |
227
|
|
|
{ |
228
|
20 |
|
return $this->encoding; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|