1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Codepoint; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Unicode\Codepoint\Range\RangeRegexBuilder; |
7
|
|
|
use UCD\Unicode\Collection\TraversableBackedCollection; |
8
|
|
|
use UCD\Unicode\TransformationFormat; |
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 fromUTF16($string) |
83
|
|
|
{ |
84
|
|
|
return self::fromEncodedString( |
85
|
|
|
$string, |
86
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $string |
92
|
|
|
* @return static |
93
|
|
|
*/ |
94
|
|
|
public static function fromUTF32($string) |
95
|
|
|
{ |
96
|
|
|
return self::fromEncodedString( |
97
|
|
|
$string, |
98
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $string |
104
|
|
|
* @param TransformationFormat $encoding |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public static function fromEncodedString($string, TransformationFormat $encoding) |
108
|
|
|
{ |
109
|
|
|
$characters = TransformationFormat\StringUtility::split($string, $encoding); |
110
|
|
|
|
111
|
|
|
$mapper = function ($character) use ($encoding) { |
112
|
|
|
return Codepoint::fromEncodedCharacter($character, $encoding); |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
return static::fromArray( |
116
|
|
|
array_map($mapper, $characters) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function toUTF8() |
124
|
|
|
{ |
125
|
|
|
return $this->toEncodedString( |
126
|
|
|
TransformationFormat::ofType(TransformationFormat::EIGHT) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function toUTF16() |
134
|
|
|
{ |
135
|
|
|
return $this->toEncodedString( |
136
|
|
|
TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function toUTF32() |
144
|
|
|
{ |
145
|
|
|
return $this->toEncodedString( |
146
|
|
|
TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param TransformationFormat $encoding |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function toEncodedString(TransformationFormat $encoding) |
155
|
|
|
{ |
156
|
|
|
$characters = ''; |
157
|
|
|
|
158
|
|
|
$this->traverseWith(function (Codepoint $codepoint) use ($encoding, &$characters) { |
159
|
|
|
$characters .= $codepoint->toEncodedCharacter($encoding); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
return $characters; |
163
|
|
|
} |
164
|
|
|
} |