]>
jfr.im git - uguu.git/blob - src/Classes/Response.php
5 * @copyright Copyright (c) 2022 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. Valid options are: csv, html, json, text.
32 public function __construct(string $response_type = "json")
34 switch ($response_type) {
36 header('Content-Type: text/csv; charset=UTF-8');
37 $this->type
= $response_type;
40 header('Content-Type: text/html; charset=UTF-8');
41 $this->type
= $response_type;
44 header('Content-Type: application/json; charset=UTF-8');
45 $this->type
= $response_type;
48 header('Content-Type: text/plain; charset=UTF-8');
52 header('Content-Type: text/plain; charset=UTF-8');
53 $this->type
= $response_type;
56 header('Content-Type: application/json; charset=UTF-8');
58 $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.');
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(mixed $code, string $desc):void
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);
81 /* Returning a string that contains the error message. */
82 private static function csvError(string $description):string
84 return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
88 * Returns a string containing an HTML paragraph element with the error code and description
90 * @param $code int|string The error code.
91 * @param $description string The description of the error.
93 * @return string A string.
95 private static function htmlError(int|string $code, string $description):string
97 return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
101 * Returns a JSON string with the error code and description
103 * @param $code int|string The error code.
104 * @param $description string The description of the error.
106 * @return bool|string A JSON string
108 private static function jsonError(int|string $code, string $description):bool|string
112 'errorcode' => $code,
113 'description' => $description,
114 ], JSON_PRETTY_PRINT
);
118 * Returns a string that contains the error code and description
120 * @param $code int|string The error code.
121 * @param $description string The description of the error.
123 * @return string A string with the error code and description.
125 private static function textError(int|string $code, string $description):string
127 return 'ERROR: (' . $code . ') ' . $description;
131 * "If the type is csv, then call the csvSuccess function, if the type is html, then call the htmlSuccess function, etc."
133 * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful
135 * @param $files array An array of file objects.
137 public function send(array $files):void
139 $response = match ($this->type
) {
140 'csv' => $this->csvSuccess($files),
141 'html' => $this->htmlSuccess($files),
142 'json' => $this->jsonSuccess($files),
143 'text' => $this->textSuccess($files),
145 http_response_code(200); // "200 OK". Success.
150 * Takes an array of files and returns a CSV string
152 * @param $files array An array of files that have been uploaded.
154 * @return string A string of the files in the array.
156 private static function csvSuccess(array $files):string
158 $result = '"name","url","hash","size"' . "\r\n";
159 foreach ($files as $file) {
160 $result .= '"' . $file['name'] . '"' . ',' .
161 '"' . $file['url'] . '"' . ',' .
162 '"' . $file['hash'] . '"' . ',' .
163 '"' . $file['size'] . '"' . "\r\n";
169 * Takes an array of files and returns a string of HTML links
171 * @param $files array An array of files to be uploaded.
173 * @return string the result of the foreach loop.
175 private static function htmlSuccess(array $files):string
178 foreach ($files as $file) {
179 $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>';
185 * Returns a JSON string that contains a success message and the files that were uploaded
187 * @param $files array The files to be uploaded.
189 * @return bool|string A JSON string
191 private static function jsonSuccess(array $files):bool|string
196 ], JSON_PRETTY_PRINT
);
200 * Takes an array of files and returns a string of URLs
202 * @param $files array The files to be uploaded.
204 * @return string the url of the file.
206 private static function textSuccess(array $files):string
209 foreach ($files as $file) {
210 $result .= $file['url'] . "\n";