|
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 array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $wordMap; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Emojify constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param null|string $emojiMapFile |
|
34
|
|
|
* |
|
35
|
|
|
* @throws TelegramEmojiMapFileNotFoundException |
|
36
|
|
|
*/ |
|
37
|
76 |
|
public function __construct($emojiMapFile = null) |
|
38
|
|
|
{ |
|
39
|
76 |
|
if (empty($emojiMapFile)) { |
|
40
|
76 |
|
$emojiMapFile = __DIR__.self::EMOJI_MAP_FILE; |
|
41
|
76 |
|
} |
|
42
|
|
|
|
|
43
|
76 |
|
if (!file_exists($emojiMapFile)) { |
|
44
|
2 |
|
throw new TelegramEmojiMapFileNotFoundException(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
76 |
|
$emojiMapFileContents = file_get_contents($emojiMapFile); |
|
48
|
76 |
|
$this->emojiMap = (array)json_decode($emojiMapFileContents); |
|
49
|
76 |
|
$this->wordMap = array_flip($this->emojiMap); |
|
50
|
76 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Translate Word to Emoji |
|
54
|
|
|
* |
|
55
|
|
|
* @param $text |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function toEmoji($text) |
|
60
|
|
|
{ |
|
61
|
6 |
|
return $this->replace($text, $this->emojiMap); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Alias of toEmoji() |
|
66
|
|
|
* |
|
67
|
|
|
* @param $text |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function text($text) |
|
72
|
|
|
{ |
|
73
|
|
|
return (new static)->toEmoji($text); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Translate Emoji to Word |
|
78
|
|
|
* |
|
79
|
|
|
* @param $text |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
10 |
|
public function toWord($text) |
|
84
|
|
|
{ |
|
85
|
10 |
|
return $this->replace($text, $this->wordMap, true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Alias of toWord() |
|
90
|
|
|
* |
|
91
|
|
|
* @param $text |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
8 |
|
public static function translate($text) |
|
96
|
|
|
{ |
|
97
|
8 |
|
return (new static)->toWord($text); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Replace |
|
102
|
|
|
* |
|
103
|
|
|
* @param $line |
|
104
|
|
|
* @param $replace |
|
105
|
|
|
* @param bool $toWord |
|
106
|
|
|
* @param string $delimiter |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function replace($line, $replace, $toWord = false, $delimiter = ':') |
|
111
|
|
|
{ |
|
112
|
14 |
|
uksort($replace, function ($a, $b) { |
|
113
|
14 |
|
return strlen($b) - strlen($a); |
|
114
|
14 |
|
}); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
14 |
|
if ($toWord) { |
|
118
|
10 |
|
return $this->emojiToWordReplace($line, $replace, $delimiter); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
6 |
|
return $this->wordToEmojiReplace($line, $replace, $delimiter); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Finds words enclosed by the delimiter and converts them to the |
|
126
|
|
|
* appropriate emoji character. |
|
127
|
|
|
* |
|
128
|
|
|
* @param $line |
|
129
|
|
|
* @param $replace |
|
130
|
|
|
* @param $delimiter |
|
131
|
|
|
* |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
*/ |
|
134
|
6 |
View Code Duplication |
protected function wordToEmojiReplace($line, $replace, $delimiter) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
6 |
|
foreach ($replace as $key => $value) { |
|
137
|
6 |
|
$line = str_replace($delimiter.$key.$delimiter, $value, $line); |
|
138
|
6 |
|
} |
|
139
|
|
|
|
|
140
|
6 |
|
return $line; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Finds emojis and replaces them with text enclosed by the delimiter |
|
145
|
|
|
* |
|
146
|
|
|
* @param $line |
|
147
|
|
|
* @param $replace |
|
148
|
|
|
* @param $delimiter |
|
149
|
|
|
* |
|
150
|
|
|
* @return mixed |
|
151
|
|
|
*/ |
|
152
|
10 |
View Code Duplication |
protected function emojiToWordReplace($line, $replace, $delimiter) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
10 |
|
foreach ($replace as $key => $value) { |
|
155
|
10 |
|
$line = str_replace($key, $delimiter.$value.$delimiter, $line); |
|
156
|
10 |
|
} |
|
157
|
|
|
|
|
158
|
10 |
|
return $line; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.