]>
Commit | Line | Data |
---|---|---|
e480c0e5 | 1 | <?php |
8f7f8840 | 2 | /** |
cec6349e | 3 | * Uguu |
8f7f8840 | 4 | * |
cec6349e | 5 | * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se> |
8f7f8840 | 6 | * |
cec6349e GJ |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
8f7f8840 | 11 | * |
cec6349e GJ |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
8f7f8840 | 16 | * |
cec6349e GJ |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
8f7f8840 | 19 | */ |
cec6349e | 20 | |
d5e2cc4c | 21 | namespace Pomf\Uguu\Classes; |
cec6349e GJ |
22 | |
23 | class Response | |
e480c0e5 | 24 | { |
cec6349e GJ |
25 | public mixed $type; |
26 | ||
27 | /** | |
28 | * Takes a string as an argument and sets the header to the appropriate content type | |
29 | * | |
30 | * @param $response_type string The type of response you want to return. Valid options are: csv, html, json, text. | |
31 | */ | |
32 | public function __construct(string $response_type = "json") | |
33 | { | |
34 | switch ($response_type) { | |
35 | case 'csv': | |
36 | header('Content-Type: text/csv; charset=UTF-8'); | |
37 | $this->type = $response_type; | |
38 | break; | |
39 | case 'html': | |
40 | header('Content-Type: text/html; charset=UTF-8'); | |
41 | $this->type = $response_type; | |
42 | break; | |
43 | case 'json': | |
44 | header('Content-Type: application/json; charset=UTF-8'); | |
45 | $this->type = $response_type; | |
46 | break; | |
47 | case 'gyazo': | |
48 | header('Content-Type: text/plain; charset=UTF-8'); | |
49 | $this->type = 'text'; | |
50 | break; | |
51 | case 'text': | |
52 | header('Content-Type: text/plain; charset=UTF-8'); | |
53 | $this->type = $response_type; | |
54 | break; | |
55 | default: | |
56 | header('Content-Type: application/json; charset=UTF-8'); | |
57 | $this->type = 'json'; | |
58 | $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.'); | |
59 | break; | |
60 | } | |
e480c0e5 | 61 | } |
cec6349e GJ |
62 | |
63 | /** | |
64 | * Returns a string based on the type of response requested | |
65 | * | |
66 | * @param $code mixed The HTTP status code to return. | |
67 | * @param $desc string The description of the error. | |
68 | */ | |
69 | public function error(mixed $code, string $desc):void | |
70 | { | |
71 | $response = match ($this->type) { | |
72 | 'csv' => $this->csvError($desc), | |
73 | 'html' => $this->htmlError($code, $desc), | |
74 | 'json' => $this->jsonError($code, $desc), | |
75 | 'text' => $this->textError($code, $desc), | |
76 | }; | |
77 | http_response_code($code); | |
78 | echo $response; | |
e480c0e5 | 79 | } |
cec6349e GJ |
80 | |
81 | /* Returning a string that contains the error message. */ | |
82 | private static function csvError(string $description):string | |
83 | { | |
84 | return '"error"' . "\r\n" . "\"$description\"" . "\r\n"; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Returns a string containing an HTML paragraph element with the error code and description | |
89 | * | |
90 | * @param $code int|string The error code. | |
91 | * @param $description string The description of the error. | |
92 | * | |
93 | * @return string A string. | |
94 | */ | |
95 | private static function htmlError(int|string $code, string $description):string | |
96 | { | |
97 | return '<p>ERROR: (' . $code . ') ' . $description . '</p>'; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Returns a JSON string with the error code and description | |
102 | * | |
103 | * @param $code int|string The error code. | |
104 | * @param $description string The description of the error. | |
105 | * | |
106 | * @return bool|string A JSON string | |
107 | */ | |
108 | private static function jsonError(int|string $code, string $description):bool|string | |
109 | { | |
110 | return json_encode([ | |
111 | 'success' => false, | |
112 | 'errorcode' => $code, | |
113 | 'description' => $description, | |
114 | ], JSON_PRETTY_PRINT); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Returns a string that contains the error code and description | |
119 | * | |
120 | * @param $code int|string The error code. | |
121 | * @param $description string The description of the error. | |
122 | * | |
123 | * @return string A string with the error code and description. | |
124 | */ | |
125 | private static function textError(int|string $code, string $description):string | |
126 | { | |
127 | return 'ERROR: (' . $code . ') ' . $description; | |
128 | } | |
129 | ||
130 | /** | |
131 | * "If the type is csv, then call the csvSuccess function, if the type is html, then call the htmlSuccess function, etc." | |
132 | * | |
133 | * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful | |
134 | * | |
135 | * @param $files array An array of file objects. | |
136 | */ | |
137 | public function send(array $files):void | |
138 | { | |
139 | $response = match ($this->type) { | |
140 | 'csv' => $this->csvSuccess($files), | |
141 | 'html' => $this->htmlSuccess($files), | |
142 | 'json' => $this->jsonSuccess($files), | |
143 | 'text' => $this->textSuccess($files), | |
144 | }; | |
145 | http_response_code(200); // "200 OK". Success. | |
146 | echo $response; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Takes an array of files and returns a CSV string | |
151 | * | |
152 | * @param $files array An array of files that have been uploaded. | |
153 | * | |
154 | * @return string A string of the files in the array. | |
155 | */ | |
156 | private static function csvSuccess(array $files):string | |
157 | { | |
158 | $result = '"name","url","hash","size"' . "\r\n"; | |
159 | foreach ($files as $file) { | |
160 | $result .= '"' . $file['name'] . '"' . ',' . | |
161 | '"' . $file['url'] . '"' . ',' . | |
162 | '"' . $file['hash'] . '"' . ',' . | |
163 | '"' . $file['size'] . '"' . "\r\n"; | |
164 | } | |
165 | return $result; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Takes an array of files and returns a string of HTML links | |
170 | * | |
171 | * @param $files array An array of files to be uploaded. | |
172 | * | |
173 | * @return string the result of the foreach loop. | |
174 | */ | |
175 | private static function htmlSuccess(array $files):string | |
176 | { | |
177 | $result = ''; | |
178 | foreach ($files as $file) { | |
179 | $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>'; | |
180 | } | |
181 | return $result; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Returns a JSON string that contains a success message and the files that were uploaded | |
186 | * | |
187 | * @param $files array The files to be uploaded. | |
188 | * | |
189 | * @return bool|string A JSON string | |
190 | */ | |
191 | private static function jsonSuccess(array $files):bool|string | |
192 | { | |
193 | return json_encode([ | |
194 | 'success' => true, | |
195 | 'files' => $files, | |
196 | ], JSON_PRETTY_PRINT); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Takes an array of files and returns a string of URLs | |
201 | * | |
202 | * @param $files array The files to be uploaded. | |
203 | * | |
204 | * @return string the url of the file. | |
205 | */ | |
206 | private static function textSuccess(array $files):string | |
207 | { | |
208 | $result = ''; | |
209 | foreach ($files as $file) { | |
210 | $result .= $file['url'] . "\n"; | |
211 | } | |
212 | return $result; | |
e480c0e5 | 213 | } |
e480c0e5 | 214 | } |