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