TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Adapter/ServerQuery/Reply.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: Reply.php 2/18/2012 12:42:45 scp@orilla $
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * @package   TeamSpeak3
00023  * @version   1.1.12
00024  * @author    Sven 'ScP' Paulsen
00025  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
00026  */
00027 
00028 /**
00029  * @class TeamSpeak3_Adapter_ServerQuery_Reply
00030  * @brief Provides methods to analyze and format a ServerQuery reply.
00031  */
00032 class TeamSpeak3_Adapter_ServerQuery_Reply
00033 {
00034   /**
00035    * Stores the command used to get this reply.
00036    *
00037    * @var TeamSpeak3_Helper_String
00038    */
00039   protected $cmd = null;
00040 
00041   /**
00042    * Stores the servers reply (if available).
00043    *
00044    * @var TeamSpeak3_Helper_String
00045    */
00046   protected $rpl = null;
00047 
00048   /**
00049    * Stores connected TeamSpeak3_Node_Host object.
00050    *
00051    * @var TeamSpeak3_Node_Host
00052    */
00053   protected $con = null;
00054 
00055   /**
00056    * Stores an assoc array containing the error info for this reply.
00057    *
00058    * @var array
00059    */
00060   protected $err = array();
00061 
00062   /**
00063    * Sotres an array of events that occured before or during this reply.
00064    *
00065    * @var array
00066    */
00067   protected $evt = array();
00068 
00069   /**
00070    * Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object.
00071    *
00072    * @param  array  $rpl
00073    * @param  string $cmd
00074    * @param  TeamSpeak3_Node_Host $con
00075    * @return TeamSpeak3_Adapter_ServerQuery_Reply
00076    */
00077   public function __construct(array $rpl, $cmd = null, TeamSpeak3_Node_Host $con = null)
00078   {
00079     $this->cmd = new TeamSpeak3_Helper_String($cmd);
00080     $this->con = $con;
00081 
00082     $this->fetchError(array_pop($rpl));
00083     $this->fetchReply($rpl);
00084   }
00085 
00086   /**
00087    * Returns the reply as an TeamSpeak3_Helper_String object.
00088    *
00089    * @return TeamSpeak3_Helper_String
00090    */
00091   public function toString()
00092   {
00093     return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
00094   }
00095 
00096   /**
00097    * Returns the reply as a standard PHP array where each element represents one item.
00098    *
00099    * @return array
00100    */
00101   public function toLines()
00102   {
00103     if(!count($this->rpl)) return array();
00104 
00105     $list = $this->toString(0)->split(TeamSpeak3::SEPARATOR_LIST);
00106 
00107     if(!func_num_args())
00108     {
00109       for($i = 0; $i < count($list); $i++) $list[$i]->unescape();
00110     }
00111 
00112     return $list;
00113   }
00114 
00115   /**
00116    * Returns the reply as a standard PHP array where each element represents one item in table format.
00117    *
00118    * @return array
00119    */
00120   public function toTable()
00121   {
00122     $table = array();
00123 
00124     foreach($this->toLines(0) as $cells)
00125     {
00126       $pairs = $cells->split(TeamSpeak3::SEPARATOR_CELL);
00127 
00128       if(!func_num_args())
00129       {
00130         for($i = 0; $i < count($pairs); $i++) $pairs[$i]->unescape();
00131       }
00132 
00133       $table[] = $pairs;
00134     }
00135 
00136     return $table;
00137   }
00138 
00139   /**
00140    * Returns a multi-dimensional array containing the reply splitted in multiple rows and columns.
00141    *
00142    * @return array
00143    */
00144   public function toArray()
00145   {
00146     $array = array();
00147     $table = $this->toTable(1);
00148 
00149     for($i = 0; $i < count($table); $i++)
00150     {
00151       foreach($table[$i] as $pair)
00152       {
00153         if(!$pair->contains(TeamSpeak3::SEPARATOR_PAIR))
00154         {
00155           $array[$i][$pair->toString()] = null;
00156         }
00157         else
00158         {
00159           list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR, 2);
00160 
00161           $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
00162         }
00163       }
00164     }
00165 
00166     return $array;
00167   }
00168 
00169   /**
00170    * Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns.
00171    * The identifier specified by key will be used while indexing the array.
00172    *
00173    * @param  $key
00174    * @return array
00175    */
00176   public function toAssocArray($ident)
00177   {
00178     $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
00179     $array = array();
00180 
00181     foreach($nodes as $node)
00182     {
00183       if(array_key_exists($ident, $node))
00184       {
00185         $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
00186       }
00187       else
00188       {
00189         throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid parameter", 0x602);
00190       }
00191     }
00192 
00193     return $array;
00194   }
00195 
00196   /**
00197    * Returns an array containing the reply splitted in multiple rows and columns.
00198    *
00199    * @return array
00200    */
00201   public function toList()
00202   {
00203     $array = func_num_args() ? $this->toArray(1) : $this->toArray();
00204 
00205     if(count($array) == 1)
00206     {
00207       return array_shift($array);
00208     }
00209 
00210     return $array;
00211   }
00212 
00213   /**
00214    * Returns an array containing stdClass objects.
00215    *
00216    * @return ArrayObject
00217    */
00218   public function toObjectArray()
00219   {
00220     $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
00221 
00222     for($i = 0; $i < count($array); $i++)
00223     {
00224       $array[$i] = (object) $array[$i];
00225     }
00226 
00227     return $array;
00228   }
00229 
00230   /**
00231    * Returns the command used to get this reply.
00232    *
00233    * @return TeamSpeak3_Helper_String
00234    */
00235   public function getCommandString()
00236   {
00237     return new TeamSpeak3_Helper_String($this->cmd);
00238   }
00239 
00240   /**
00241    * Returns an array of events that occured before or during this reply.
00242    *
00243    * @return array
00244    */
00245   public function getNotifyEvents()
00246   {
00247     return $this->evt;
00248   }
00249 
00250   /**
00251    * Returns the value for a specified error property.
00252    *
00253    * @param  string $ident
00254    * @param  mixed  $default
00255    * @return mixed
00256    */
00257   public function getErrorProperty($ident, $default = null)
00258   {
00259     return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
00260   }
00261 
00262   /**
00263    * Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if
00264    * there's an error.
00265    *
00266    * @param  string $err
00267    * @throws TeamSpeak3_Adapter_ServerQuery_Exception
00268    * @return void
00269    */
00270   protected function fetchError($err)
00271   {
00272     $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3);
00273 
00274     foreach($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair)
00275     {
00276       list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR);
00277 
00278       $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
00279     }
00280 
00281     TeamSpeak3_Helper_Signal::getInstance()->emit("notifyError", $this);
00282 
00283     if($this->getErrorProperty("id", 0x00) != 0x00)
00284     {
00285       if($permid = $this->getErrorProperty("failed_permid"))
00286       {
00287         try
00288         {
00289           $suffix = " (failed on " . $this->con->permissionGetNameById($permid) . ")";
00290         }
00291         catch(TeamSpeak3_Adapter_ServerQuery_Exception $e)
00292         {
00293           $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
00294         }
00295       }
00296       elseif($details = $this->getErrorProperty("extra_msg"))
00297       {
00298         $suffix = " (" . $details . ")";
00299       }
00300       else
00301       {
00302         $suffix = "";
00303       }
00304 
00305       throw new TeamSpeak3_Adapter_ServerQuery_Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"));
00306     }
00307   }
00308 
00309   /**
00310    * Parses a ServerQuery reply and creates a TeamSpeak3_Helper_String object.
00311    *
00312    * @param  string $rpl
00313    * @return void
00314    */
00315   protected function fetchReply($rpl)
00316   {
00317     foreach($rpl as $key => $val)
00318     {
00319       if($val->startsWith(TeamSpeak3::GREET))
00320       {
00321         unset($rpl[$key]);
00322       }
00323       elseif($val->startsWith(TeamSpeak3::EVENT))
00324       {
00325         $this->evt[] = new TeamSpeak3_Adapter_ServerQuery_Event($rpl[$key], $this->con);
00326         unset($rpl[$key]);
00327       }
00328     }
00329 
00330     $this->rpl = new TeamSpeak3_Helper_String(implode(TeamSpeak3::SEPARATOR_LIST, $rpl));
00331   }
00332 }
 All Classes Files Functions Variables