]>
jfr.im git - uguu.git/blob - src/Classes/Response.php
6 * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
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.
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.
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/>.
22 namespace Pomf\Uguu\Classes
;
28 public function __construct($response_type = "json")
30 switch ($response_type) {
32 header('Content-Type: text/csv; charset=UTF-8');
33 $this->type
= $response_type;
36 header('Content-Type: text/html; charset=UTF-8');
37 $this->type
= $response_type;
40 header('Content-Type: application/json; charset=UTF-8');
41 $this->type
= $response_type;
44 header('Content-Type: text/plain; charset=UTF-8');
48 header('Content-Type: text/plain; charset=UTF-8');
49 $this->type
= $response_type;
52 header('Content-Type: application/json; charset=UTF-8');
54 $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.');
59 public function error($code, $desc): void
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),
67 http_response_code($code);
71 private static function csvError($description): string
73 return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
76 private static function htmlError($code, $description): string
78 return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
81 private static function jsonError($code, $description): bool|string
86 'description' => $description,
87 ], JSON_PRETTY_PRINT
);
91 private static function textError($code, $description): string
93 return 'ERROR: (' . $code . ') ' . $description;
96 public function send($files): void
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),
105 http_response_code(200); // "200 OK". Success.
109 private static function csvSuccess($files): string
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";
122 private static function htmlSuccess($files): string
126 foreach ($files as $file) {
127 $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>';
133 private static function jsonSuccess($files): bool|string
138 ], JSON_PRETTY_PRINT
);
141 private static function textSuccess($files): string
145 foreach ($files as $file) {
146 $result .= $file['url'] . "\n";