]> jfr.im git - uguu.git/blame - src/Classes/Response.php
Update checkfiles.sh
[uguu.git] / src / Classes / Response.php
CommitLineData
e480c0e5 1<?php
24383942 2
8f7f8840 3 /**
cec6349e 4 * Uguu
8f7f8840 5 *
cec6349e 6 * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
8f7f8840 7 *
cec6349e
GJ
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.
8f7f8840 12 *
cec6349e
GJ
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.
8f7f8840 17 *
cec6349e
GJ
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/>.
8f7f8840 20 */
24383942 21
f059e2cf 22 namespace Pomf\Uguu\Classes;
24383942
GJ
23
24class Response
25{
26 public string $type;
27
28 /**
29 * Takes a string as an argument and sets the header to the appropriate content type
30 *
31 * @param $response_type string The type of response you want to return.
32 * Valid options are: csv, html, json, text.
33 */
34 public function __construct(string $response_type)
e480c0e5 35 {
24383942
GJ
36 switch ($response_type) {
37 case 'csv':
38 header('Content-Type: text/csv; charset=UTF-8');
39 $this->type = $response_type;
40 break;
41 case 'html':
42 header('Content-Type: text/html; charset=UTF-8');
43 $this->type = $response_type;
44 break;
45 case 'json':
46 header('Content-Type: application/json; charset=UTF-8');
47 $this->type = $response_type;
48 break;
49 case 'gyazo':
50 header('Content-Type: text/plain; charset=UTF-8');
51 $this->type = 'text';
52 break;
53 case 'text':
54 header('Content-Type: text/plain; charset=UTF-8');
55 $this->type = $response_type;
56 break;
57 default:
58 header('Content-Type: application/json; charset=UTF-8');
59 $this->type = 'json';
60 break;
e480c0e5 61 }
24383942
GJ
62 }
63
64 /**
65 * Returns a string based on the type of response requested
66 *
67 * @param $code mixed The HTTP status code to return.
68 * @param $desc string The description of the error.
69 */
70 public function error(int $code, string $desc): void
71 {
72 $response = match ($this->type) {
73 'csv' => $this->csvError($desc),
74 'html' => $this->htmlError($code, $desc),
75 'json' => $this->jsonError($code, $desc),
76 'text' => $this->textError($code, $desc),
77 };
cec6349e
GJ
78 http_response_code($code);
79 echo $response;
24383942
GJ
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 };
cec6349e
GJ
147 http_response_code(200); // "200 OK". Success.
148 echo $response;
24383942
GJ
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";
cec6349e 166 }
24383942
GJ
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>';
cec6349e 182 }
24383942
GJ
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";
e480c0e5 213 }
24383942 214 return $result;
e480c0e5 215 }
24383942 216}