|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fatindeed\EmojiHelper; |
|
4
|
|
|
|
|
5
|
|
|
use SplMinHeap; |
|
6
|
|
|
|
|
7
|
|
|
class EmojiData |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Emoji code points. |
|
11
|
|
|
* |
|
12
|
|
|
* @var SplMinHeap |
|
13
|
|
|
*/ |
|
14
|
|
|
private $_data; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Reload emoji data from unicode.org. |
|
18
|
|
|
* |
|
19
|
|
|
* @return void |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://unicode.org/Public/emoji/12.1/emoji-data.txt |
|
22
|
|
|
*/ |
|
23
|
|
|
private function reload() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->_data = new SplMinHeap(); |
|
26
|
|
|
$lines = file('https://unicode.org/Public/emoji/12.1/emoji-data.txt'); |
|
27
|
|
|
foreach ($lines as $line) { |
|
28
|
|
|
$line = trim($line); |
|
29
|
|
|
// Skip comments or empty lines |
|
30
|
|
|
if (empty($line) || substr($line, 0, 1) == '#') { |
|
31
|
|
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
$codePoints = array_map('hexdec', explode('..', trim(strstr($line, ';', true)))); |
|
34
|
|
|
if (count($codePoints) == 1) { |
|
35
|
|
|
$this->insert($codePoints[0]); |
|
36
|
|
|
} elseif (count($codePoints) == 2) { |
|
37
|
|
|
for ($i = $codePoints[0]; $i <= $codePoints[1]; $i++) { |
|
38
|
|
|
$this->insert($i); |
|
39
|
|
|
} |
|
40
|
|
|
} else { |
|
41
|
|
|
trigger_error('Invalid code data - ' . $line, E_USER_ERROR); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Insert a code point(decimal). |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $nubmer Code point. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
private function insert(int $nubmer): void |
|
54
|
|
|
{ |
|
55
|
|
|
if ($nubmer > 255) { |
|
56
|
|
|
$this->_data->insert($nubmer); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get emoji data content(php). |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getContent() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->reload(); |
|
68
|
|
|
$content = '<?php' . PHP_EOL . PHP_EOL; |
|
69
|
|
|
$content .= 'return [' . PHP_EOL; |
|
70
|
|
|
$seq = 0; |
|
71
|
|
|
foreach ($this->_data as $number) { |
|
72
|
|
|
if ($number - $seq > 1) { |
|
73
|
|
|
if ($seq) { |
|
74
|
|
|
$content .= $this->getLine($seq); |
|
75
|
|
|
} |
|
76
|
|
|
$this->from = $number; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
$seq = $number; |
|
79
|
|
|
} |
|
80
|
|
|
$content .= $this->getLine($seq); |
|
81
|
|
|
$content .= '];' . PHP_EOL; |
|
82
|
|
|
return $content; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get line. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $seq Number. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
private function getLine(int $seq): string |
|
93
|
|
|
{ |
|
94
|
|
|
$text = self::printCode($this->from); |
|
95
|
|
|
if ($seq != $this->from) { |
|
96
|
|
|
$text .= '-' . self::printCode($seq); |
|
97
|
|
|
} |
|
98
|
|
|
return ' \'' . $text . '\',' . PHP_EOL; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get unicode. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $nubmer Number. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
private static function printCode(int $nubmer): string |
|
109
|
|
|
{ |
|
110
|
|
|
return '\\x{' . sprintf('%04s', dechex($nubmer)) . '}'; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|