]>
jfr.im git - uguu.git/blob - static/php/classes/Response.class.php
3 * The Response class is a do-it-all for getting responses out in different
6 * @todo Create sub-classes to split and extend this god object.
11 * Indicates response type used for routing.
13 * Valid strings are 'csv', 'html', 'json' and 'text'.
15 * @var string Response type
20 * Indicates requested response type.
22 * Valid strings are 'csv', 'html', 'json', 'gyazo' and 'text'.
24 * @param string|null $response_type Response type
26 public function __construct($response_type = null)
28 switch ($response_type) {
30 header('Content-Type: text/csv; charset=UTF-8');
31 $this->type
= $response_type;
34 header('Content-Type: text/html; charset=UTF-8');
35 $this->type
= $response_type;
38 header('Content-Type: application/json; charset=UTF-8');
39 $this->type
= $response_type;
42 header('Content-Type: text/plain; charset=UTF-8');
46 header('Content-Type: text/plain; charset=UTF-8');
47 $this->type
= $response_type;
50 header('Content-Type: application/json; charset=UTF-8');
52 $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.');
58 * Routes error messages depending on response type.
60 * @param int $code HTTP status code number
61 * @param int $desc descriptive error message
65 public function error($code, $desc)
69 switch ($this->type
) {
71 $response = $this->csvError($desc);
74 $response = $this->htmlError($code, $desc);
77 $response = $this->jsonError($code, $desc);
80 $response = $this->textError($code, $desc);
84 //http_response_code(500); // "500 Internal Server Error"
89 * Routes success messages depending on response type.
91 * @param mixed[] $files
95 public function send($files)
99 switch ($this->type
) {
101 $response = $this->csvSuccess($files);
104 $response = $this->htmlSuccess($files);
107 $response = $this->jsonSuccess($files);
110 $response = $this->textSuccess($files);
114 http_response_code(200); // "200 OK". Success.
119 * Indicates with CSV body the request was invalid.
121 * @deprecated 2.1.0 Will be renamed to camelCase format.
123 * @param int $description descriptive error message
125 * @return string error message in CSV format
127 private static function csvError($description)
129 return '"error"'."\r\n"."\"$description\""."\r\n";
133 * Indicates with CSV body the request was successful.
135 * @deprecated 2.1.0 Will be renamed to camelCase format.
137 * @param mixed[] $files
139 * @return string success message in CSV format
141 private static function csvSuccess($files)
143 $result = '"name","url","hash","size"'."\r\n";
144 foreach ($files as $file) {
145 $result .= '"'.$file['name'].'"'.','.
146 '"'.$file['url'].'"'.','.
147 '"'.$file['hash'].'"'.','.
148 '"'.$file['size'].'"'."\r\n";
155 * Indicates with HTML body the request was invalid.
157 * @deprecated 2.1.0 Will be renamed to camelCase format.
159 * @param int $code HTTP status code number
160 * @param int $description descriptive error message
162 * @return string error message in HTML format
164 private static function htmlError($code, $description)
166 return '<p>ERROR: ('.$code.') '.$description.'</p>';
170 * Indicates with HTML body the request was successful.
172 * @deprecated 2.1.0 Will be renamed to camelCase format.
174 * @param mixed[] $files
176 * @return string success message in HTML format
178 private static function htmlSuccess($files)
182 foreach ($files as $file) {
183 $result .= '<a href="'.$file['url'].'">'.$file['url'].'</a><br>';
190 * Indicates with JSON body the request was invalid.
192 * @deprecated 2.1.0 Will be renamed to camelCase format.
194 * @param int $code HTTP status code number
195 * @param int $description descriptive error message
197 * @return string error message in pretty-printed JSON format
199 private static function jsonError($code, $description)
203 'errorcode' => $code,
204 'description' => $description,
205 ], JSON_PRETTY_PRINT
);
209 * Indicates with JSON body the request was successful.
211 * @deprecated 2.1.0 Will be renamed to camelCase format.
213 * @param mixed[] $files
215 * @return string success message in pretty-printed JSON format
217 private static function jsonSuccess($files)
222 ], JSON_PRETTY_PRINT
);
226 * Indicates with plain text body the request was invalid.
228 * @deprecated 2.1.0 Will be renamed to camelCase format.
230 * @param int $code HTTP status code number
231 * @param int $description descriptive error message
233 * @return string error message in plain text format
235 private static function textError($code, $description)
237 return 'ERROR: ('.$code.') '.$description;
241 * Indicates with plain text body the request was successful.
243 * @deprecated 2.1.0 Will be renamed to camelCase format.
245 * @param mixed[] $files
247 * @return string success message in plain text format
249 private static function textSuccess($files)
253 foreach ($files as $file) {
254 $result .= $file['url']."\n";