|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Response; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Handling a response. |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class Response |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Properties |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
private $headers = []; // Set all headers to send |
|
16
|
|
|
private $statusCode; // Set statuscode to use |
|
17
|
|
|
private $body; // Body to send with response |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set headers. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $header type of header to set |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function setStatusCode($value) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$supportedValues = [200, 403, 404, 500]; |
|
31
|
1 |
|
if (!in_array($value, $supportedValues)) { |
|
32
|
1 |
|
throw new Exception("Unsupported statuscode: $value"); |
|
33
|
|
|
} |
|
34
|
|
|
$this->statusCode = $value; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set headers. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $header type of header to set |
|
43
|
|
|
* |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addHeader($header) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->headers[] = $header; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check if headers are already sent and throw exception if it is. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
* |
|
58
|
|
|
* @throws Exception |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function checkIfHeadersAlreadySent() |
|
61
|
|
|
{ |
|
62
|
1 |
|
if (headers_sent($file, $line)) { |
|
63
|
1 |
|
throw new Exception("Try to send headers but headers already sent, output started at $file line $line."); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Send headers. |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function sendHeaders() |
|
75
|
|
|
{ |
|
76
|
|
|
$statusHeader = [ |
|
77
|
|
|
"200" => "HTTP/1.1 200 OK", |
|
78
|
|
|
"403" => "HTTP/1.1 403 Forbidden", |
|
79
|
|
|
"404" => "HTTP/1.1 404 Not Found", |
|
80
|
|
|
"500" => "HTTP/1.1 500 Internal Server Error" |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$this->checkIfHeadersAlreadySent(); |
|
84
|
|
|
|
|
85
|
|
|
if (isset($statusHeader[$this->statusCode])) { |
|
86
|
|
|
header($statusHeader[$this->statusCode]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach ($this->headers as $header) { |
|
90
|
|
|
header($header); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set the body. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $body |
|
102
|
|
|
* |
|
103
|
|
|
* @return this |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function setBody($body) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->body = $body; |
|
108
|
1 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the body. |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getBody() |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->body; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Send response with an optional statuscode. |
|
127
|
|
|
* |
|
128
|
|
|
* @param integer $statusCode optional statuscode to send. |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function send($statusCode = null) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($statusCode) { |
|
|
|
|
|
|
135
|
|
|
$this->setStatusCode($statusCode); |
|
136
|
|
|
} |
|
137
|
|
|
$this->sendHeaders(); |
|
138
|
|
|
echo $this->getBody(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Send JSON response with an optional statuscode. |
|
145
|
|
|
* |
|
146
|
|
|
* @param mixed $data to be encoded as json. |
|
147
|
|
|
* @param integer $statusCode optional statuscode to send. |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function sendJson($data, $statusCode = null) |
|
152
|
|
|
{ |
|
153
|
|
|
if ($statusCode) { |
|
|
|
|
|
|
154
|
|
|
$this->setStatusCode($statusCode); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->addHeader("Content-Type: application/json; charset=utf8"); |
|
158
|
|
|
$this->sendHeaders(); |
|
159
|
|
|
echo json_encode($data, JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Redirect to another page. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $url to redirect to |
|
168
|
|
|
* |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function redirect($url) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->checkIfHeadersAlreadySent(); |
|
174
|
|
|
header("Location: " . $url); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.