|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package presentation |
|
4
|
|
|
* @subpackage responses |
|
5
|
|
|
* @author marius orcsik <[email protected]> |
|
6
|
|
|
* @date 09.08.30 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace vsc\presentation\responses; |
|
9
|
|
|
|
|
10
|
|
|
use vsc\domain\models\ErrorModel; |
|
11
|
|
|
use vsc\infrastructure\vsc; |
|
12
|
|
|
use vsc\infrastructure\Base; |
|
13
|
|
|
use vsc\infrastructure\BaseObject; |
|
14
|
|
|
use vsc\presentation\requests\HttpRequestTypes; |
|
15
|
|
|
use vsc\presentation\views\ViewA; |
|
16
|
|
|
|
|
17
|
|
|
abstract class HttpResponseA extends BaseObject { |
|
18
|
|
|
use HttpHeadersTrait; |
|
19
|
|
|
|
|
20
|
|
|
private $sServerProtocol; |
|
21
|
|
|
private $iStatus; |
|
22
|
|
|
private $aAllow = array(HttpRequestTypes::GET, HttpRequestTypes::POST, HttpRequestTypes::PUT, HttpRequestTypes::DELETE); |
|
23
|
|
|
private $sCacheControl; |
|
24
|
|
|
private $sContentEncoding; |
|
25
|
|
|
private $sContentLanguage; |
|
26
|
|
|
private $iContentLength; |
|
27
|
|
|
private $sContentLocation; |
|
28
|
|
|
private $sContentDisposition; |
|
29
|
|
|
private $sContentMd5; |
|
30
|
|
|
private $sDate; |
|
31
|
|
|
private $sETag; |
|
32
|
|
|
private $sExpires; |
|
33
|
|
|
private $sLastModified; |
|
34
|
|
|
private $sLocation; |
|
35
|
|
|
private $oView; |
|
36
|
|
|
|
|
37
|
|
|
protected $aHeaders = []; |
|
38
|
|
|
protected $sContentType; |
|
39
|
|
|
|
|
40
|
1 |
|
public function __construct() { |
|
41
|
1 |
|
if (is_array($_SERVER)) { |
|
42
|
1 |
|
if (array_key_exists('SERVER_PROTOCOL', $_SERVER)) { |
|
43
|
|
|
$this->sServerProtocol = $_SERVER['SERVER_PROTOCOL']; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
22 |
|
public function setStatus($iStatus) { |
|
49
|
22 |
|
if (!HttpResponseType::isValidStatus($iStatus)) { |
|
50
|
|
|
throw new ExceptionResponse('[' . $iStatus . '] is not a valid ' . $this->getServerProtocol() . ' status'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
22 |
|
$this->iStatus = $iStatus; |
|
54
|
22 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $sValue |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function setLocation($sValue) { |
|
61
|
1 |
|
$this->sLocation = $sValue; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function addHeader($sName, $sValue) { |
|
65
|
1 |
|
$this->aHeaders[$sName] = $sValue; |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $sValue |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function setCacheControl($sValue) { |
|
73
|
2 |
|
$this->sCacheControl = $sValue; |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $sValue |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function setContentEncoding($sValue) { |
|
81
|
2 |
|
$this->sContentEncoding = $sValue; |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $sValue |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function setContentLanguage($sValue) { |
|
89
|
2 |
|
$this->sContentLanguage = $sValue; |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param integer $iValue |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function setContentLength($iValue) { |
|
97
|
2 |
|
$this->iContentLength = $iValue; |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $sValue |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function setContentLocation($sValue) { |
|
105
|
2 |
|
$this->sContentLocation = $sValue; |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $sValue |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function setContentDisposition($sValue) { |
|
113
|
2 |
|
$this->sContentDisposition = $sValue; |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $sValue |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function setContentMd5($sValue) { |
|
121
|
2 |
|
$this->sContentMd5 = $sValue; |
|
122
|
2 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $sValue |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function setContentType($sValue) { |
|
129
|
2 |
|
$this->sContentType = $sValue; |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $sValue |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function setDate($sValue) { |
|
137
|
2 |
|
$this->sDate = $sValue; |
|
138
|
2 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $sValue |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function setETag($sValue) { |
|
145
|
1 |
|
$this->sETag = $sValue; |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $sValue |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function setExpires($sValue) { |
|
153
|
2 |
|
$this->sExpires = $sValue; |
|
154
|
2 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $sValue |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function setLastModified($sValue) { |
|
161
|
2 |
|
$this->sLastModified = $sValue; |
|
162
|
2 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function getLocation() { |
|
168
|
1 |
|
return $this->sLocation; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function getCacheControl() { |
|
175
|
1 |
|
return $this->sCacheControl; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function getContentEncoding() { |
|
182
|
1 |
|
return $this->sContentEncoding; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function getContentLanguage() { |
|
189
|
1 |
|
return $this->sContentLanguage; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return integer |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function getContentLength() { |
|
196
|
1 |
|
return $this->iContentLength; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
1 |
|
public function getContentLocation() { |
|
203
|
1 |
|
return $this->sContentLocation; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
1 |
|
public function getContentDisposition() { |
|
210
|
1 |
|
return $this->sContentDisposition; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
1 |
|
public function getContentMd5() { |
|
217
|
1 |
|
return $this->sContentMd5; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
1 |
|
public function getContentType() { |
|
224
|
1 |
|
return $this->sContentType; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return string |
|
229
|
|
|
*/ |
|
230
|
1 |
|
public function getDate() { |
|
231
|
1 |
|
return $this->sDate; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function getETag() { |
|
238
|
1 |
|
return $this->sETag; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
1 |
|
public function getExpires() { |
|
245
|
1 |
|
return $this->sExpires; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
1 |
|
public function getLastModified() { |
|
252
|
1 |
|
return $this->sLastModified; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return string |
|
257
|
|
|
*/ |
|
258
|
1 |
|
public function getServerProtocol() { |
|
259
|
1 |
|
return $this->sServerProtocol; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
2 |
|
protected function getCustomHeaders() { |
|
266
|
2 |
|
return $this->aHeaders; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param string $sProtocol |
|
271
|
|
|
* @param int $iStatus |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
1 |
|
public static function getHttpStatusString($sProtocol, $iStatus) { |
|
275
|
1 |
|
return $sProtocol . ' ' . HttpResponseType::getStatus($iStatus); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @return int |
|
280
|
|
|
*/ |
|
281
|
1 |
|
public function getStatus() { |
|
282
|
1 |
|
return $this->iStatus; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param ViewA $oView |
|
287
|
|
|
* @return string |
|
|
|
|
|
|
288
|
|
|
*/ |
|
289
|
1 |
|
public function setView(ViewA $oView) { |
|
290
|
1 |
|
$this->oView = $oView; |
|
291
|
1 |
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @returns ViewA |
|
295
|
|
|
*/ |
|
296
|
1 |
|
public function getView() { |
|
297
|
1 |
|
if (!ViewA::isValid($this->oView)) { |
|
298
|
1 |
|
$this->oView = new Base(); |
|
299
|
|
|
} |
|
300
|
1 |
|
return $this->oView; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @return string |
|
305
|
|
|
* @throws ExceptionResponse |
|
306
|
|
|
*/ |
|
307
|
|
|
public function getOutput() { |
|
308
|
|
|
$sResponseBody = null; |
|
309
|
|
|
$oView = null; |
|
310
|
|
|
if (ViewA::isValid($this->getView())) { |
|
311
|
|
|
$this->setContentType($this->getView()->getContentType()); |
|
|
|
|
|
|
312
|
|
|
} else { |
|
313
|
|
|
$this->setContentType('*/*'); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
$oRequest = vsc::getEnv()->getHttpRequest(); |
|
317
|
|
|
try { |
|
318
|
|
|
if (!$oRequest->isHead() && !$this->isRedirect()) { |
|
319
|
|
|
$oView = $this->getView(); |
|
320
|
|
|
$sResponseBody = $oView->getOutput(); |
|
|
|
|
|
|
321
|
|
|
} else { |
|
322
|
|
|
$this->setContentLength(0); |
|
323
|
|
|
} |
|
324
|
|
|
} catch (ExceptionResponseError $r) { |
|
325
|
|
|
$this->setStatus($r->getCode()); |
|
326
|
|
|
if (ViewA::isValid($oView)) { |
|
327
|
|
|
$oView->setModel(new ErrorModel($r)); |
|
|
|
|
|
|
328
|
|
|
$sResponseBody = $oView->getOutput(); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
return $sResponseBody; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
22 |
|
public function isSuccess() { |
|
335
|
22 |
|
return ($this->getStatus() == HttpResponseType::OK); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
22 |
|
public function isRedirect() { |
|
339
|
22 |
|
return ($this->getStatus() >= 300 && $this->getStatus() < 400); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
22 |
|
public function isUserError() { |
|
343
|
22 |
|
return ($this->getStatus() >= 400 && $this->getStatus() < 500); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
22 |
|
public function isServerError() { |
|
347
|
22 |
|
return ($this->getStatus() >= 500 && $this->getStatus() < 600); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
22 |
|
public function isError() { |
|
351
|
22 |
|
return ($this->isUserError() || $this->isServerError()); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.