ts3phpframework
Loading...
Searching...
No Matches
TeamSpeak3Exception.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Exception;
4
5use Exception;
6use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Signal;
7use PlanetTeamSpeak\TeamSpeak3Framework\Helper\StringHelper;
8
16{
22 protected int $raw_code = 0x00;
23
29 protected StringHelper|string|null $raw_mesg = null;
30
36 protected static array $messages = [];
37
44 public function __construct(string $mesg, int $code = 0x00)
45 {
46 parent::__construct($mesg, $code);
47
48 $this->raw_code = $code;
49 $this->raw_mesg = $mesg;
50
51 if (array_key_exists($code, self::$messages)) {
52 $this->message = $this->prepareCustomMessage(self::$messages[$code]);
53 }
54
55 Signal::getInstance()->emit("errorException", $this);
56 }
57
64 protected function prepareCustomMessage(StringHelper $mesg): string
65 {
66 $args = [
67 "code" => $this->getCode(),
68 "mesg" => $this->getMessage(),
69 "line" => $this->getLine(),
70 "file" => $this->getFile(),
71 ];
72
73 return $mesg->arg($args)->toString();
74 }
75
84 public static function registerCustomMessage(int $code, string $mesg): void
85 {
86 if (array_key_exists($code, self::$messages)) {
87 throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
88 }
89
90 self::$messages[$code] = new StringHelper($mesg);
91 }
92
100 public static function unregisterCustomMessage(int $code): void
101 {
102 if (!array_key_exists($code, self::$messages)) {
103 throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
104 }
105
106 unset(self::$messages[$code]);
107 }
108
114 public function getRawCode(): int
115 {
116 return $this->raw_code;
117 }
118
124 public function getRawMessage(): string|StringHelper|null
125 {
126 return $this->raw_mesg;
127 }
128
134 public function getSender(): string
135 {
136 $trace = $this->getTrace();
137
138 return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
139 }
140}