|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Emoji\Dataset; |
|
6
|
|
|
|
|
7
|
|
|
use League\Configuration\ConfigurationInterface; |
|
8
|
|
|
use League\Emoji\Emojibase\EmojibaseDatasetInterface; |
|
9
|
|
|
use League\Emoji\Emojibase\EmojibaseShortcodeInterface; |
|
10
|
|
|
use League\Emoji\Exception\FileNotFoundException; |
|
11
|
|
|
use League\Emoji\Exception\LocalePresetException; |
|
12
|
|
|
use League\Emoji\Exception\MalformedArchiveException; |
|
13
|
|
|
use League\Emoji\Exception\UnarchiveException; |
|
14
|
|
|
use League\Emoji\Parser\EmojiParser; |
|
15
|
|
|
|
|
16
|
|
|
final class RuntimeDataset implements \ArrayAccess, \Countable, \SeekableIterator |
|
17
|
|
|
{ |
|
18
|
|
|
public const DEFAULT = 'en'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ConfigurationInterface */ |
|
21
|
|
|
private $config; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ?Dataset */ |
|
24
|
|
|
private $dataset; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ?string */ |
|
27
|
|
|
private $locale; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ?bool */ |
|
30
|
|
|
private $native; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ?string[] */ |
|
33
|
|
|
private $presets; |
|
34
|
|
|
|
|
35
|
663 |
|
public function __construct(ConfigurationInterface $configuration, ?Dataset $dataset = null) |
|
36
|
|
|
{ |
|
37
|
663 |
|
$this->config = $configuration; |
|
38
|
663 |
|
$this->dataset = $dataset; |
|
39
|
663 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string[] $indices |
|
43
|
|
|
* |
|
44
|
|
|
* @return false|string |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public static function archive(Dataset $dataset, array $indices = EmojiParser::INDICES) |
|
47
|
|
|
{ |
|
48
|
3 |
|
foreach ($indices as $index) { |
|
49
|
3 |
|
$dataset->indexBy($index); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
$serialize = \serialize($dataset); |
|
53
|
|
|
|
|
54
|
3 |
|
return \gzencode($serialize, 9); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
663 |
|
public static function unarchive(string $filename): Dataset |
|
58
|
|
|
{ |
|
59
|
663 |
|
if (! \file_exists($filename)) { |
|
60
|
402 |
|
throw new FileNotFoundException($filename); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ( |
|
64
|
261 |
|
! ($contents = \file_get_contents($filename)) || |
|
65
|
261 |
|
! ($decoded = \gzdecode($contents)) |
|
66
|
|
|
) { |
|
67
|
3 |
|
throw new UnarchiveException($filename); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
|
|
/** @var ?Dataset $dataset */ |
|
72
|
258 |
|
$dataset = \unserialize($decoded, [ |
|
73
|
258 |
|
'allowed_classes' => [Dataset::class, Emoji::class], |
|
74
|
|
|
]); |
|
75
|
3 |
|
} catch (\Throwable $throwable) { |
|
76
|
3 |
|
throw new MalformedArchiveException($filename, $throwable); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
255 |
|
if (! $dataset instanceof Dataset) { |
|
80
|
3 |
|
throw new MalformedArchiveException($filename); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
252 |
|
return $dataset; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
public function count(): int |
|
87
|
|
|
{ |
|
88
|
6 |
|
return $this->getDataset()->count(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
public function current(): ?Emoji |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var ?Emoji $current */ |
|
94
|
3 |
|
$current = $this->getDataset()->current(); |
|
95
|
|
|
|
|
96
|
3 |
|
return $current; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param callable(Emoji):bool $callback |
|
101
|
|
|
*/ |
|
102
|
3 |
|
public function filter(callable $callback): RuntimeDataset |
|
103
|
|
|
{ |
|
104
|
3 |
|
return new self($this->config, $this->getDataset()->filter($callback)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
657 |
|
public function getDataset(): Dataset |
|
108
|
|
|
{ |
|
109
|
657 |
|
if ($this->dataset === null) { |
|
110
|
|
|
/** @var \Throwable[] $throwables */ |
|
111
|
651 |
|
$throwables = []; |
|
112
|
651 |
|
$locale = $this->getLocale(); |
|
113
|
648 |
|
$presets = $this->getPresets(); |
|
114
|
|
|
|
|
115
|
648 |
|
$remaining = $presets; |
|
116
|
648 |
|
while (\count($remaining) > 0) { |
|
117
|
648 |
|
$preset = \array_shift($remaining); |
|
118
|
|
|
try { |
|
119
|
648 |
|
$this->dataset = self::unarchive(\sprintf('%s/%s/%s.gz', Dataset::DIRECTORY, $locale, $preset)); |
|
120
|
249 |
|
break; |
|
121
|
399 |
|
} catch (\Throwable $throwable) { |
|
122
|
399 |
|
$throwables[$preset] = $throwable; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
648 |
|
if ($this->dataset === null) { |
|
127
|
399 |
|
if ($this->config->data()->has('locale')) { |
|
128
|
399 |
|
$locale = (string) $this->config->data()->get('locale'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
399 |
|
throw new LocalePresetException($locale, $throwables); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
255 |
|
return $this->dataset; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
651 |
|
public function getLocale(): string |
|
139
|
|
|
{ |
|
140
|
651 |
|
if ($this->locale === null) { |
|
141
|
651 |
|
$this->locale = (string) ($this->config->get('locale') ?? self::DEFAULT); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
648 |
|
return $this->locale; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string[] |
|
149
|
|
|
*/ |
|
150
|
648 |
|
public function getPresets(): array |
|
151
|
|
|
{ |
|
152
|
648 |
|
if ($this->presets === null) { |
|
153
|
|
|
/** @var string[] $presets */ |
|
154
|
648 |
|
$presets = (array) $this->config->get('preset'); |
|
155
|
|
|
|
|
156
|
|
|
// Prepend the native preset if local is requires it and enabled. |
|
157
|
648 |
|
if ($this->isNative()) { |
|
158
|
24 |
|
\array_unshift($presets, EmojibaseShortcodeInterface::PRESET_CLDR_NATIVE); |
|
159
|
|
|
} else { |
|
160
|
|
|
/** @var int|false $key */ |
|
161
|
627 |
|
$key = \array_search(EmojibaseShortcodeInterface::PRESET_CLDR_NATIVE, $presets, true); |
|
162
|
|
|
|
|
163
|
|
|
// Only remove the CLDR native preset if it's not the only one provided. |
|
164
|
627 |
|
if ($key !== false && \count($presets) !== 1) { |
|
165
|
72 |
|
\array_splice($presets, $key, 1); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
648 |
|
$this->presets = \array_filter(\array_values(\array_unique(\array_filter($presets)))); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
648 |
|
return $this->presets; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
654 |
|
public function indexBy(string $index = 'hexcode'): RuntimeDataset |
|
176
|
|
|
{ |
|
177
|
654 |
|
return new self($this->config, $this->getDataset()->indexBy($index)); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
648 |
|
public function isNative(): bool |
|
181
|
|
|
{ |
|
182
|
648 |
|
if ($this->native === null) { |
|
183
|
648 |
|
$locale = $this->getLocale(); |
|
184
|
648 |
|
$default = \in_array($locale, EmojibaseDatasetInterface::NON_LATIN_LOCALES, true); |
|
185
|
|
|
|
|
186
|
|
|
/** @var ?bool $native */ |
|
187
|
648 |
|
$native = $this->config->get('native'); |
|
188
|
|
|
|
|
189
|
648 |
|
$this->native = $native === null |
|
190
|
195 |
|
? $default |
|
191
|
456 |
|
: $native && $default; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
648 |
|
return $this->native; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
3 |
|
public function key(): string |
|
198
|
|
|
{ |
|
199
|
3 |
|
return (string) $this->getDataset()->key(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
3 |
|
public function next(): void |
|
203
|
|
|
{ |
|
204
|
3 |
|
$this->getDataset()->next(); |
|
205
|
3 |
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** @param string $offset */ |
|
208
|
3 |
|
public function offsetExists($offset): bool // phpcs:ignore |
|
209
|
|
|
{ |
|
210
|
3 |
|
return $this->getDataset()->offsetExists($offset); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** @param string $offset */ |
|
214
|
102 |
|
public function offsetGet($offset): ?Emoji // phpcs:ignore |
|
215
|
|
|
{ |
|
216
|
102 |
|
return $this->getDataset()->offsetGet($offset); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $offset |
|
221
|
|
|
* @param mixed $value |
|
222
|
|
|
*/ |
|
223
|
3 |
|
public function offsetSet($offset, $value): void // phpcs:ignore |
|
224
|
|
|
{ |
|
225
|
3 |
|
throw new \BadMethodCallException('Unable to modify immutable object.'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** @param string $offset */ |
|
229
|
3 |
|
public function offsetUnset($offset): void // phpcs:ignore |
|
230
|
|
|
{ |
|
231
|
3 |
|
throw new \BadMethodCallException('Unable to modify immutable object.'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
3 |
|
public function rewind(): void |
|
235
|
|
|
{ |
|
236
|
3 |
|
$this->getDataset()->rewind(); |
|
237
|
3 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** @param int $position */ |
|
240
|
3 |
|
public function seek($position): void // phpcs:ignore |
|
241
|
|
|
{ |
|
242
|
3 |
|
$this->getDataset()->seek($position); |
|
243
|
3 |
|
} |
|
244
|
|
|
|
|
245
|
3 |
|
public function valid(): bool |
|
246
|
|
|
{ |
|
247
|
3 |
|
return $this->getDataset()->valid(); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|