1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Character; |
6
|
|
|
use UCD\Unicode\Character\Collection; |
7
|
|
|
use UCD\Unicode\Character\Properties\General\Block; |
8
|
|
|
use UCD\Unicode\Character\Properties\General\GeneralCategory; |
9
|
|
|
use UCD\Unicode\Character\Properties\General\Script; |
10
|
|
|
use UCD\Unicode\Character\Repository; |
11
|
|
|
use UCD\Unicode\Character\Repository\CharacterNotFoundException; |
12
|
|
|
use UCD\Unicode\Codepoint; |
13
|
|
|
use UCD\Unicode\Codepoint\AggregatorRelay; |
14
|
|
|
use UCD\Unicode\CodepointAssigned; |
15
|
|
|
use UCD\Unicode\NonCharacter; |
16
|
|
|
use UCD\Unicode\Surrogate; |
17
|
|
|
|
18
|
|
|
use UCD\Exception\InvalidArgumentException; |
19
|
|
|
use UCD\Exception\OutOfRangeException; |
20
|
|
|
|
21
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\RangeFile\PHPRangeFileDirectory; |
22
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\Serializer\PHPSerializer; |
23
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository; |
24
|
|
|
|
25
|
|
|
class Database |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Repository |
29
|
|
|
*/ |
30
|
|
|
private $sourceRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Repository $sourceRepository |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Repository $sourceRepository) |
36
|
|
|
{ |
37
|
|
|
$this->sourceRepository = $sourceRepository; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return static |
42
|
|
|
*/ |
43
|
|
|
public static function fromDisk() |
44
|
|
|
{ |
45
|
|
|
return new static( |
46
|
|
|
self::createFileRepository() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Codepoint $codepoint |
52
|
|
|
* @return CodepointAssigned |
53
|
|
|
* @throws CharacterNotFoundException |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
* @throws OutOfRangeException |
56
|
|
|
*/ |
57
|
|
|
public function getByCodepoint(Codepoint $codepoint) |
58
|
|
|
{ |
59
|
|
|
return $this->sourceRepository |
60
|
|
|
->getByCodepoint($codepoint); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Codepoint\Collection $codepoints |
65
|
|
|
* @return Character\Collection|CodepointAssigned[] |
66
|
|
|
*/ |
67
|
|
|
public function getByCodepoints(Codepoint\Collection $codepoints) |
68
|
|
|
{ |
69
|
|
|
return $this->sourceRepository |
70
|
|
|
->getByCodepoints($codepoints); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Codepoint $codepoint |
75
|
|
|
* @return Character |
76
|
|
|
* @throws CharacterNotFoundException |
77
|
|
|
*/ |
78
|
|
|
public function getCharacterByCodepoint(Codepoint $codepoint) |
79
|
|
|
{ |
80
|
|
|
$assigned = $this->getByCodepoint($codepoint); |
81
|
|
|
|
82
|
|
|
if ($assigned instanceof Character) { |
83
|
|
|
return $assigned; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw CharacterNotFoundException::withCodepoint($codepoint); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Collection|CodepointAssigned[] |
91
|
|
|
*/ |
92
|
|
|
public function all() |
93
|
|
|
{ |
94
|
|
|
return $this->sourceRepository |
95
|
|
|
->getAll(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Collection|Character[] |
100
|
|
|
*/ |
101
|
|
|
public function onlyCharacters() |
102
|
|
|
{ |
103
|
|
|
return $this->all() |
104
|
|
|
->getCharacters(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Collection|NonCharacter[] |
109
|
|
|
*/ |
110
|
|
|
public function onlyNonCharacters() |
111
|
|
|
{ |
112
|
|
|
return $this->all() |
113
|
|
|
->getNonCharacters(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Collection|Surrogate[] |
118
|
|
|
*/ |
119
|
|
|
public function onlySurrogates() |
120
|
|
|
{ |
121
|
|
|
return $this->all() |
122
|
|
|
->getSurrogates(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Block $block |
127
|
|
|
* @throws Repository\BlockNotFoundException |
128
|
|
|
* @return Codepoint\Range\Collection |
129
|
|
|
*/ |
130
|
|
|
public function getCodepointsByBlock(Block $block) |
131
|
|
|
{ |
132
|
|
|
return $this->sourceRepository |
133
|
|
|
->getCodepointsByBlock($block); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Block $block |
138
|
|
|
* @return Collection|CodepointAssigned[] |
139
|
|
|
*/ |
140
|
|
|
public function getByBlock(Block $block) |
141
|
|
|
{ |
142
|
|
|
return $this->getByCodepointRanges( |
143
|
|
|
$this->getCodepointsByBlock($block) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param GeneralCategory $category |
149
|
|
|
* @throws Repository\BlockNotFoundException |
150
|
|
|
* @return Codepoint\Range\Collection |
151
|
|
|
*/ |
152
|
|
|
public function getCodepointsByCategory(GeneralCategory $category) |
153
|
|
|
{ |
154
|
|
|
return $this->sourceRepository |
155
|
|
|
->getCodepointsByCategory($category); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param GeneralCategory $category |
160
|
|
|
* @return Collection|CodepointAssigned[] |
161
|
|
|
*/ |
162
|
|
|
public function getByCategory(GeneralCategory $category) |
163
|
|
|
{ |
164
|
|
|
return $this->getByCodepointRanges( |
165
|
|
|
$this->getCodepointsByCategory($category) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param Script $script |
171
|
|
|
* @throws Repository\BlockNotFoundException |
172
|
|
|
* @return Codepoint\Range\Collection |
173
|
|
|
*/ |
174
|
|
|
public function getCodepointsByScript(Script $script) |
175
|
|
|
{ |
176
|
|
|
return $this->sourceRepository |
177
|
|
|
->getCodepointsByScript($script); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param Script $script |
182
|
|
|
* @return Collection|CodepointAssigned[] |
183
|
|
|
*/ |
184
|
|
|
public function getByScript(Script $script) |
185
|
|
|
{ |
186
|
|
|
return $this->getByCodepointRanges( |
187
|
|
|
$this->getCodepointsByScript($script) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param Codepoint\Range\Collection $ranges |
193
|
|
|
* @return CodepointAssigned[] |
194
|
|
|
*/ |
195
|
|
|
private function getByCodepointRanges(Codepoint\Range\Collection $ranges) |
196
|
|
|
{ |
197
|
|
|
return $this->getByCodepoints( |
198
|
|
|
$ranges->expand() |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public function getSize() |
206
|
|
|
{ |
207
|
|
|
return count($this->sourceRepository); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Repository |
212
|
|
|
*/ |
213
|
|
|
private static function createFileRepository() |
214
|
|
|
{ |
215
|
|
|
$dbPathInfo = new \SplFileInfo(sprintf('%s/../../resources/generated/ucd', __DIR__)); |
216
|
|
|
$charactersDirectory = PHPRangeFileDirectory::fromPath($dbPathInfo); |
217
|
|
|
$propsPathInfo = new \SplFileInfo(sprintf('%s/../../resources/generated/props', __DIR__)); |
218
|
|
|
$propertiesDirectory = FileRepository\PropertyFile\PHPPropertyFileDirectory::fromPath($propsPathInfo); |
219
|
|
|
$aggregators = new FileRepository\PropertyAggregators(); |
220
|
|
|
$serializer = new PHPSerializer(); |
221
|
|
|
|
222
|
|
|
return new FileRepository($charactersDirectory, $propertiesDirectory, $aggregators, $serializer); |
223
|
|
|
} |
224
|
|
|
} |