]>
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 $type 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.
64 public function error($code, $desc)
68 switch ($this->type
) {
70 $response = $this->csvError($desc);
73 $response = $this->htmlError($code, $desc);
76 $response = $this->jsonError($code, $desc);
79 $response = $this->textError($code, $desc);
83 http_response_code(500); // "500 Internal Server Error"
88 * Routes success messages depending on response type.
90 * @param mixed[] $files
93 public function send($files)
97 switch ($this->type
) {
99 $response = $this->csvSuccess($files);
102 $response = $this->htmlSuccess($files);
105 $response = $this->jsonSuccess($files);
108 $response = $this->textSuccess($files);
112 http_response_code(200); // "200 OK". Success.
117 * Indicates with CSV body the request was invalid.
119 * @deprecated 2.1.0 Will be renamed to camelCase format.
120 * @param int $description Descriptive error message.
121 * @return string Error message in CSV format.
123 private static function csvError($description)
125 return '"error"'."\r\n"."\"$description\""."\r\n";
129 * Indicates with CSV body the request was successful.
131 * @deprecated 2.1.0 Will be renamed to camelCase format.
132 * @param mixed[] $files
133 * @return string Success message in CSV format.
135 private static function csvSuccess($files)
137 $result = '"name","url","hash","size"'."\r\n";
138 foreach ($files as $file) {
139 $result .= '"'.$file['name'].'"'.','.
140 '"'.$file['url'].'"'.','.
141 '"'.$file['hash'].'"'.','.
142 '"'.$file['size'].'"'."\r\n";
149 * Indicates with HTML body the request was invalid.
151 * @deprecated 2.1.0 Will be renamed to camelCase format.
152 * @param int $code HTTP status code number.
153 * @param int $description Descriptive error message.
154 * @return string Error message in HTML format.
156 private static function htmlError($code, $description)
158 return '<p>ERROR: ('.$code.') '.$description.'</p>';
162 * Indicates with HTML body the request was successful.
164 * @deprecated 2.1.0 Will be renamed to camelCase format.
165 * @param mixed[] $files
166 * @return string Success message in HTML format.
168 private static function htmlSuccess($files)
172 foreach ($files as $file) {
173 $result .= '<a href="'.$file['url'].'">'.$file['url'].'</a><br>';
180 * Indicates with JSON body the request was invalid.
182 * @deprecated 2.1.0 Will be renamed to camelCase format.
183 * @param int $code HTTP status code number.
184 * @param int $description Descriptive error message.
185 * @return string Error message in pretty-printed JSON format.
187 private static function jsonError($code, $description)
189 return json_encode(array(
191 'errorcode' => $code,
192 'description' => $description,
193 ), JSON_PRETTY_PRINT
);
197 * Indicates with JSON body the request was successful.
199 * @deprecated 2.1.0 Will be renamed to camelCase format.
200 * @param mixed[] $files
201 * @return string Success message in pretty-printed JSON format.
203 private static function jsonSuccess($files)
205 return json_encode(array(
208 ), JSON_PRETTY_PRINT
);
212 * Indicates with plain text body the request was invalid.
214 * @deprecated 2.1.0 Will be renamed to camelCase format.
215 * @param int $code HTTP status code number.
216 * @param int $description Descriptive error message.
217 * @return string Error message in plain text format.
219 private static function textError($code, $description)
221 return 'ERROR: ('.$code.') '.$description;
225 * Indicates with plain text body the request was successful.
227 * @deprecated 2.1.0 Will be renamed to camelCase format.
228 * @param mixed[] $files
229 * @return string Success message in plain text format.
231 private static function textSuccess($files)
235 foreach ($files as $file) {
236 $result .= $file['url']."\n";