Completed
Push — master ( b6fc98...917978 )
by Irfaq
02:29
created

Emojify::replace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Telegram\Bot\Helpers;
4
5
use Telegram\Bot\Exceptions\TelegramEmojiMapFileNotFoundException;
6
7
class Emojify
8
{
9
    /**
10
     * The path to the file containing the emoji map.
11
     *
12
     * @var string
13
     */
14
    const EMOJI_MAP_FILE = '/../Storage/emoji.json';
15
16
    /**
17
     * The array mapping words to emoji.
18
     *
19
     * @var array
20
     */
21
    protected $emojiMap;
22
23
    /**
24
     * The array mapping emoji back to words.
25
     *
26
     * @var [type]
27
     */
28
    protected $wordMap;
29
30
    /**
31
     * Emojify constructor.
32
     *
33
     * @param null $emojiMapFile
34
     */
35 82
    public function __construct($emojiMapFile = null)
36
    {
37 82
        if (empty($emojiMapFile)) {
38 82
            $emojiMapFile = __DIR__.self::EMOJI_MAP_FILE;
39 82
        }
40
41 82
        if (!file_exists($emojiMapFile)) {
42 2
            throw new TelegramEmojiMapFileNotFoundException();
43
        }
44
45 82
        $emojiMapFileContents = file_get_contents($emojiMapFile);
46 82
        $this->emojiMap = (array)json_decode($emojiMapFileContents);
47 82
        $this->wordMap = array_flip($this->emojiMap);
48 82
    }
49
50
    /**
51
     * Translate Word to Emoji
52
     *
53
     * @param $text
54
     *
55
     * @return mixed
56
     */
57 6
    public function toEmoji($text)
58
    {
59 6
        return $this->replace($text, $this->emojiMap);
60
    }
61
62
    /**
63
     * Alias of toEmoji()
64
     *
65
     * @param $text
66
     *
67
     * @return mixed
68
     */
69
    public static function text($text)
70
    {
71
        return (new static)->toEmoji($text);
72
    }
73
74
    /**
75
     * Translate Emoji to Word
76
     *
77
     * @param $text
78
     *
79
     * @return mixed
80
     */
81 10
    public function toWord($text)
82
    {
83 10
        return $this->replace($text, $this->wordMap, null);
84
    }
85
86
    /**
87
     * Alias of toWord()
88
     *
89
     * @param $text
90
     *
91
     * @return mixed
92
     */
93 8
    public static function translate($text)
94
    {
95 8
        return (new static)->toWord($text);
96
    }
97
98
    /**
99
     * Replace
100
     *
101
     * @param        $line
102
     * @param        $replace
103
     * @param string $startDelimiter
104
     *
105
     * @return mixed
106
     */
107
    protected function replace($line, $replace, $startDelimiter = ':')
108
    {
109 14
        uksort($replace, function ($a, $b) {
110 14
            return strlen($b) - strlen($a);
111 14
        });
112
113 14
        foreach ($replace as $key => $value) {
114 14
            $line = str_replace($startDelimiter.$key, $value, $line);
115 14
        }
116
117 14
        return $line;
118
    }
119
}
120