]>
jfr.im git - uguu.git/blob - src/Classes/Response.php
5 * @copyright Copyright (c) 2022-2024 Go Johansson (nokonoko) <neku@pomf.se>
7 * Note that this was previously distributed under the MIT license 2015-2022.
9 * If you are a company that wants to use Uguu I urge you to contact me to
10 * solve any potential license issues rather then using pre-2022 code.
12 * A special thanks goes out to the open source community around the world
13 * for supporting and being the backbone of projects like Uguu.
15 * This project can be found at <https://github.com/nokonoko/Uguu>.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 namespace Pomf\Uguu\Classes
;
38 * Takes a string as an argument and sets the header to the appropriate content type
40 * @param $response_type string The type of response you want to return.
41 * Valid options are: csv, html, json, text.
43 public function __construct(string $response_type)
45 switch ($response_type) {
47 header('Content-Type: text/csv; charset=UTF-8');
48 $this->type
= $response_type;
51 header('Content-Type: text/html; charset=UTF-8');
52 $this->type
= $response_type;
55 header('Content-Type: application/json; charset=UTF-8');
56 $this->type
= $response_type;
59 header('Content-Type: text/plain; charset=UTF-8');
63 header('Content-Type: text/plain; charset=UTF-8');
64 $this->type
= $response_type;
67 header('Content-Type: application/json; charset=UTF-8');
74 * Returns a string based on the type of response requested
76 * @param $code mixed The HTTP status code to return.
77 * @param $desc string The description of the error.
79 public function error(int $code, string $desc):string
81 $response = match ($this->type
) {
82 'csv' => $this->csvError($desc),
83 'html' => $this->htmlError($code, $desc),
84 'json' => $this->jsonError($code, $desc),
85 'text' => $this->textError($code, $desc),
87 http_response_code($code);
92 /* Returning a string that contains the error message. */
93 private static function csvError(string $description):string
95 return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
99 * Returns a string containing an HTML paragraph element with the error code and description
101 * @param $code int|string The error code.
102 * @param $description string The description of the error.
104 * @return string A string.
106 private static function htmlError(int|string $code, string $description):string
108 return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
112 * Returns a JSON string with the error code and description
114 * @param $code int|string The error code.
115 * @param $description string The description of the error.
117 * @return bool|string A JSON string
119 private static function jsonError(int|string $code, string $description):bool|string
123 'errorcode' => $code,
124 'description' => $description,
125 ], JSON_PRETTY_PRINT
);
129 * Returns a string that contains the error code and description
131 * @param $code int|string The error code.
132 * @param $description string The description of the error.
134 * @return string A string with the error code and description.
136 private static function textError(int|string $code, string $description):string
138 return 'ERROR: (' . $code . ') ' . $description;
142 * "If the type is csv, then call the csvSuccess function,
143 * if the type is html, then call the htmlSuccess function, etc."
145 * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful
147 * @param $files array An array of file objects.
149 public function send(array $files):void
151 $response = match ($this->type
) {
152 'csv' => $this->csvSuccess($files),
153 'html' => $this->htmlSuccess($files),
154 'json' => $this->jsonSuccess($files),
155 'text' => $this->textSuccess($files),
157 http_response_code(200); // "200 OK". Success.
162 * Takes an array of files and returns a CSV string
164 * @param $files array An array of files that have been uploaded.
166 * @return string A string of the files in the array.
168 private static function csvSuccess(array $files):string
170 $result = '"name","url","hash","size"' . "\r\n";
171 foreach ($files as $file) {
172 $result .= '"' . $file['name'] . '"' . ',' .
173 '"' . $file['url'] . '"' . ',' .
174 '"' . $file['hash'] . '"' . ',' .
175 '"' . $file['size'] . '"' . "\r\n";
181 * Takes an array of files and returns a string of HTML links
183 * @param $files array An array of files to be uploaded.
185 * @return string the result of the foreach loop.
187 private static function htmlSuccess(array $files):string
190 foreach ($files as $file) {
191 $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>';
197 * Returns a JSON string that contains a success message and the files that were uploaded
199 * @param $files array The files to be uploaded.
201 * @return bool|string A JSON string
203 private static function jsonSuccess(array $files):bool|string
208 ], JSON_PRETTY_PRINT
);
212 * Takes an array of files and returns a string of URLs
214 * @param $files array The files to be uploaded.
216 * @return string the url of the file.
218 private static function textSuccess(array $files):string
221 foreach ($files as $file) {
222 $result .= $file['url'] . "\n";