TeamSpeak 3 PHP Framework
1.1.12
|
00001 <?php 00002 00003 /** 00004 * @file 00005 * TeamSpeak 3 PHP Framework 00006 * 00007 * $Id: Char.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_Helper_Char 00030 * @brief Helper class for char handling. 00031 */ 00032 class TeamSpeak3_Helper_Char 00033 { 00034 /** 00035 * Stores the original character. 00036 * 00037 * @var string 00038 */ 00039 protected $char = null; 00040 00041 /** 00042 * The TeamSpeak3_Helper_Char constructor. 00043 * 00044 * @param string $var 00045 * @throws TeamSpeak3_Helper_Exception 00046 * @return TeamSpeak3_Helper_Char 00047 */ 00048 public function __construct($char) 00049 { 00050 if(strlen($char) != 1) 00051 { 00052 throw new TeamSpeak3_Helper_Exception("char parameter may not contain more or less than one character"); 00053 } 00054 00055 $this->char = strval($char); 00056 } 00057 00058 /** 00059 * Returns true if the character is a letter. 00060 * 00061 * @return boolean 00062 */ 00063 public function isLetter() 00064 { 00065 return ctype_alpha($this->char); 00066 } 00067 00068 /** 00069 * Returns true if the character is a decimal digit. 00070 * 00071 * @return boolean 00072 */ 00073 public function isDigit() 00074 { 00075 return ctype_digit($this->char); 00076 } 00077 00078 /** 00079 * Returns true if the character is a space. 00080 * 00081 * @return boolean 00082 */ 00083 public function isSpace() 00084 { 00085 return ctype_space($this->char); 00086 } 00087 00088 /** 00089 * Returns true if the character is a mark. 00090 * 00091 * @return boolean 00092 */ 00093 public function isMark() 00094 { 00095 return ctype_punct($this->char); 00096 } 00097 00098 /** 00099 * Returns true if the character is a control character (i.e. "\t"). 00100 * 00101 * @return boolean 00102 */ 00103 public function isControl() 00104 { 00105 return ctype_cntrl($this->char); 00106 } 00107 00108 /** 00109 * Returns true if the character is a printable character. 00110 * 00111 * @return boolean 00112 */ 00113 public function isPrintable() 00114 { 00115 return ctype_print($this->char); 00116 } 00117 00118 /** 00119 * Returns true if the character is the Unicode character 0x0000 ("\0"). 00120 * 00121 * @return boolean 00122 */ 00123 public function isNull() 00124 { 00125 return ($this->char === "\0") ? TRUE : FALSE; 00126 } 00127 00128 /** 00129 * Returns true if the character is an uppercase letter. 00130 * 00131 * @return boolean 00132 */ 00133 public function isUpper() 00134 { 00135 return ($this->char === strtoupper($this->char)) ? TRUE : FALSE; 00136 } 00137 00138 /** 00139 * Returns true if the character is a lowercase letter. 00140 * 00141 * @return boolean 00142 */ 00143 public function isLower() 00144 { 00145 return ($this->char === strtolower($this->char)) ? TRUE : FALSE; 00146 } 00147 00148 /** 00149 * Returns the uppercase equivalent if the character is lowercase. 00150 * 00151 * @return TeamSpeak3_Helper_Char 00152 */ 00153 public function toUpper() 00154 { 00155 return ($this->isUpper()) ? $this : new self(strtoupper($this)); 00156 } 00157 00158 /** 00159 * Returns the lowercase equivalent if the character is uppercase. 00160 * 00161 * @return TeamSpeak3_Helper_Char 00162 */ 00163 public function toLower() 00164 { 00165 return ($this->isLower()) ? $this : new self(strtolower($this)); 00166 } 00167 00168 /** 00169 * Returns the ascii value of the character. 00170 * 00171 * @return integer 00172 */ 00173 public function toAscii() 00174 { 00175 return ord($this->char); 00176 } 00177 00178 /** 00179 * Returns the Unicode value of the character. 00180 * 00181 * @return integer 00182 */ 00183 public function toUnicode() 00184 { 00185 $h = ord($this->char{0}); 00186 00187 if($h <= 0x7F) 00188 { 00189 return $h; 00190 } 00191 else if($h < 0xC2) 00192 { 00193 return FALSE; 00194 } 00195 else if($h <= 0xDF) 00196 { 00197 return ($h & 0x1F) << 6 | (ord($this->char{1}) & 0x3F); 00198 } 00199 else if($h <= 0xEF) 00200 { 00201 return ($h & 0x0F) << 12 | (ord($this->char{1}) & 0x3F) << 6 | (ord($this->char{2}) & 0x3F); 00202 } 00203 else if($h <= 0xF4) 00204 { 00205 return ($h & 0x0F) << 18 | (ord($this->char{1}) & 0x3F) << 12 | (ord($this->char{2}) & 0x3F) << 6 | (ord($this->char{3}) & 0x3F); 00206 } 00207 else 00208 { 00209 return FALSE; 00210 } 00211 } 00212 00213 /** 00214 * Returns the hexadecimal value of the char. 00215 * 00216 * @return string 00217 */ 00218 public function toHex() 00219 { 00220 return strtoupper(dechex($this->toAscii())); 00221 } 00222 00223 /** 00224 * Returns the TeamSpeak3_Helper_Char based on a given hex value. 00225 * 00226 * @param string $hex 00227 * @throws TeamSpeak3_Helper_Exception 00228 * @return TeamSpeak3_Helper_Char 00229 */ 00230 public static function fromHex($hex) 00231 { 00232 if(strlen($hex) != 2) 00233 { 00234 throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number"); 00235 } 00236 00237 return new self(chr(hexdec($hex))); 00238 } 00239 00240 /** 00241 * Returns the character as a standard string. 00242 * 00243 * @return string 00244 */ 00245 public function toString() 00246 { 00247 return $this->char; 00248 } 00249 00250 /** 00251 * Returns the integer value of the character. 00252 * 00253 * @return integer 00254 */ 00255 public function toInt() 00256 { 00257 return intval($this->char); 00258 } 00259 00260 /** 00261 * Returns the character as a standard string. 00262 * 00263 * @return string 00264 */ 00265 public function __toString() 00266 { 00267 return $this->char; 00268 } 00269 }