Char.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Char.php 06/06/2016 22:27:13 scp@Svens-iMac $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.24
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. /**
  27. * @class TeamSpeak3_Helper_Char
  28. * @brief Helper class for char handling.
  29. */
  30. class TeamSpeak3_Helper_Char
  31. {
  32. /**
  33. * Stores the original character.
  34. *
  35. * @var string
  36. */
  37. protected $char = null;
  38. /**
  39. * The TeamSpeak3_Helper_Char constructor.
  40. *
  41. * @param string $var
  42. * @throws TeamSpeak3_Helper_Exception
  43. * @return TeamSpeak3_Helper_Char
  44. */
  45. public function __construct($char)
  46. {
  47. if(strlen($char) != 1)
  48. {
  49. throw new TeamSpeak3_Helper_Exception("char parameter may not contain more or less than one character");
  50. }
  51. $this->char = strval($char);
  52. }
  53. /**
  54. * Returns true if the character is a letter.
  55. *
  56. * @return boolean
  57. */
  58. public function isLetter()
  59. {
  60. return ctype_alpha($this->char);
  61. }
  62. /**
  63. * Returns true if the character is a decimal digit.
  64. *
  65. * @return boolean
  66. */
  67. public function isDigit()
  68. {
  69. return ctype_digit($this->char);
  70. }
  71. /**
  72. * Returns true if the character is a space.
  73. *
  74. * @return boolean
  75. */
  76. public function isSpace()
  77. {
  78. return ctype_space($this->char);
  79. }
  80. /**
  81. * Returns true if the character is a mark.
  82. *
  83. * @return boolean
  84. */
  85. public function isMark()
  86. {
  87. return ctype_punct($this->char);
  88. }
  89. /**
  90. * Returns true if the character is a control character (i.e. "\t").
  91. *
  92. * @return boolean
  93. */
  94. public function isControl()
  95. {
  96. return ctype_cntrl($this->char);
  97. }
  98. /**
  99. * Returns true if the character is a printable character.
  100. *
  101. * @return boolean
  102. */
  103. public function isPrintable()
  104. {
  105. return ctype_print($this->char);
  106. }
  107. /**
  108. * Returns true if the character is the Unicode character 0x0000 ("\0").
  109. *
  110. * @return boolean
  111. */
  112. public function isNull()
  113. {
  114. return ($this->char === "\0") ? TRUE : FALSE;
  115. }
  116. /**
  117. * Returns true if the character is an uppercase letter.
  118. *
  119. * @return boolean
  120. */
  121. public function isUpper()
  122. {
  123. return ($this->char === strtoupper($this->char)) ? TRUE : FALSE;
  124. }
  125. /**
  126. * Returns true if the character is a lowercase letter.
  127. *
  128. * @return boolean
  129. */
  130. public function isLower()
  131. {
  132. return ($this->char === strtolower($this->char)) ? TRUE : FALSE;
  133. }
  134. /**
  135. * Returns the uppercase equivalent if the character is lowercase.
  136. *
  137. * @return TeamSpeak3_Helper_Char
  138. */
  139. public function toUpper()
  140. {
  141. return ($this->isUpper()) ? $this : new self(strtoupper($this));
  142. }
  143. /**
  144. * Returns the lowercase equivalent if the character is uppercase.
  145. *
  146. * @return TeamSpeak3_Helper_Char
  147. */
  148. public function toLower()
  149. {
  150. return ($this->isLower()) ? $this : new self(strtolower($this));
  151. }
  152. /**
  153. * Returns the ascii value of the character.
  154. *
  155. * @return integer
  156. */
  157. public function toAscii()
  158. {
  159. return ord($this->char);
  160. }
  161. /**
  162. * Returns the Unicode value of the character.
  163. *
  164. * @return integer
  165. */
  166. public function toUnicode()
  167. {
  168. $h = ord($this->char{0});
  169. if($h <= 0x7F)
  170. {
  171. return $h;
  172. }
  173. else if($h < 0xC2)
  174. {
  175. return FALSE;
  176. }
  177. else if($h <= 0xDF)
  178. {
  179. return ($h & 0x1F) << 6 | (ord($this->char{1}) & 0x3F);
  180. }
  181. else if($h <= 0xEF)
  182. {
  183. return ($h & 0x0F) << 12 | (ord($this->char{1}) & 0x3F) << 6 | (ord($this->char{2}) & 0x3F);
  184. }
  185. else if($h <= 0xF4)
  186. {
  187. return ($h & 0x0F) << 18 | (ord($this->char{1}) & 0x3F) << 12 | (ord($this->char{2}) & 0x3F) << 6 | (ord($this->char{3}) & 0x3F);
  188. }
  189. else
  190. {
  191. return FALSE;
  192. }
  193. }
  194. /**
  195. * Returns the hexadecimal value of the char.
  196. *
  197. * @return string
  198. */
  199. public function toHex()
  200. {
  201. return strtoupper(dechex($this->toAscii()));
  202. }
  203. /**
  204. * Returns the TeamSpeak3_Helper_Char based on a given hex value.
  205. *
  206. * @param string $hex
  207. * @throws TeamSpeak3_Helper_Exception
  208. * @return TeamSpeak3_Helper_Char
  209. */
  210. public static function fromHex($hex)
  211. {
  212. if(strlen($hex) != 2)
  213. {
  214. throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
  215. }
  216. return new self(chr(hexdec($hex)));
  217. }
  218. /**
  219. * Returns the character as a standard string.
  220. *
  221. * @return string
  222. */
  223. public function toString()
  224. {
  225. return $this->char;
  226. }
  227. /**
  228. * Returns the integer value of the character.
  229. *
  230. * @return integer
  231. */
  232. public function toInt()
  233. {
  234. return intval($this->char);
  235. }
  236. /**
  237. * Returns the character as a standard string.
  238. *
  239. * @return string
  240. */
  241. public function __toString()
  242. {
  243. return $this->char;
  244. }
  245. }