]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | /** | |
4 | * Uguu | |
5 | * | |
6 | * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | namespace Pomf\Uguu\Classes; | |
23 | ||
24 | class Response | |
25 | { | |
26 | public mixed $type; | |
27 | ||
28 | public function __construct($response_type = "json") | |
29 | { | |
30 | switch ($response_type) { | |
31 | case 'csv': | |
32 | header('Content-Type: text/csv; charset=UTF-8'); | |
33 | $this->type = $response_type; | |
34 | break; | |
35 | case 'html': | |
36 | header('Content-Type: text/html; charset=UTF-8'); | |
37 | $this->type = $response_type; | |
38 | break; | |
39 | case 'json': | |
40 | header('Content-Type: application/json; charset=UTF-8'); | |
41 | $this->type = $response_type; | |
42 | break; | |
43 | case 'gyazo': | |
44 | header('Content-Type: text/plain; charset=UTF-8'); | |
45 | $this->type = 'text'; | |
46 | break; | |
47 | case 'text': | |
48 | header('Content-Type: text/plain; charset=UTF-8'); | |
49 | $this->type = $response_type; | |
50 | break; | |
51 | default: | |
52 | header('Content-Type: application/json; charset=UTF-8'); | |
53 | $this->type = 'json'; | |
54 | $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.'); | |
55 | break; | |
56 | } | |
57 | } | |
58 | ||
59 | public function error($code, $desc): void | |
60 | { | |
61 | $response = match ($this->type) { | |
62 | 'csv' => $this->csvError($desc), | |
63 | 'html' => $this->htmlError($code, $desc), | |
64 | 'json' => $this->jsonError($code, $desc), | |
65 | 'text' => $this->textError($code, $desc), | |
66 | }; | |
67 | http_response_code($code); | |
68 | echo $response; | |
69 | } | |
70 | ||
71 | private static function csvError($description): string | |
72 | { | |
73 | return '"error"' . "\r\n" . "\"$description\"" . "\r\n"; | |
74 | } | |
75 | ||
76 | private static function htmlError($code, $description): string | |
77 | { | |
78 | return '<p>ERROR: (' . $code . ') ' . $description . '</p>'; | |
79 | } | |
80 | ||
81 | private static function jsonError($code, $description): bool|string | |
82 | { | |
83 | return json_encode([ | |
84 | 'success' => false, | |
85 | 'errorcode' => $code, | |
86 | 'description' => $description, | |
87 | ], JSON_PRETTY_PRINT); | |
88 | } | |
89 | ||
90 | ||
91 | private static function textError($code, $description): string | |
92 | { | |
93 | return 'ERROR: (' . $code . ') ' . $description; | |
94 | } | |
95 | ||
96 | public function send($files): void | |
97 | { | |
98 | $response = match ($this->type) { | |
99 | 'csv' => $this->csvSuccess($files), | |
100 | 'html' => $this->htmlSuccess($files), | |
101 | 'json' => $this->jsonSuccess($files), | |
102 | 'text' => $this->textSuccess($files), | |
103 | }; | |
104 | ||
105 | http_response_code(200); // "200 OK". Success. | |
106 | echo $response; | |
107 | } | |
108 | ||
109 | private static function csvSuccess($files): string | |
110 | { | |
111 | $result = '"name","url","hash","size"' . "\r\n"; | |
112 | foreach ($files as $file) { | |
113 | $result .= '"' . $file['name'] . '"' . ',' . | |
114 | '"' . $file['url'] . '"' . ',' . | |
115 | '"' . $file['hash'] . '"' . ',' . | |
116 | '"' . $file['size'] . '"' . "\r\n"; | |
117 | } | |
118 | ||
119 | return $result; | |
120 | } | |
121 | ||
122 | private static function htmlSuccess($files): string | |
123 | { | |
124 | $result = ''; | |
125 | ||
126 | foreach ($files as $file) { | |
127 | $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>'; | |
128 | } | |
129 | ||
130 | return $result; | |
131 | } | |
132 | ||
133 | private static function jsonSuccess($files): bool|string | |
134 | { | |
135 | return json_encode([ | |
136 | 'success' => true, | |
137 | 'files' => $files, | |
138 | ], JSON_PRETTY_PRINT); | |
139 | } | |
140 | ||
141 | private static function textSuccess($files): string | |
142 | { | |
143 | $result = ''; | |
144 | ||
145 | foreach ($files as $file) { | |
146 | $result .= $file['url'] . "\n"; | |
147 | } | |
148 | ||
149 | return $result; | |
150 | } | |
151 | } |