ts3phpframework
Loading...
Searching...
No Matches
Char.php
Go to the documentation of this file.
1<?php
2
4
6
13class Char
14{
20 protected string $char;
21
28 public function __construct(string $char)
29 {
30 if (strlen($char) != 1) {
31 throw new HelperException("char parameter may not contain more or less than one character");
32 }
33
34 $this->char = $char;
35 }
36
42 public function isLetter(): bool
43 {
44 return ctype_alpha($this->char);
45 }
46
52 public function isDigit(): bool
53 {
54 return ctype_digit($this->char);
55 }
56
62 public function isSpace(): bool
63 {
64 return ctype_space($this->char);
65 }
66
72 public function isMark(): bool
73 {
74 return ctype_punct($this->char);
75 }
76
82 public function isControl(): bool
83 {
84 return ctype_cntrl($this->char);
85 }
86
92 public function isPrintable(): bool
93 {
94 return ctype_print($this->char);
95 }
96
102 public function isNull(): bool
103 {
104 return $this->char === "\0";
105 }
106
112 public function isUpper(): bool
113 {
114 return $this->char === strtoupper($this->char);
115 }
116
122 public function isLower(): bool
123 {
124 return $this->char === strtolower($this->char);
125 }
126
133 public function toUpper(): Char
134 {
135 return ($this->isUpper()) ? $this : new self(strtoupper($this));
136 }
137
144 public function toLower(): Char
145 {
146 return ($this->isLower()) ? $this : new self(strtolower($this));
147 }
148
154 public function toAscii(): int
155 {
156 return ord($this->char);
157 }
158
164 public function toUnicode(): int
165 {
166 $h = ord($this->char[0]);
167
168 if ($h <= 0x7F) {
169 return $h;
170 } elseif ($h < 0xC2) {
171 return false;
172 } elseif ($h <= 0xDF) {
173 return ($h & 0x1F) << 6 | (ord($this->char[1]) & 0x3F);
174 } elseif ($h <= 0xEF) {
175 return ($h & 0x0F) << 12 | (ord($this->char[1]) & 0x3F) << 6 | (ord($this->char[2]) & 0x3F);
176 } elseif ($h <= 0xF4) {
177 return ($h & 0x0F) << 18 | (ord($this->char[1]) & 0x3F) << 12 | (ord($this->char[2]) & 0x3F) << 6 | (ord($this->char[3]) & 0x3F);
178 } else {
179 return -1;
180 }
181 }
182
188 public function toHex(): string
189 {
190 return strtoupper(dechex($this->toAscii()));
191 }
192
200 public static function fromHex(string $hex): Char
201 {
202 if (strlen($hex) != 2) {
203 throw new HelperException("given parameter '" . $hex . "' is not a valid hexadecimal number");
204 }
205
206 return new self(chr(hexdec($hex)));
207 }
208
214 public function toString(): string
215 {
216 return $this->char;
217 }
218
224 public function toInt(): int
225 {
226 return intval($this->char);
227 }
228
234 public function __toString()
235 {
236 return $this->char;
237 }
238}
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Helper* objects.
Helper class for char handling.
Definition Char.php:14