]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * Uguu | |
4 | * | |
5 | * @copyright Copyright (c) 2022-2023 Go Johansson (nokonoko) <neku@pomf.se> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
21 | namespace Pomf\Uguu\Classes; | |
22 | ||
23 | class Response | |
24 | { | |
25 | public string $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. | |
31 | * Valid options are: csv, html, json, text. | |
32 | */ | |
33 | public function __construct(string $response_type) | |
34 | { | |
35 | switch ($response_type) { | |
36 | case 'csv': | |
37 | header('Content-Type: text/csv; charset=UTF-8'); | |
38 | $this->type = $response_type; | |
39 | break; | |
40 | case 'html': | |
41 | header('Content-Type: text/html; charset=UTF-8'); | |
42 | $this->type = $response_type; | |
43 | break; | |
44 | case 'json': | |
45 | header('Content-Type: application/json; charset=UTF-8'); | |
46 | $this->type = $response_type; | |
47 | break; | |
48 | case 'gyazo': | |
49 | header('Content-Type: text/plain; charset=UTF-8'); | |
50 | $this->type = 'text'; | |
51 | break; | |
52 | case 'text': | |
53 | header('Content-Type: text/plain; charset=UTF-8'); | |
54 | $this->type = $response_type; | |
55 | break; | |
56 | default: | |
57 | header('Content-Type: application/json; charset=UTF-8'); | |
58 | $this->type = 'json'; | |
59 | break; | |
60 | } | |
61 | } | |
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(int $code, string $desc):string | |
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; | |
79 | exit(1); | |
80 | } | |
81 | ||
82 | /* Returning a string that contains the error message. */ | |
83 | private static function csvError(string $description):string | |
84 | { | |
85 | return '"error"' . "\r\n" . "\"$description\"" . "\r\n"; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Returns a string containing an HTML paragraph element with the error code and description | |
90 | * | |
91 | * @param $code int|string The error code. | |
92 | * @param $description string The description of the error. | |
93 | * | |
94 | * @return string A string. | |
95 | */ | |
96 | private static function htmlError(int|string $code, string $description):string | |
97 | { | |
98 | return '<p>ERROR: (' . $code . ') ' . $description . '</p>'; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Returns a JSON string with the error code and description | |
103 | * | |
104 | * @param $code int|string The error code. | |
105 | * @param $description string The description of the error. | |
106 | * | |
107 | * @return bool|string A JSON string | |
108 | */ | |
109 | private static function jsonError(int|string $code, string $description):bool|string | |
110 | { | |
111 | return json_encode([ | |
112 | 'success' => false, | |
113 | 'errorcode' => $code, | |
114 | 'description' => $description, | |
115 | ], JSON_PRETTY_PRINT); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns a string that contains the error code and description | |
120 | * | |
121 | * @param $code int|string The error code. | |
122 | * @param $description string The description of the error. | |
123 | * | |
124 | * @return string A string with the error code and description. | |
125 | */ | |
126 | private static function textError(int|string $code, string $description):string | |
127 | { | |
128 | return 'ERROR: (' . $code . ') ' . $description; | |
129 | } | |
130 | ||
131 | /** | |
132 | * "If the type is csv, then call the csvSuccess function, | |
133 | * if the type is html, then call the htmlSuccess function, etc." | |
134 | * | |
135 | * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful | |
136 | * | |
137 | * @param $files array An array of file objects. | |
138 | */ | |
139 | public function send(array $files):void | |
140 | { | |
141 | $response = match ($this->type) { | |
142 | 'csv' => $this->csvSuccess($files), | |
143 | 'html' => $this->htmlSuccess($files), | |
144 | 'json' => $this->jsonSuccess($files), | |
145 | 'text' => $this->textSuccess($files), | |
146 | }; | |
147 | http_response_code(200); // "200 OK". Success. | |
148 | echo $response; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Takes an array of files and returns a CSV string | |
153 | * | |
154 | * @param $files array An array of files that have been uploaded. | |
155 | * | |
156 | * @return string A string of the files in the array. | |
157 | */ | |
158 | private static function csvSuccess(array $files):string | |
159 | { | |
160 | $result = '"name","url","hash","size"' . "\r\n"; | |
161 | foreach ($files as $file) { | |
162 | $result .= '"' . $file['name'] . '"' . ',' . | |
163 | '"' . $file['url'] . '"' . ',' . | |
164 | '"' . $file['hash'] . '"' . ',' . | |
165 | '"' . $file['size'] . '"' . "\r\n"; | |
166 | } | |
167 | return $result; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Takes an array of files and returns a string of HTML links | |
172 | * | |
173 | * @param $files array An array of files to be uploaded. | |
174 | * | |
175 | * @return string the result of the foreach loop. | |
176 | */ | |
177 | private static function htmlSuccess(array $files):string | |
178 | { | |
179 | $result = ''; | |
180 | foreach ($files as $file) { | |
181 | $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>'; | |
182 | } | |
183 | return $result; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Returns a JSON string that contains a success message and the files that were uploaded | |
188 | * | |
189 | * @param $files array The files to be uploaded. | |
190 | * | |
191 | * @return bool|string A JSON string | |
192 | */ | |
193 | private static function jsonSuccess(array $files):bool|string | |
194 | { | |
195 | return json_encode([ | |
196 | 'success' => true, | |
197 | 'files' => $files, | |
198 | ], JSON_PRETTY_PRINT); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Takes an array of files and returns a string of URLs | |
203 | * | |
204 | * @param $files array The files to be uploaded. | |
205 | * | |
206 | * @return string the url of the file. | |
207 | */ | |
208 | private static function textSuccess(array $files):string | |
209 | { | |
210 | $result = ''; | |
211 | foreach ($files as $file) { | |
212 | $result .= $file['url'] . "\n"; | |
213 | } | |
214 | return $result; | |
215 | } | |
216 | } |