Skip to content

Commit eb449ee

Browse files
committed
backed enum
1 parent ad53049 commit eb449ee

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

resources/lib/UnityHTTPD.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44

55
use UnityWebPortal\lib\exceptions\NoDieException;
66
use UnityWebPortal\lib\exceptions\ArrayKeyException;
7+
use RuntimeException;
78

8-
enum UnityHTTPDMessageSeverity
9+
enum UnityHTTPDMessageSeverity: string
910
{
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";
2214
}
2315

2416
class UnityHTTPD
@@ -234,19 +226,30 @@ public static function messageInfo(string $title, string $body)
234226
return self::message($title, $body, UnityHTTPDMessageSeverity::INFO);
235227
}
236228

237-
public static function dumpMessages()
229+
public static function getMessages()
238230
{
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");
244241
}
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]) {
246249
$title_stripped = strip_tags($title);
247250
$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";
250253
}
251254
return $output;
252255
}

0 commit comments

Comments
 (0)