]>
jfr.im git - uguu.git/blob - src/Classes/Response.php
18c743d04a52d1009be3f62fe2ae359086337de2
5 * @copyright Copyright (c) 2022-2023 Go Johansson (nokonoko) <neku@pomf.se>
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.
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.
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/>.
21 namespace Pomf\Uguu\Classes
;
28 * Takes a string as an argument and sets the header to the appropriate content type
30 * @param $response_type string The type of response you want to return.
31 * Valid options are: csv, html, json, text.
33 public function __construct(string $response_type)
35 switch ($response_type) {
37 header('Content-Type: text/csv; charset=UTF-8');
38 $this->type
= $response_type;
41 header('Content-Type: text/html; charset=UTF-8');
42 $this->type
= $response_type;
45 header('Content-Type: application/json; charset=UTF-8');
46 $this->type
= $response_type;
49 header('Content-Type: text/plain; charset=UTF-8');
53 header('Content-Type: text/plain; charset=UTF-8');
54 $this->type
= $response_type;
57 header('Content-Type: application/json; charset=UTF-8');
64 * Returns a string based on the type of response requested
66 * @param $code mixed The HTTP status code to return.
67 * @param $desc string The description of the error.
69 public function error(int $code, string $desc):string
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),
77 http_response_code($code);
82 /* Returning a string that contains the error message. */
83 private static function csvError(string $description):string
85 return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
89 * Returns a string containing an HTML paragraph element with the error code and description
91 * @param $code int|string The error code.
92 * @param $description string The description of the error.
94 * @return string A string.
96 private static function htmlError(int|string $code, string $description):string
98 return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
102 * Returns a JSON string with the error code and description
104 * @param $code int|string The error code.
105 * @param $description string The description of the error.
107 * @return bool|string A JSON string
109 private static function jsonError(int|string $code, string $description):bool|string
113 'errorcode' => $code,
114 'description' => $description,
115 ], JSON_PRETTY_PRINT
);
119 * Returns a string that contains the error code and description
121 * @param $code int|string The error code.
122 * @param $description string The description of the error.
124 * @return string A string with the error code and description.
126 private static function textError(int|string $code, string $description):string
128 return 'ERROR: (' . $code . ') ' . $description;
132 * "If the type is csv, then call the csvSuccess function,
133 * if the type is html, then call the htmlSuccess function, etc."
135 * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful
137 * @param $files array An array of file objects.
139 public function send(array $files):void
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),
147 http_response_code(200); // "200 OK". Success.
152 * Takes an array of files and returns a CSV string
154 * @param $files array An array of files that have been uploaded.
156 * @return string A string of the files in the array.
158 private static function csvSuccess(array $files):string
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";
171 * Takes an array of files and returns a string of HTML links
173 * @param $files array An array of files to be uploaded.
175 * @return string the result of the foreach loop.
177 private static function htmlSuccess(array $files):string
180 foreach ($files as $file) {
181 $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>';
187 * Returns a JSON string that contains a success message and the files that were uploaded
189 * @param $files array The files to be uploaded.
191 * @return bool|string A JSON string
193 private static function jsonSuccess(array $files):bool|string
198 ], JSON_PRETTY_PRINT
);
202 * Takes an array of files and returns a string of URLs
204 * @param $files array The files to be uploaded.
206 * @return string the url of the file.
208 private static function textSuccess(array $files):string
211 foreach ($files as $file) {
212 $result .= $file['url'] . "\n";