ts3phpframework
Loading...
Searching...
No Matches
Reply.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Adapter\ServerQuery;
4
5use PlanetTeamSpeak\TeamSpeak3Framework\Exception\AdapterException;
6use PlanetTeamSpeak\TeamSpeak3Framework\Exception\ServerQueryException;
7use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Signal;
8use PlanetTeamSpeak\TeamSpeak3Framework\Helper\StringHelper;
9use PlanetTeamSpeak\TeamSpeak3Framework\Node\Host;
10use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
11
18class Reply
19{
25 protected StringHelper $cmd;
26
32 protected ?StringHelper $rpl = null;
33
39 protected ?Host $con;
40
46 protected array $err = [];
47
53 protected array $evt = [];
54
60 protected bool $exp = true;
61
72 public function __construct(array $rpl, string $cmd = "", Host $con = null, bool $exp = true)
73 {
74 $this->cmd = new StringHelper($cmd);
75 $this->con = $con;
76 $this->exp = $exp;
77
78 $this->fetchError(array_pop($rpl));
79 $this->fetchReply($rpl);
80 }
81
87 public function toString(): ?StringHelper
88 {
89 return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
90 }
91
97 public function toLines(): array
98 {
99 if (!count($this->rpl)) {
100 return [];
101 }
102
103 $list = $this->toString(0)->split(TeamSpeak3::SEPARATOR_LIST);
104
105 if (!func_num_args()) {
106 for ($i = 0; $i < count($list); $i++) {
107 $list[$i]->unescape();
108 }
109 }
110
111 return $list;
112 }
113
119 public function toTable(): array
120 {
121 $table = [];
122
123 foreach ($this->toLines(0) as $cells) {
124 $pairs = $cells->split(TeamSpeak3::SEPARATOR_CELL);
125
126 if (!func_num_args()) {
127 for ($i = 0; $i < count($pairs); $i++) {
128 $pairs[$i]->unescape();
129 }
130 }
131
132 $table[] = $pairs;
133 }
134
135 return $table;
136 }
137
143 public function toArray(): array
144 {
145 $array = [];
146 $table = $this->toTable(1);
147
148 for ($i = 0; $i < count($table); $i++) {
149 foreach ($table[$i] as $pair) {
150 if (!count($pair)) {
151 continue;
152 }
153
154 if (!$pair->contains(TeamSpeak3::SEPARATOR_PAIR)) {
155 $array[$i][$pair->toString()] = null;
156 } else {
157 list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR, 2);
158
159 $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
160 }
161 }
162 }
163
164 return $array;
165 }
166
175 public function toAssocArray($ident): array
176 {
177 $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
178 $array = [];
179
180 foreach ($nodes as $node) {
181 if (isset($node[$ident])) {
182 $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
183 } else {
184 throw new ServerQueryException("invalid parameter. ident '$ident' does not exist in node '".json_encode($node)."'", 0x602);
185 }
186 }
187
188 return $array;
189 }
190
196 public function toList(): array
197 {
198 $array = func_num_args() ? $this->toArray(1) : $this->toArray();
199
200 if (count($array) == 1) {
201 return array_shift($array);
202 }
203
204 return $array;
205 }
206
212 public function toObjectArray(): array
213 {
214 $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
215
216 for ($i = 0; $i < count($array); $i++) {
217 $array[$i] = (object)$array[$i];
218 }
219
220 return $array;
221 }
222
229 {
230 return new StringHelper($this->cmd);
231 }
232
238 public function getNotifyEvents(): array
239 {
240 return $this->evt;
241 }
242
250 public function getErrorProperty(string $ident, mixed $default = null): mixed
251 {
252 return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
253 }
254
264 protected function fetchError(StringHelper $err): void
265 {
266 $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3);
267
268 foreach ($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair) {
269 list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR);
270
271 $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
272 }
273
274 Signal::getInstance()->emit("notifyError", $this);
275
276 if ($this->getErrorProperty("id", 0x00) != 0x00 && $this->exp) {
277 if ($permid = $this->getErrorProperty("failed_permid")) {
278 if ($permsid = key($this->con->request("permget permid=" . $permid, false)->toAssocArray("permsid"))) {
279 $suffix = " (failed on " . $permsid . ")";
280 } else {
281 $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
282 }
283 } elseif ($details = $this->getErrorProperty("extra_msg")) {
284 $suffix = " (" . trim($details) . ")";
285 } else {
286 $suffix = "";
287 }
288
289 throw new ServerQueryException($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"), $this->getErrorProperty("return_code"));
290 }
291 }
292
300 protected function fetchReply(array $rpl): void
301 {
302 foreach ($rpl as $key => $val) {
303 if ($val->startsWith(TeamSpeak3::TS3_MOTD_PREFIX) || $val->startsWith(TeamSpeak3::TEA_MOTD_PREFIX) || (defined("CUSTOM_MOTD_PREFIX") && $val->startsWith(CUSTOM_MOTD_PREFIX))) {
304 unset($rpl[$key]);
305 } elseif ($val->startsWith(TeamSpeak3::EVENT)) {
306 $this->evt[] = new Event($val, $this->con);
307 unset($rpl[$key]);
308 }
309 }
310
311 $this->rpl = new StringHelper(implode(TeamSpeak3::SEPARATOR_LIST, $rpl));
312 }
313}
Provides methods to analyze and format a ServerQuery event.
Definition Event.php:21
Provides methods to analyze and format a ServerQuery reply.
Definition Reply.php:19
getErrorProperty(string $ident, mixed $default=null)
Definition Reply.php:250
__construct(array $rpl, string $cmd="", Host $con=null, bool $exp=true)
Definition Reply.php:72
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Adapter\ServerQuery objects.
Class describing a TeamSpeak 3 server instance and all it's parameters.
Definition Host.php:23
const SEPARATOR_CELL
protocol cell separator
const SEPARATOR_PAIR
protocol pair separator
const SEPARATOR_LIST
protocol list separator