]> jfr.im git - uguu.git/blame - static/php/classes/Response.class.php
durr
[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 *
15 * @var string $type Response type
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 *
60 * @param int $code HTTP status code number.
61 * @param int $desc Descriptive error message.
62 * @return void
63 */
64 public function error($code, $desc)
65 {
66 $response = null;
67
68 switch ($this->type) {
69 case 'csv':
70 $response = $this->csvError($desc);
71 break;
72 case 'html':
73 $response = $this->htmlError($code, $desc);
74 break;
75 case 'json':
76 $response = $this->jsonError($code, $desc);
77 break;
78 case 'text':
79 $response = $this->textError($code, $desc);
80 break;
81 }
82
83 http_response_code(500); // "500 Internal Server Error"
84 echo $response;
85 }
86
87 /**
88 * Routes success messages depending on response type.
89 *
90 * @param mixed[] $files
91 * @return void
92 */
93 public function send($files)
94 {
95 $response = null;
96
97 switch ($this->type) {
98 case 'csv':
99 $response = $this->csvSuccess($files);
100 break;
101 case 'html':
102 $response = $this->htmlSuccess($files);
103 break;
104 case 'json':
105 $response = $this->jsonSuccess($files);
106 break;
107 case 'text':
108 $response = $this->textSuccess($files);
109 break;
110 }
111
112 http_response_code(200); // "200 OK". Success.
113 echo $response;
114 }
115
116 /**
117 * Indicates with CSV body the request was invalid.
118 *
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.
122 */
123 private static function csvError($description)
124 {
125 return '"error"'."\r\n"."\"$description\""."\r\n";
126 }
127
128 /**
129 * Indicates with CSV body the request was successful.
130 *
131 * @deprecated 2.1.0 Will be renamed to camelCase format.
132 * @param mixed[] $files
133 * @return string Success message in CSV format.
134 */
135 private static function csvSuccess($files)
136 {
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";
143 }
144
145 return $result;
146 }
147
148 /**
149 * Indicates with HTML body the request was invalid.
150 *
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.
155 */
156 private static function htmlError($code, $description)
157 {
158 return '<p>ERROR: ('.$code.') '.$description.'</p>';
159 }
160
161 /**
162 * Indicates with HTML body the request was successful.
163 *
164 * @deprecated 2.1.0 Will be renamed to camelCase format.
165 * @param mixed[] $files
166 * @return string Success message in HTML format.
167 */
168 private static function htmlSuccess($files)
169 {
170 $result = '';
171
172 foreach ($files as $file) {
173 $result .= '<a href="'.$file['url'].'">'.$file['url'].'</a><br>';
174 }
175
176 return $result;
177 }
178
179 /**
180 * Indicates with JSON body the request was invalid.
181 *
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.
186 */
187 private static function jsonError($code, $description)
188 {
189 return json_encode(array(
190 'success' => false,
191 'errorcode' => $code,
192 'description' => $description,
193 ), JSON_PRETTY_PRINT);
194 }
195
196 /**
197 * Indicates with JSON body the request was successful.
198 *
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.
202 */
203 private static function jsonSuccess($files)
204 {
205 return json_encode(array(
206 'success' => true,
207 'files' => $files,
208 ), JSON_PRETTY_PRINT);
209 }
210
211 /**
212 * Indicates with plain text body the request was invalid.
213 *
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.
218 */
219 private static function textError($code, $description)
220 {
221 return 'ERROR: ('.$code.') '.$description;
222 }
223
224 /**
225 * Indicates with plain text body the request was successful.
226 *
227 * @deprecated 2.1.0 Will be renamed to camelCase format.
228 * @param mixed[] $files
229 * @return string Success message in plain text format.
230 */
231 private static function textSuccess($files)
232 {
233 $result = '';
234
235 foreach ($files as $file) {
236 $result .= $file['url']."\n";
237 }
238
239 return $result;
240 }
241}