TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Exception.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Exception.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_Exception
30  * @brief Enhanced exception class for TeamSpeak3 objects.
31  */
32 class TeamSpeak3_Exception extends Exception
33 {
34  /**
35  * Stores custom error messages.
36  *
37  * @var array
38  */
39  protected static $messages = array();
40 
41  /**
42  * The TeamSpeak3_Exception constructor.
43  *
44  * @param string $mesg
45  * @param integer $code
46  * @return TeamSpeak3_Exception
47  */
48  public function __construct($mesg, $code = 0x00)
49  {
50  parent::__construct($mesg, $code);
51 
52  if(array_key_exists((int) $code, self::$messages))
53  {
54  $this->message = $this->prepareCustomMessage(self::$messages[intval($code)]);
55  }
56 
57  TeamSpeak3_Helper_Signal::getInstance()->emit("errorException", $this);
58  }
59 
60  /**
61  * Prepares a custom error message by replacing pre-defined signs with given values.
62  *
63  * @param TeamSpeak3_Helper_String $mesg
64  * @return TeamSpeak3_Helper_String
65  */
67  {
68  $args = array(
69  "code" => $this->getCode(),
70  "mesg" => $this->getMessage(),
71  "line" => $this->getLine(),
72  "file" => $this->getFile(),
73  );
74 
75  return $mesg->arg($args)->toString();
76  }
77 
78  /**
79  * Registers a custom error message to $code.
80  *
81  * @param integer $code
82  * @param string $mesg
83  * @throws TeamSpeak3_Exception
84  * @return void
85  */
86  public static function registerCustomMessage($code, $mesg)
87  {
88  if(array_key_exists((int) $code, self::$messages))
89  {
90  throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
91  }
92 
93  if(!is_string($mesg))
94  {
95  throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " must be a string");
96  }
97 
98  self::$messages[(int) $code] = new TeamSpeak3_Helper_String($mesg);
99  }
100 
101  /**
102  * Unregisters a custom error message from $code.
103  *
104  * @param integer $code
105  * @throws TeamSpeak3_Exception
106  * @return void
107  */
108  public static function unregisterCustomMessage($code)
109  {
110  if(!array_key_exists((int) $code, self::$messages))
111  {
112  throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
113  }
114 
115  unset(self::$messages[intval($code)]);
116  }
117 
118  /**
119  * Returns the class from which the exception was thrown.
120  *
121  * @return string
122  */
123  public function getSender()
124  {
125  $trace = $this->getTrace();
126 
127  return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
128  }
129 }