TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Char.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Char.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_Helper_Char
30  * @brief Helper class for char handling.
31  */
33 {
34  /**
35  * Stores the original character.
36  *
37  * @var string
38  */
39  protected $char = null;
40 
41  /**
42  * The TeamSpeak3_Helper_Char constructor.
43  *
44  * @param string $var
45  * @throws TeamSpeak3_Helper_Exception
46  * @return TeamSpeak3_Helper_Char
47  */
48  public function __construct($char)
49  {
50  if(strlen($char) != 1)
51  {
52  throw new TeamSpeak3_Helper_Exception("char parameter may not contain more or less than one character");
53  }
54 
55  $this->char = strval($char);
56  }
57 
58  /**
59  * Returns true if the character is a letter.
60  *
61  * @return boolean
62  */
63  public function isLetter()
64  {
65  return ctype_alpha($this->char);
66  }
67 
68  /**
69  * Returns true if the character is a decimal digit.
70  *
71  * @return boolean
72  */
73  public function isDigit()
74  {
75  return ctype_digit($this->char);
76  }
77 
78  /**
79  * Returns true if the character is a space.
80  *
81  * @return boolean
82  */
83  public function isSpace()
84  {
85  return ctype_space($this->char);
86  }
87 
88  /**
89  * Returns true if the character is a mark.
90  *
91  * @return boolean
92  */
93  public function isMark()
94  {
95  return ctype_punct($this->char);
96  }
97 
98  /**
99  * Returns true if the character is a control character (i.e. "\t").
100  *
101  * @return boolean
102  */
103  public function isControl()
104  {
105  return ctype_cntrl($this->char);
106  }
107 
108  /**
109  * Returns true if the character is a printable character.
110  *
111  * @return boolean
112  */
113  public function isPrintable()
114  {
115  return ctype_print($this->char);
116  }
117 
118  /**
119  * Returns true if the character is the Unicode character 0x0000 ("\0").
120  *
121  * @return boolean
122  */
123  public function isNull()
124  {
125  return ($this->char === "\0") ? TRUE : FALSE;
126  }
127 
128  /**
129  * Returns true if the character is an uppercase letter.
130  *
131  * @return boolean
132  */
133  public function isUpper()
134  {
135  return ($this->char === strtoupper($this->char)) ? TRUE : FALSE;
136  }
137 
138  /**
139  * Returns true if the character is a lowercase letter.
140  *
141  * @return boolean
142  */
143  public function isLower()
144  {
145  return ($this->char === strtolower($this->char)) ? TRUE : FALSE;
146  }
147 
148  /**
149  * Returns the uppercase equivalent if the character is lowercase.
150  *
151  * @return TeamSpeak3_Helper_Char
152  */
153  public function toUpper()
154  {
155  return ($this->isUpper()) ? $this : new self(strtoupper($this));
156  }
157 
158  /**
159  * Returns the lowercase equivalent if the character is uppercase.
160  *
161  * @return TeamSpeak3_Helper_Char
162  */
163  public function toLower()
164  {
165  return ($this->isLower()) ? $this : new self(strtolower($this));
166  }
167 
168  /**
169  * Returns the ascii value of the character.
170  *
171  * @return integer
172  */
173  public function toAscii()
174  {
175  return ord($this->char);
176  }
177 
178  /**
179  * Returns the Unicode value of the character.
180  *
181  * @return integer
182  */
183  public function toUnicode()
184  {
185  $h = ord($this->char{0});
186 
187  if($h <= 0x7F)
188  {
189  return $h;
190  }
191  else if($h < 0xC2)
192  {
193  return FALSE;
194  }
195  else if($h <= 0xDF)
196  {
197  return ($h & 0x1F) << 6 | (ord($this->char{1}) & 0x3F);
198  }
199  else if($h <= 0xEF)
200  {
201  return ($h & 0x0F) << 12 | (ord($this->char{1}) & 0x3F) << 6 | (ord($this->char{2}) & 0x3F);
202  }
203  else if($h <= 0xF4)
204  {
205  return ($h & 0x0F) << 18 | (ord($this->char{1}) & 0x3F) << 12 | (ord($this->char{2}) & 0x3F) << 6 | (ord($this->char{3}) & 0x3F);
206  }
207  else
208  {
209  return FALSE;
210  }
211  }
212 
213  /**
214  * Returns the hexadecimal value of the char.
215  *
216  * @return string
217  */
218  public function toHex()
219  {
220  return strtoupper(dechex($this->toAscii()));
221  }
222 
223  /**
224  * Returns the TeamSpeak3_Helper_Char based on a given hex value.
225  *
226  * @param string $hex
227  * @throws TeamSpeak3_Helper_Exception
228  * @return TeamSpeak3_Helper_Char
229  */
230  public static function fromHex($hex)
231  {
232  if(strlen($hex) != 2)
233  {
234  throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
235  }
236 
237  return new self(chr(hexdec($hex)));
238  }
239 
240  /**
241  * Returns the character as a standard string.
242  *
243  * @return string
244  */
245  public function toString()
246  {
247  return $this->char;
248  }
249 
250  /**
251  * Returns the integer value of the character.
252  *
253  * @return integer
254  */
255  public function toInt()
256  {
257  return intval($this->char);
258  }
259 
260  /**
261  * Returns the character as a standard string.
262  *
263  * @return string
264  */
265  public function __toString()
266  {
267  return $this->char;
268  }
269 }