1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Codepoint; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Unicode\Collection\TraversableBackedCollection; |
7
|
|
|
use UCD\Unicode\TransformationFormat; |
8
|
|
|
use UCD\Unicode\TransformationFormat\StringUtility; |
9
|
|
|
|
10
|
|
|
class Collection extends TraversableBackedCollection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @return \Traversable|int[] |
14
|
|
|
*/ |
15
|
|
|
public function flatten() |
16
|
|
|
{ |
17
|
|
|
/** @var Codepoint $codepoint */ |
18
|
|
|
foreach ($this as $codepoint) { |
19
|
|
|
yield $codepoint->getValue(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Codepoint $codepoint |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function has(Codepoint $codepoint) |
28
|
|
|
{ |
29
|
|
|
foreach ($this as $check) { |
30
|
|
|
if ($codepoint->equals($check)) { |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return Range[]|Range\Collection |
40
|
|
|
*/ |
41
|
|
|
public function aggregate() |
42
|
|
|
{ |
43
|
|
|
$aggregator = new Aggregator(); |
44
|
|
|
|
45
|
|
|
$this->traverseWith(function (Codepoint $codepoint) use ($aggregator) { |
46
|
|
|
$aggregator->addCodepoint($codepoint); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
return $aggregator->getAggregated(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function toRegexCharacterClass() |
56
|
|
|
{ |
57
|
|
|
$builder = new RegexBuilder(); |
58
|
|
|
|
59
|
|
|
$this->traverseWith(function (Codepoint $codepoint) use ($builder) { |
60
|
|
|
$builder->addCodepoint($codepoint); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
return $builder->getCharacterClass(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $string |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
|
|
public static function fromUTF8($string) |
71
|
|
|
{ |
72
|
|
|
return self::fromEncodedString( |
73
|
|
|
$string, |
74
|
|
|
TransformationFormat::ofType(TransformationFormat::EIGHT) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $string |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
|
|
public static function fromUTF16LE($string) |
83
|
|
|
{ |
84
|
|
|
return self::fromEncodedString( |
85
|
|
|
$string, |
86
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_LITTLE_ENDIAN) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $string |
92
|
|
|
* @return static |
93
|
|
|
*/ |
94
|
|
|
public static function fromUTF16BE($string) |
95
|
|
|
{ |
96
|
|
|
return self::fromEncodedString( |
97
|
|
|
$string, |
98
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $string |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
|
|
public static function fromUTF32LE($string) |
107
|
|
|
{ |
108
|
|
|
return self::fromEncodedString( |
109
|
|
|
$string, |
110
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $string |
116
|
|
|
* @return static |
117
|
|
|
*/ |
118
|
|
|
public static function fromUTF32BE($string) |
119
|
|
|
{ |
120
|
|
|
return self::fromEncodedString( |
121
|
|
|
$string, |
122
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $string |
128
|
|
|
* @param TransformationFormat $encoding |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public static function fromEncodedString($string, TransformationFormat $encoding) |
132
|
|
|
{ |
133
|
|
|
$characters = StringUtility::split($string, $encoding); |
134
|
|
|
|
135
|
|
|
$mapper = function ($character) use ($encoding) { |
136
|
|
|
return Codepoint::fromEncodedCharacter($character, $encoding); |
137
|
|
|
}; |
138
|
|
|
|
139
|
|
|
return static::fromArray( |
140
|
|
|
array_map($mapper, $characters) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function toUTF8() |
148
|
|
|
{ |
149
|
|
|
return $this->toEncodedString( |
150
|
|
|
TransformationFormat::ofType(TransformationFormat::EIGHT) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function toUTF16LE() |
158
|
|
|
{ |
159
|
|
|
return $this->toEncodedString( |
160
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_LITTLE_ENDIAN) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function toUTF16BE() |
168
|
|
|
{ |
169
|
|
|
return $this->toEncodedString( |
170
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function toUTF32LE() |
178
|
|
|
{ |
179
|
|
|
return $this->toEncodedString( |
180
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function toUTF32BE() |
188
|
|
|
{ |
189
|
|
|
return $this->toEncodedString( |
190
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN) |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param TransformationFormat $convertTo |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function toEncodedString(TransformationFormat $convertTo) |
199
|
|
|
{ |
200
|
|
|
$characters = ''; |
201
|
|
|
|
202
|
|
|
$this->traverseWith(function (Codepoint $codepoint) use ($convertTo, &$characters) { |
203
|
|
|
$characters .= $codepoint->toEncodedCharacter($convertTo); |
204
|
|
|
}); |
205
|
|
|
|
206
|
|
|
return $characters; |
207
|
|
|
} |
208
|
|
|
} |