TeamSpeak 3 PHP Framework  1.1.23
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
TeamSpeak3_Adapter_ServerQuery_Reply Class Reference

Provides methods to analyze and format a ServerQuery reply. More...

Public Member Functions

 __construct (array $rpl, $cmd=null, TeamSpeak3_Node_Host $con=null, $exp=TRUE)
 Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object. More...
 
 toString ()
 Returns the reply as an TeamSpeak3_Helper_String object. More...
 
 toLines ()
 Returns the reply as a standard PHP array where each element represents one item. More...
 
 toTable ()
 Returns the reply as a standard PHP array where each element represents one item in table format. More...
 
 toArray ()
 Returns a multi-dimensional array containing the reply splitted in multiple rows and columns. More...
 
 toAssocArray ($ident)
 Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns. More...
 
 toList ()
 Returns an array containing the reply splitted in multiple rows and columns. More...
 
 toObjectArray ()
 Returns an array containing stdClass objects. More...
 
 getCommandString ()
 Returns the command used to get this reply. More...
 
 getNotifyEvents ()
 Returns an array of events that occured before or during this reply. More...
 
 getErrorProperty ($ident, $default=null)
 Returns the value for a specified error property. More...
 

Protected Member Functions

 fetchError ($err)
 Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if there's an error. More...
 
 fetchReply ($rpl)
 Parses a ServerQuery reply and creates a TeamSpeak3_Helper_String object. More...
 

Protected Attributes

 $cmd = null
 
 $rpl = null
 
 $con = null
 
 $err = array()
 
 $evt = array()
 
 $exp = TRUE
 

Detailed Description

Provides methods to analyze and format a ServerQuery reply.

Definition at line 32 of file Reply.php.

Constructor & Destructor Documentation

TeamSpeak3_Adapter_ServerQuery_Reply::__construct ( array  $rpl,
  $cmd = null,
TeamSpeak3_Node_Host  $con = null,
  $exp = TRUE 
)

Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object.

Parameters
array$rpl
string$cmd
boolean$exp
TeamSpeak3_Node_Host$con
Returns
TeamSpeak3_Adapter_ServerQuery_Reply

Definition at line 85 of file Reply.php.

References fetchError(), and fetchReply().

86  {
87  $this->cmd = new TeamSpeak3_Helper_String($cmd);
88  $this->con = $con;
89  $this->exp = (bool) $exp;
90 
91  $this->fetchError(array_pop($rpl));
92  $this->fetchReply($rpl);
93  }

Member Function Documentation

TeamSpeak3_Adapter_ServerQuery_Reply::toString ( )

Returns the reply as an TeamSpeak3_Helper_String object.

Returns
TeamSpeak3_Helper_String

Definition at line 100 of file Reply.php.

Referenced by toAssocArray(), and toLines().

101  {
102  return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
103  }
TeamSpeak3_Adapter_ServerQuery_Reply::toLines ( )

Returns the reply as a standard PHP array where each element represents one item.

Returns
array

Definition at line 110 of file Reply.php.

References TeamSpeak3\SEPARATOR_LIST, and toString().

Referenced by toTable().

111  {
112  if(!count($this->rpl)) return array();
113 
114  $list = $this->toString(0)->split(TeamSpeak3::SEPARATOR_LIST);
115 
116  if(!func_num_args())
117  {
118  for($i = 0; $i < count($list); $i++) $list[$i]->unescape();
119  }
120 
121  return $list;
122  }
TeamSpeak3_Adapter_ServerQuery_Reply::toTable ( )

Returns the reply as a standard PHP array where each element represents one item in table format.

Returns
array

Definition at line 129 of file Reply.php.

References TeamSpeak3\SEPARATOR_CELL, and toLines().

Referenced by toArray().

130  {
131  $table = array();
132 
133  foreach($this->toLines(0) as $cells)
134  {
135  $pairs = $cells->split(TeamSpeak3::SEPARATOR_CELL);
136 
137  if(!func_num_args())
138  {
139  for($i = 0; $i < count($pairs); $i++) $pairs[$i]->unescape();
140  }
141 
142  $table[] = $pairs;
143  }
144 
145  return $table;
146  }
TeamSpeak3_Adapter_ServerQuery_Reply::toArray ( )

Returns a multi-dimensional array containing the reply splitted in multiple rows and columns.

Returns
array

Definition at line 153 of file Reply.php.

References TeamSpeak3\SEPARATOR_PAIR, and toTable().

Referenced by toAssocArray(), toList(), and toObjectArray().

154  {
155  $array = array();
156  $table = $this->toTable(1);
157 
158  for($i = 0; $i < count($table); $i++)
159  {
160  foreach($table[$i] as $pair)
161  {
162  if(!count($pair))
163  {
164  continue;
165  }
166 
167  if(!$pair->contains(TeamSpeak3::SEPARATOR_PAIR))
168  {
169  $array[$i][$pair->toString()] = null;
170  }
171  else
172  {
173  list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR, 2);
174 
175  $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
176  }
177  }
178  }
179 
180  return $array;
181  }
TeamSpeak3_Adapter_ServerQuery_Reply::toAssocArray (   $ident)

Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns.

The identifier specified by key will be used while indexing the array.

Parameters
$key
Returns
array

Definition at line 190 of file Reply.php.

References toArray(), and toString().

191  {
192  $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
193  $array = array();
194 
195  foreach($nodes as $node)
196  {
197  if(array_key_exists($ident, $node))
198  {
199  $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
200  }
201  else
202  {
203  throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid parameter", 0x602);
204  }
205  }
206 
207  return $array;
208  }
TeamSpeak3_Adapter_ServerQuery_Reply::toList ( )

Returns an array containing the reply splitted in multiple rows and columns.

Returns
array

Definition at line 215 of file Reply.php.

References toArray().

216  {
217  $array = func_num_args() ? $this->toArray(1) : $this->toArray();
218 
219  if(count($array) == 1)
220  {
221  return array_shift($array);
222  }
223 
224  return $array;
225  }
TeamSpeak3_Adapter_ServerQuery_Reply::toObjectArray ( )

Returns an array containing stdClass objects.

Returns
ArrayObject

Definition at line 232 of file Reply.php.

References toArray().

233  {
234  $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
235 
236  for($i = 0; $i < count($array); $i++)
237  {
238  $array[$i] = (object) $array[$i];
239  }
240 
241  return $array;
242  }
TeamSpeak3_Adapter_ServerQuery_Reply::getCommandString ( )

Returns the command used to get this reply.

Returns
TeamSpeak3_Helper_String

Definition at line 249 of file Reply.php.

250  {
251  return new TeamSpeak3_Helper_String($this->cmd);
252  }
TeamSpeak3_Adapter_ServerQuery_Reply::getNotifyEvents ( )

Returns an array of events that occured before or during this reply.

Returns
array

Definition at line 259 of file Reply.php.

260  {
261  return $this->evt;
262  }
TeamSpeak3_Adapter_ServerQuery_Reply::getErrorProperty (   $ident,
  $default = null 
)

Returns the value for a specified error property.

Parameters
string$ident
mixed$default
Returns
mixed

Definition at line 271 of file Reply.php.

Referenced by fetchError().

272  {
273  return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
274  }
TeamSpeak3_Adapter_ServerQuery_Reply::fetchError (   $err)
protected

Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if there's an error.

Parameters
string$err
Exceptions
TeamSpeak3_Adapter_ServerQuery_Exception
Returns
void

Definition at line 284 of file Reply.php.

References getErrorProperty(), TeamSpeak3_Helper_Signal\getInstance(), TeamSpeak3\SEPARATOR_CELL, and TeamSpeak3\SEPARATOR_PAIR.

Referenced by __construct().

285  {
286  $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3);
287 
288  foreach($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair)
289  {
290  list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR);
291 
292  $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
293  }
294 
295  TeamSpeak3_Helper_Signal::getInstance()->emit("notifyError", $this);
296 
297  if($this->getErrorProperty("id", 0x00) != 0x00 && $this->exp)
298  {
299  if($permid = $this->getErrorProperty("failed_permid"))
300  {
301  if($permsid = key($this->con->request("permget permid=" . $permid, FALSE)->toAssocArray("permsid")))
302  {
303  $suffix = " (failed on " . $permsid . ")";
304  }
305  else
306  {
307  $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
308  }
309  }
310  elseif($details = $this->getErrorProperty("extra_msg"))
311  {
312  $suffix = " (" . trim($details) . ")";
313  }
314  else
315  {
316  $suffix = "";
317  }
318 
319  throw new TeamSpeak3_Adapter_ServerQuery_Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"));
320  }
321  }
TeamSpeak3_Adapter_ServerQuery_Reply::fetchReply (   $rpl)
protected

Parses a ServerQuery reply and creates a TeamSpeak3_Helper_String object.

Parameters
string$rpl
Returns
void

Definition at line 329 of file Reply.php.

References TeamSpeak3\EVENT, TeamSpeak3\GREET, and TeamSpeak3\SEPARATOR_LIST.

Referenced by __construct().

330  {
331  foreach($rpl as $key => $val)
332  {
333  if($val->startsWith(TeamSpeak3::GREET))
334  {
335  unset($rpl[$key]);
336  }
337  elseif($val->startsWith(TeamSpeak3::EVENT))
338  {
339  $this->evt[] = new TeamSpeak3_Adapter_ServerQuery_Event($rpl[$key], $this->con);
340  unset($rpl[$key]);
341  }
342  }
343 
344  $this->rpl = new TeamSpeak3_Helper_String(implode(TeamSpeak3::SEPARATOR_LIST, $rpl));
345  }

The documentation for this class was generated from the following file: