|
4 | 4 |
|
5 | 5 | use UnityWebPortal\lib\exceptions\NoDieException; |
6 | 6 | use UnityWebPortal\lib\exceptions\ArrayKeyException; |
| 7 | +use RuntimeException; |
7 | 8 |
|
8 | | -enum UnityHTTPDMessageSeverity |
| 9 | +enum UnityHTTPDMessageSeverity: string |
9 | 10 | { |
10 | | - case INFO; |
11 | | - case GOOD; |
12 | | - case BAD; |
13 | | - |
14 | | - public function toCSSClass(): string |
15 | | - { |
16 | | - return match ($this) { |
17 | | - self::INFO => "info", |
18 | | - self::GOOD => "good", |
19 | | - self::BAD => "bad", |
20 | | - }; |
21 | | - } |
| 11 | + case INFO = "info"; |
| 12 | + case GOOD = "good"; |
| 13 | + case BAD = "bad"; |
22 | 14 | } |
23 | 15 |
|
24 | 16 | class UnityHTTPD |
@@ -234,19 +226,30 @@ public static function messageInfo(string $title, string $body) |
234 | 226 | return self::message($title, $body, UnityHTTPDMessageSeverity::INFO); |
235 | 227 | } |
236 | 228 |
|
237 | | - public static function dumpMessages() |
| 229 | + public static function getMessages() |
238 | 230 | { |
239 | | - $output = ""; |
240 | | - try { |
241 | | - $messages = $_SESSION["messages"]; |
242 | | - } catch (ArrayKeyException) { |
243 | | - return $output; |
| 231 | + if (!isset($_SESSION)) { |
| 232 | + throw new RuntimeException('$_SESSION is unset'); |
| 233 | + } |
| 234 | + if (!array_key_exists("messages", $_SESSION)) { |
| 235 | + throw new RuntimeException('array key "messages" does not exist for $_SESSION'); |
| 236 | + } |
| 237 | + $messages = $_SESSION["messages"]; |
| 238 | + if (!is_array($messages)) { |
| 239 | + $type = gettype($messages); |
| 240 | + throw new RuntimeException("\$_SESSION['messages'] is type '$type', not an array"); |
244 | 241 | } |
245 | | - foreach ($messages as [$title, $body, $severity]) { |
| 242 | + return $messages; |
| 243 | + } |
| 244 | + |
| 245 | + public static function exportMessagesHTML() |
| 246 | + { |
| 247 | + $output = ""; |
| 248 | + foreach (self::getMessages() as [$title, $body, $severity]) { |
246 | 249 | $title_stripped = strip_tags($title); |
247 | 250 | $body_stripped = strip_tags($body); |
248 | | - $severityCSSClass = $severity->toCSSClass(); |
249 | | - $output .= "<div class='message $severityCSSClass'><h2>$title_stripped</h2><p>$body_stripped</p></div>\n"; |
| 251 | + $severity_str = $severity->value; |
| 252 | + $output .= "<div class='message $severity_str'><h2>$title_stripped</h2><p>$body_stripped</p></div>\n"; |
250 | 253 | } |
251 | 254 | return $output; |
252 | 255 | } |
|
0 commit comments