File size: 3,209 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace Mautic\CoreBundle\Helper;

/**
 * Helper class for Emoji unicodes.
 *
 * Build from modified https://github.com/iamcal/php-emoji
 */
class EmojiHelper
{
    /**
     * Convert to html.
     */
    public static function toHtml(string $text, string $from = 'emoji'): string
    {
        return self::emojiConvert($text, $from, 'html');
    }

    /**
     * Convert to emoji.
     */
    public static function toEmoji(string $text, string $from = 'html'): string
    {
        return self::emojiConvert($text, $from, 'emoji');
    }

    /**
     * Convert to short code.
     */
    public static function toShort(string $text, string $from = 'emoji'): string
    {
        return self::emojiConvert($text, $from, 'short');
    }

    /**
     * Converts emojis.
     */
    private static function emojiConvert(string $text, string $from, string $to): string
    {
        $maps = [];
        switch ($from) {
            case 'html':
                switch ($to) {
                    case 'emoji':
                        $maps[] = 'HtmlToUnicode';
                        break;
                    case 'short':
                        $maps[] = 'HtmlToUnicode';
                        $maps[] = 'UnicodeToShort';
                        break;
                }
                break;
            case 'emoji':
                switch ($to) {
                    case 'html':
                        $maps[] = 'UnicodeToHtml';
                        break;
                    case 'short':
                        $maps[] = 'UnicodeToShort';
                        break;
                }
                break;
            case 'short':
                switch ($to) {
                    case 'html':
                        $maps[] = 'ShortToUnicode';
                        $maps[] = 'UnicodeToHtml';
                        break;
                    case 'emoji':
                        $maps[] = 'ShortToUnicode';
                        break;
                }
                break;
        }

        foreach ($maps as $useMap) {
            $mapClass = "Mautic\\CoreBundle\\Helper\\EmojiMap\\{$useMap}EmojiMap";
            $text     = str_replace(array_keys($mapClass::$map), $mapClass::$map, $text);

            if (isset($mapClass::$exceptions)) {
                $text = str_replace(array_keys($mapClass::$exceptions), $mapClass::$exceptions, $text);
            }
        }

        if ('emoji' !== $to) {
            // Parse out missed emojis
            $text = self::removeEmoji($text);
        }

        return $text;
    }

    /**
     * Remove emojis from text.
     */
    private static function removeEmoji(string $text): string
    {
        return preg_replace('/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $text);
    }
}