TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Reply.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Reply.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Adapter_ServerQuery_Reply
30  * @brief Provides methods to analyze and format a ServerQuery reply.
31  */
33 {
34  /**
35  * Stores the command used to get this reply.
36  *
37  * @var TeamSpeak3_Helper_String
38  */
39  protected $cmd = null;
40 
41  /**
42  * Stores the servers reply (if available).
43  *
44  * @var TeamSpeak3_Helper_String
45  */
46  protected $rpl = null;
47 
48  /**
49  * Stores connected TeamSpeak3_Node_Host object.
50  *
51  * @var TeamSpeak3_Node_Host
52  */
53  protected $con = null;
54 
55  /**
56  * Stores an assoc array containing the error info for this reply.
57  *
58  * @var array
59  */
60  protected $err = array();
61 
62  /**
63  * Sotres an array of events that occured before or during this reply.
64  *
65  * @var array
66  */
67  protected $evt = array();
68 
69  /**
70  * Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object.
71  *
72  * @param array $rpl
73  * @param string $cmd
74  * @param TeamSpeak3_Node_Host $con
75  * @return TeamSpeak3_Adapter_ServerQuery_Reply
76  */
77  public function __construct(array $rpl, $cmd = null, TeamSpeak3_Node_Host $con = null)
78  {
79  $this->cmd = new TeamSpeak3_Helper_String($cmd);
80  $this->con = $con;
81 
82  $this->fetchError(array_pop($rpl));
83  $this->fetchReply($rpl);
84  }
85 
86  /**
87  * Returns the reply as an TeamSpeak3_Helper_String object.
88  *
89  * @return TeamSpeak3_Helper_String
90  */
91  public function toString()
92  {
93  return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
94  }
95 
96  /**
97  * Returns the reply as a standard PHP array where each element represents one item.
98  *
99  * @return array
100  */
101  public function toLines()
102  {
103  if(!count($this->rpl)) return array();
104 
105  $list = $this->toString(0)->split(TeamSpeak3::SEPARATOR_LIST);
106 
107  if(!func_num_args())
108  {
109  for($i = 0; $i < count($list); $i++) $list[$i]->unescape();
110  }
111 
112  return $list;
113  }
114 
115  /**
116  * Returns the reply as a standard PHP array where each element represents one item in table format.
117  *
118  * @return array
119  */
120  public function toTable()
121  {
122  $table = array();
123 
124  foreach($this->toLines(0) as $cells)
125  {
126  $pairs = $cells->split(TeamSpeak3::SEPARATOR_CELL);
127 
128  if(!func_num_args())
129  {
130  for($i = 0; $i < count($pairs); $i++) $pairs[$i]->unescape();
131  }
132 
133  $table[] = $pairs;
134  }
135 
136  return $table;
137  }
138 
139  /**
140  * Returns a multi-dimensional array containing the reply splitted in multiple rows and columns.
141  *
142  * @return array
143  */
144  public function toArray()
145  {
146  $array = array();
147  $table = $this->toTable(1);
148 
149  for($i = 0; $i < count($table); $i++)
150  {
151  foreach($table[$i] as $pair)
152  {
153  if(!$pair->contains(TeamSpeak3::SEPARATOR_PAIR))
154  {
155  $array[$i][$pair->toString()] = null;
156  }
157  else
158  {
159  list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR, 2);
160 
161  $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
162  }
163  }
164  }
165 
166  return $array;
167  }
168 
169  /**
170  * Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns.
171  * The identifier specified by key will be used while indexing the array.
172  *
173  * @param $key
174  * @return array
175  */
176  public function toAssocArray($ident)
177  {
178  $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
179  $array = array();
180 
181  foreach($nodes as $node)
182  {
183  if(array_key_exists($ident, $node))
184  {
185  $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
186  }
187  else
188  {
189  throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid parameter", 0x602);
190  }
191  }
192 
193  return $array;
194  }
195 
196  /**
197  * Returns an array containing the reply splitted in multiple rows and columns.
198  *
199  * @return array
200  */
201  public function toList()
202  {
203  $array = func_num_args() ? $this->toArray(1) : $this->toArray();
204 
205  if(count($array) == 1)
206  {
207  return array_shift($array);
208  }
209 
210  return $array;
211  }
212 
213  /**
214  * Returns an array containing stdClass objects.
215  *
216  * @return ArrayObject
217  */
218  public function toObjectArray()
219  {
220  $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
221 
222  for($i = 0; $i < count($array); $i++)
223  {
224  $array[$i] = (object) $array[$i];
225  }
226 
227  return $array;
228  }
229 
230  /**
231  * Returns the command used to get this reply.
232  *
233  * @return TeamSpeak3_Helper_String
234  */
235  public function getCommandString()
236  {
237  return new TeamSpeak3_Helper_String($this->cmd);
238  }
239 
240  /**
241  * Returns an array of events that occured before or during this reply.
242  *
243  * @return array
244  */
245  public function getNotifyEvents()
246  {
247  return $this->evt;
248  }
249 
250  /**
251  * Returns the value for a specified error property.
252  *
253  * @param string $ident
254  * @param mixed $default
255  * @return mixed
256  */
257  public function getErrorProperty($ident, $default = null)
258  {
259  return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
260  }
261 
262  /**
263  * Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if
264  * there's an error.
265  *
266  * @param string $err
267  * @throws TeamSpeak3_Adapter_ServerQuery_Exception
268  * @return void
269  */
270  protected function fetchError($err)
271  {
272  $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3);
273 
274  foreach($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair)
275  {
276  list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR);
277 
278  $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
279  }
280 
281  TeamSpeak3_Helper_Signal::getInstance()->emit("notifyError", $this);
282 
283  if($this->getErrorProperty("id", 0x00) != 0x00)
284  {
285  if($permid = $this->getErrorProperty("failed_permid"))
286  {
287  try
288  {
289  $suffix = " (failed on " . key($this->con->selfPermCheck($permid)) . ")";
290  }
292  {
293  $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
294  }
295  }
296  elseif($details = $this->getErrorProperty("extra_msg"))
297  {
298  $suffix = " (" . trim($details) . ")";
299  }
300  else
301  {
302  $suffix = "";
303  }
304 
305  throw new TeamSpeak3_Adapter_ServerQuery_Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"));
306  }
307  }
308 
309  /**
310  * Parses a ServerQuery reply and creates a TeamSpeak3_Helper_String object.
311  *
312  * @param string $rpl
313  * @return void
314  */
315  protected function fetchReply($rpl)
316  {
317  foreach($rpl as $key => $val)
318  {
319  if($val->startsWith(TeamSpeak3::GREET))
320  {
321  unset($rpl[$key]);
322  }
323  elseif($val->startsWith(TeamSpeak3::EVENT))
324  {
325  $this->evt[] = new TeamSpeak3_Adapter_ServerQuery_Event($rpl[$key], $this->con);
326  unset($rpl[$key]);
327  }
328  }
329 
330  $this->rpl = new TeamSpeak3_Helper_String(implode(TeamSpeak3::SEPARATOR_LIST, $rpl));
331  }
332 }