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