TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Exception.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: Exception.php 2/18/2012 12:42:46 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_Exception
00030  * @brief Enhanced exception class for TeamSpeak3 objects.
00031  */
00032 class TeamSpeak3_Exception extends Exception
00033 {
00034   /**
00035    * Stores custom error messages.
00036    *
00037    * @var array
00038    */
00039   protected static $messages = array();
00040 
00041   /**
00042    * The TeamSpeak3_Exception constructor.
00043    *
00044    * @param  string  $mesg
00045    * @param  integer $code
00046    * @return TeamSpeak3_Exception
00047    */
00048   public function __construct($mesg, $code = 0x00)
00049   {
00050     parent::__construct($mesg, $code);
00051 
00052     if(array_key_exists((int) $code, self::$messages))
00053     {
00054       $this->message = $this->prepareCustomMessage(self::$messages[intval($code)]);
00055     }
00056 
00057     TeamSpeak3_Helper_Signal::getInstance()->emit("errorException", $this);
00058   }
00059 
00060   /**
00061    * Prepares a custom error message by replacing pre-defined signs with given values.
00062    *
00063    * @param  TeamSpeak3_Helper_String $mesg
00064    * @return TeamSpeak3_Helper_String
00065    */
00066   protected function prepareCustomMessage(TeamSpeak3_Helper_String $mesg)
00067   {
00068     $args = array(
00069       "code" => $this->getCode(),
00070       "mesg" => $this->getMessage(),
00071       "line" => $this->getLine(),
00072       "file" => $this->getFile(),
00073     );
00074 
00075     return $mesg->arg($args)->toString();
00076   }
00077 
00078   /**
00079    * Registers a custom error message to $code.
00080    *
00081    * @param  integer $code
00082    * @param  string  $mesg
00083    * @throws TeamSpeak3_Exception
00084    * @return void
00085    */
00086   public static function registerCustomMessage($code, $mesg)
00087   {
00088     if(array_key_exists((int) $code, self::$messages))
00089     {
00090       throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
00091     }
00092 
00093     if(!is_string($mesg))
00094     {
00095       throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " must be a string");
00096     }
00097 
00098     self::$messages[(int) $code] = new TeamSpeak3_Helper_String($mesg);
00099   }
00100 
00101   /**
00102    * Unregisters a custom error message from $code.
00103    *
00104    * @param  integer $code
00105    * @throws TeamSpeak3_Exception
00106    * @return void
00107    */
00108   public static function unregisterCustomMessage($code)
00109   {
00110     if(!array_key_exists((int) $code, self::$messages))
00111     {
00112       throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
00113     }
00114 
00115     unset(self::$messages[intval($code)]);
00116   }
00117 
00118   /**
00119    * Returns the class from which the exception was thrown.
00120    *
00121    * @return string
00122    */
00123   public function getSender()
00124   {
00125     $trace = $this->getTrace();
00126 
00127     return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
00128   }
00129 }
 All Classes Files Functions Variables