Char.php 5.3 KB

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