]> jfr.im git - uguu.git/blame - static/php/classes/Response.class.php
changes
[uguu.git] / static / php / classes / Response.class.php
CommitLineData
d8c46ff7
GJ
1<?php
2/**
3 * The Response class is a do-it-all for getting responses out in different
4 * formats.
5 *
6 * @todo Create sub-classes to split and extend this god object.
7 */
8class Response
9{
10 /**
11 * Indicates response type used for routing.
12 *
13 * Valid strings are 'csv', 'html', 'json' and 'text'.
14 *
95b5e1a7 15 * @var string Response type
d8c46ff7
GJ
16 */
17 private $type;
18
19 /**
20 * Indicates requested response type.
21 *
22 * Valid strings are 'csv', 'html', 'json', 'gyazo' and 'text'.
23 *
24 * @param string|null $response_type Response type
25 */
26 public function __construct($response_type = null)
27 {
28 switch ($response_type) {
29 case 'csv':
30 header('Content-Type: text/csv; charset=UTF-8');
31 $this->type = $response_type;
32 break;
33 case 'html':
34 header('Content-Type: text/html; charset=UTF-8');
35 $this->type = $response_type;
36 break;
37 case 'json':
38 header('Content-Type: application/json; charset=UTF-8');
39 $this->type = $response_type;
40 break;
41 case 'gyazo':
42 header('Content-Type: text/plain; charset=UTF-8');
43 $this->type = 'text';
44 break;
45 case 'text':
46 header('Content-Type: text/plain; charset=UTF-8');
47 $this->type = $response_type;
48 break;
49 default:
50 header('Content-Type: application/json; charset=UTF-8');
51 $this->type = 'json';
52 $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.');
53 break;
54 }
55 }
56
57 /**
58 * Routes error messages depending on response type.
59 *
95b5e1a7
GJ
60 * @param int $code HTTP status code number
61 * @param int $desc descriptive error message
62 *
d8c46ff7
GJ
63 * @return void
64 */
65 public function error($code, $desc)
66 {
67 $response = null;
68
69 switch ($this->type) {
70 case 'csv':
71 $response = $this->csvError($desc);
72 break;
73 case 'html':
74 $response = $this->htmlError($code, $desc);
75 break;
76 case 'json':
77 $response = $this->jsonError($code, $desc);
78 break;
79 case 'text':
80 $response = $this->textError($code, $desc);
81 break;
82 }
83
95b5e1a7 84 //http_response_code(500); // "500 Internal Server Error"
d8c46ff7
GJ
85 echo $response;
86 }
87
88 /**
89 * Routes success messages depending on response type.
90 *
91 * @param mixed[] $files
95b5e1a7 92 *
d8c46ff7
GJ
93 * @return void
94 */
95 public function send($files)
96 {
97 $response = null;
98
99 switch ($this->type) {
100 case 'csv':
101 $response = $this->csvSuccess($files);
102 break;
103 case 'html':
104 $response = $this->htmlSuccess($files);
105 break;
106 case 'json':
107 $response = $this->jsonSuccess($files);
108 break;
109 case 'text':
110 $response = $this->textSuccess($files);
111 break;
112 }
113
114 http_response_code(200); // "200 OK". Success.
115 echo $response;
116 }
117
118 /**
119 * Indicates with CSV body the request was invalid.
120 *
121 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7
GJ
122 *
123 * @param int $description descriptive error message
124 *
125 * @return string error message in CSV format
d8c46ff7
GJ
126 */
127 private static function csvError($description)
128 {
129 return '"error"'."\r\n"."\"$description\""."\r\n";
130 }
131
132 /**
133 * Indicates with CSV body the request was successful.
134 *
135 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7 136 *
d8c46ff7 137 * @param mixed[] $files
95b5e1a7
GJ
138 *
139 * @return string success message in CSV format
d8c46ff7
GJ
140 */
141 private static function csvSuccess($files)
142 {
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";
149 }
150
151 return $result;
152 }
153
154 /**
155 * Indicates with HTML body the request was invalid.
156 *
157 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7
GJ
158 *
159 * @param int $code HTTP status code number
160 * @param int $description descriptive error message
161 *
162 * @return string error message in HTML format
d8c46ff7
GJ
163 */
164 private static function htmlError($code, $description)
165 {
166 return '<p>ERROR: ('.$code.') '.$description.'</p>';
167 }
168
169 /**
170 * Indicates with HTML body the request was successful.
171 *
172 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7 173 *
d8c46ff7 174 * @param mixed[] $files
95b5e1a7
GJ
175 *
176 * @return string success message in HTML format
d8c46ff7
GJ
177 */
178 private static function htmlSuccess($files)
179 {
180 $result = '';
181
182 foreach ($files as $file) {
95b5e1a7 183 $result .= '<a href="'.$file['url'].'">'.$file['url'].'</a><br>';
d8c46ff7
GJ
184 }
185
186 return $result;
187 }
188
189 /**
190 * Indicates with JSON body the request was invalid.
191 *
192 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7
GJ
193 *
194 * @param int $code HTTP status code number
195 * @param int $description descriptive error message
196 *
197 * @return string error message in pretty-printed JSON format
d8c46ff7
GJ
198 */
199 private static function jsonError($code, $description)
200 {
95b5e1a7 201 return json_encode([
d8c46ff7
GJ
202 'success' => false,
203 'errorcode' => $code,
204 'description' => $description,
95b5e1a7 205 ], JSON_PRETTY_PRINT);
d8c46ff7
GJ
206 }
207
208 /**
209 * Indicates with JSON body the request was successful.
210 *
211 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7 212 *
d8c46ff7 213 * @param mixed[] $files
95b5e1a7
GJ
214 *
215 * @return string success message in pretty-printed JSON format
d8c46ff7
GJ
216 */
217 private static function jsonSuccess($files)
218 {
95b5e1a7 219 return json_encode([
d8c46ff7
GJ
220 'success' => true,
221 'files' => $files,
95b5e1a7 222 ], JSON_PRETTY_PRINT);
d8c46ff7
GJ
223 }
224
225 /**
226 * Indicates with plain text body the request was invalid.
227 *
228 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7
GJ
229 *
230 * @param int $code HTTP status code number
231 * @param int $description descriptive error message
232 *
233 * @return string error message in plain text format
d8c46ff7
GJ
234 */
235 private static function textError($code, $description)
236 {
237 return 'ERROR: ('.$code.') '.$description;
238 }
239
240 /**
241 * Indicates with plain text body the request was successful.
242 *
243 * @deprecated 2.1.0 Will be renamed to camelCase format.
95b5e1a7 244 *
d8c46ff7 245 * @param mixed[] $files
95b5e1a7
GJ
246 *
247 * @return string success message in plain text format
d8c46ff7
GJ
248 */
249 private static function textSuccess($files)
250 {
251 $result = '';
252
253 foreach ($files as $file) {
254 $result .= $file['url']."\n";
255 }
256
257 return $result;
258 }
259}