]>
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 | |
f059e2cf | 21 | namespace Pomf\Uguu\Classes; |
cec6349e GJ |
22 | |
23 | class Response | |
e480c0e5 | 24 | { |
8be20956 | 25 | public string $type; |
cec6349e GJ |
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 | */ | |
8be20956 | 32 | public function __construct(string $response_type) |
cec6349e GJ |
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'; | |
cec6349e GJ |
58 | break; |
59 | } | |
e480c0e5 | 60 | } |
cec6349e GJ |
61 | |
62 | /** | |
63 | * Returns a string based on the type of response requested | |
64 | * | |
65 | * @param $code mixed The HTTP status code to return. | |
66 | * @param $desc string The description of the error. | |
67 | */ | |
68 | public function error(mixed $code, string $desc):void | |
69 | { | |
70 | $response = match ($this->type) { | |
71 | 'csv' => $this->csvError($desc), | |
72 | 'html' => $this->htmlError($code, $desc), | |
73 | 'json' => $this->jsonError($code, $desc), | |
74 | 'text' => $this->textError($code, $desc), | |
75 | }; | |
76 | http_response_code($code); | |
77 | echo $response; | |
e480c0e5 | 78 | } |
cec6349e GJ |
79 | |
80 | /* Returning a string that contains the error message. */ | |
81 | private static function csvError(string $description):string | |
82 | { | |
83 | return '"error"' . "\r\n" . "\"$description\"" . "\r\n"; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Returns a string containing an HTML paragraph element with the error code and description | |
88 | * | |
89 | * @param $code int|string The error code. | |
90 | * @param $description string The description of the error. | |
91 | * | |
92 | * @return string A string. | |
93 | */ | |
94 | private static function htmlError(int|string $code, string $description):string | |
95 | { | |
96 | return '<p>ERROR: (' . $code . ') ' . $description . '</p>'; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Returns a JSON string with the error code and description | |
101 | * | |
102 | * @param $code int|string The error code. | |
103 | * @param $description string The description of the error. | |
104 | * | |
105 | * @return bool|string A JSON string | |
106 | */ | |
107 | private static function jsonError(int|string $code, string $description):bool|string | |
108 | { | |
109 | return json_encode([ | |
110 | 'success' => false, | |
111 | 'errorcode' => $code, | |
112 | 'description' => $description, | |
113 | ], JSON_PRETTY_PRINT); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Returns a string that contains the error code and description | |
118 | * | |
119 | * @param $code int|string The error code. | |
120 | * @param $description string The description of the error. | |
121 | * | |
122 | * @return string A string with the error code and description. | |
123 | */ | |
124 | private static function textError(int|string $code, string $description):string | |
125 | { | |
126 | return 'ERROR: (' . $code . ') ' . $description; | |
127 | } | |
128 | ||
129 | /** | |
130 | * "If the type is csv, then call the csvSuccess function, if the type is html, then call the htmlSuccess function, etc." | |
131 | * | |
132 | * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful | |
133 | * | |
134 | * @param $files array An array of file objects. | |
135 | */ | |
136 | public function send(array $files):void | |
137 | { | |
138 | $response = match ($this->type) { | |
139 | 'csv' => $this->csvSuccess($files), | |
140 | 'html' => $this->htmlSuccess($files), | |
141 | 'json' => $this->jsonSuccess($files), | |
142 | 'text' => $this->textSuccess($files), | |
143 | }; | |
144 | http_response_code(200); // "200 OK". Success. | |
145 | echo $response; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Takes an array of files and returns a CSV string | |
150 | * | |
151 | * @param $files array An array of files that have been uploaded. | |
152 | * | |
153 | * @return string A string of the files in the array. | |
154 | */ | |
155 | private static function csvSuccess(array $files):string | |
156 | { | |
157 | $result = '"name","url","hash","size"' . "\r\n"; | |
158 | foreach ($files as $file) { | |
159 | $result .= '"' . $file['name'] . '"' . ',' . | |
160 | '"' . $file['url'] . '"' . ',' . | |
161 | '"' . $file['hash'] . '"' . ',' . | |
162 | '"' . $file['size'] . '"' . "\r\n"; | |
163 | } | |
164 | return $result; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Takes an array of files and returns a string of HTML links | |
169 | * | |
170 | * @param $files array An array of files to be uploaded. | |
171 | * | |
172 | * @return string the result of the foreach loop. | |
173 | */ | |
174 | private static function htmlSuccess(array $files):string | |
175 | { | |
176 | $result = ''; | |
177 | foreach ($files as $file) { | |
178 | $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>'; | |
179 | } | |
180 | return $result; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Returns a JSON string that contains a success message and the files that were uploaded | |
185 | * | |
186 | * @param $files array The files to be uploaded. | |
187 | * | |
188 | * @return bool|string A JSON string | |
189 | */ | |
190 | private static function jsonSuccess(array $files):bool|string | |
191 | { | |
192 | return json_encode([ | |
193 | 'success' => true, | |
194 | 'files' => $files, | |
195 | ], JSON_PRETTY_PRINT); | |
196 | } | |
197 | ||
198 | /** | |
199 | * Takes an array of files and returns a string of URLs | |
200 | * | |
201 | * @param $files array The files to be uploaded. | |
202 | * | |
203 | * @return string the url of the file. | |
204 | */ | |
205 | private static function textSuccess(array $files):string | |
206 | { | |
207 | $result = ''; | |
208 | foreach ($files as $file) { | |
209 | $result .= $file['url'] . "\n"; | |
210 | } | |
211 | return $result; | |
e480c0e5 | 212 | } |
e480c0e5 | 213 | } |