Completed
Branch master (5aa5a5)
by James
02:40
created

EmojiData::insert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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