1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode; |
4
|
|
|
|
5
|
|
|
use UCD\Exception\InvalidArgumentException; |
6
|
|
|
use UCD\Exception\OutOfRangeException; |
7
|
|
|
use UCD\Unicode\TransformationFormat\StringUtility; |
8
|
|
|
|
9
|
|
|
class Codepoint implements Comparable |
10
|
|
|
{ |
11
|
|
|
const MIN = 0x0; |
12
|
|
|
const MAX = 0x10FFFF; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $value; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param int $value |
21
|
|
|
* @throws OutOfRangeException |
22
|
|
|
* @throws InvalidArgumentException |
23
|
|
|
*/ |
24
|
|
|
private function __construct($value) |
25
|
|
|
{ |
26
|
|
|
if (!is_int($value)) { |
27
|
|
|
throw new InvalidArgumentException('Codepoint value must be an integer'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($value < self::MIN || $value > self::MAX) { |
31
|
|
|
throw new OutOfRangeException('Codepoint value must reside between 0x0 and 0x10FFFF'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->value = $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $value |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public static function fromHex($value) |
42
|
|
|
{ |
43
|
|
|
return self::fromInt(hexdec($value)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int $value |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
|
|
public static function fromInt($value) |
51
|
|
|
{ |
52
|
|
|
return new self($value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $value |
57
|
|
|
* @return self |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public static function fromUTF8($value) |
61
|
|
|
{ |
62
|
|
|
return self::fromEncodedCharacter( |
63
|
|
|
$value, |
64
|
|
|
TransformationFormat::ofType(TransformationFormat::EIGHT) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $value |
70
|
|
|
* @return self |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
public static function fromUTF16LE($value) |
74
|
|
|
{ |
75
|
|
|
return self::fromEncodedCharacter( |
76
|
|
|
$value, |
77
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_LITTLE_ENDIAN) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $value |
83
|
|
|
* @return self |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public static function fromUTF16BE($value) |
87
|
|
|
{ |
88
|
|
|
return self::fromEncodedCharacter( |
89
|
|
|
$value, |
90
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $value |
96
|
|
|
* @return self |
97
|
|
|
* @throws InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public static function fromUTF32LE($value) |
100
|
|
|
{ |
101
|
|
|
return self::fromEncodedCharacter( |
102
|
|
|
$value, |
103
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $value |
109
|
|
|
* @return self |
110
|
|
|
* @throws InvalidArgumentException |
111
|
|
|
*/ |
112
|
|
|
public static function fromUTF32BE($value) |
113
|
|
|
{ |
114
|
|
|
return self::fromEncodedCharacter( |
115
|
|
|
$value, |
116
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $character |
122
|
|
|
* @param TransformationFormat $convertFrom |
123
|
|
|
* @return self |
124
|
|
|
* @throws InvalidArgumentException |
125
|
|
|
*/ |
126
|
|
|
public static function fromEncodedCharacter($character, TransformationFormat $convertFrom) |
127
|
|
|
{ |
128
|
|
|
$convertTo = TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN); |
129
|
|
|
$character = StringUtility::convertCharacter($character, $convertFrom, $convertTo); |
130
|
|
|
$unpacked = unpack('N', $character); |
131
|
|
|
|
132
|
|
|
return self::fromInt( |
133
|
|
|
array_shift($unpacked) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function toUTF8() |
141
|
|
|
{ |
142
|
|
|
return $this->toEncodedCharacter( |
143
|
|
|
TransformationFormat::ofType(TransformationFormat::EIGHT) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function toUTF16LE() |
151
|
|
|
{ |
152
|
|
|
return $this->toEncodedCharacter( |
153
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_LITTLE_ENDIAN) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function toUTF16BE() |
161
|
|
|
{ |
162
|
|
|
return $this->toEncodedCharacter( |
163
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function toUTF32LE() |
171
|
|
|
{ |
172
|
|
|
return $this->toEncodedCharacter( |
173
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function toUTF32BE() |
181
|
|
|
{ |
182
|
|
|
return $this->toEncodedCharacter( |
183
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN) |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param TransformationFormat $convertTo |
189
|
|
|
* @return string |
190
|
|
|
* @throws InvalidArgumentException |
191
|
|
|
*/ |
192
|
|
|
public function toEncodedCharacter(TransformationFormat $convertTo) |
193
|
|
|
{ |
194
|
|
|
$character = pack('N', $this->value); |
195
|
|
|
$convertFrom = TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN); |
196
|
|
|
|
197
|
|
|
return StringUtility::convertCharacter($character, $convertFrom, $convertTo); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
|
|
public function getValue() |
204
|
|
|
{ |
205
|
|
|
return $this->value; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param mixed $other |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function equals($other) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
if ($this === $other) { |
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $other instanceof self |
219
|
|
|
&& $this->value === $other->value; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function __toString() |
226
|
|
|
{ |
227
|
|
|
return sprintf('U+%X', $this->value); |
228
|
|
|
} |
229
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.