1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telegram\Bot\Helpers; |
4
|
|
|
|
5
|
|
|
use Telegram\Bot\Exceptions\TelegramEmojiMapFileNotFoundException; |
6
|
|
|
|
7
|
|
|
class Emojify |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
11
|
|
|
*/ |
12
|
|
|
private static $instance; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The path to the file containing the emoji map. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
const DEFAULT_EMOJI_MAP_FILE = '/../Storage/emoji.json'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The path to the file containing the emoji map. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $emojiMapFile; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The array mapping words to emoji. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $emojiMap; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The array mapping emoji back to words. |
37
|
76 |
|
* |
38
|
|
|
* @var array |
39
|
76 |
|
*/ |
40
|
76 |
|
protected $wordMap; |
41
|
76 |
|
|
42
|
|
|
/** |
43
|
76 |
|
* Protected Emojify constructor to prevent creating a new instance of the |
44
|
2 |
|
* *Singleton* via the `new` operator from outside of this class. |
45
|
|
|
* |
46
|
|
|
* @throws TelegramEmojiMapFileNotFoundException |
47
|
76 |
|
*/ |
48
|
76 |
|
protected function __construct() |
49
|
76 |
|
{ |
50
|
76 |
|
$this->setupEmojiMaps(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the *Singleton* instance of this class. |
55
|
|
|
* |
56
|
|
|
* @return Singleton The *Singleton* instance. |
57
|
|
|
*/ |
58
|
|
|
public static function getInstance() |
59
|
6 |
|
{ |
60
|
|
|
if (null === static::$instance) { |
|
|
|
|
61
|
6 |
|
static::$instance = new static(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return static::$instance; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set File Path to Emoji Map File. |
69
|
|
|
* |
70
|
|
|
* @param string $emojiMapFile |
71
|
|
|
* |
72
|
|
|
* @return Emojify |
73
|
|
|
*/ |
74
|
|
|
public function setEmojiMapFile($emojiMapFile) |
75
|
|
|
{ |
76
|
|
|
$this->emojiMapFile = $emojiMapFile; |
77
|
|
|
$this->setupEmojiMaps(); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
10 |
|
* Translate Word to Emoji |
84
|
|
|
* |
85
|
10 |
|
* @param $text |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function toEmoji($text) |
90
|
|
|
{ |
91
|
|
|
return $this->replace($text, $this->emojiMap); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
8 |
|
* Alias of toEmoji() |
96
|
|
|
* |
97
|
8 |
|
* @param $text |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public static function text($text) |
102
|
|
|
{ |
103
|
|
|
return self::getInstance()->toEmoji($text); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Translate Emoji to Word |
108
|
|
|
* |
109
|
|
|
* @param $text |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
14 |
|
*/ |
113
|
14 |
|
public function toWord($text) |
114
|
14 |
|
{ |
115
|
|
|
return $this->replace($text, $this->wordMap, true); |
116
|
|
|
} |
117
|
14 |
|
|
118
|
10 |
|
/** |
119
|
|
|
* Alias of toWord() |
120
|
|
|
* |
121
|
6 |
|
* @param $text |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public static function translate($text) |
126
|
|
|
{ |
127
|
|
|
return self::getInstance()->toWord($text); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Replace |
132
|
|
|
* |
133
|
|
|
* @param $line |
134
|
6 |
|
* @param $replace |
135
|
|
|
* @param bool $toWord |
136
|
6 |
|
* @param string $delimiter |
137
|
6 |
|
* |
138
|
6 |
|
* @return mixed |
139
|
|
|
*/ |
140
|
6 |
|
protected function replace($line, $replace, $toWord = false, $delimiter = ':') |
141
|
|
|
{ |
142
|
|
|
uksort($replace, function ($a, $b) { |
143
|
|
|
return strlen($b) - strlen($a); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
if ($toWord) { |
148
|
|
|
return $this->emojiToWordReplace($line, $replace, $delimiter); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->wordToEmojiReplace($line, $replace, $delimiter); |
152
|
10 |
|
} |
153
|
|
|
|
154
|
10 |
|
/** |
155
|
10 |
|
* Finds words enclosed by the delimiter and converts them to the |
156
|
10 |
|
* appropriate emoji character. |
157
|
|
|
* |
158
|
10 |
|
* @param $line |
159
|
|
|
* @param $replace |
160
|
|
|
* @param $delimiter |
161
|
|
|
* |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
protected function wordToEmojiReplace($line, $replace, $delimiter) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
foreach ($replace as $key => $value) { |
167
|
|
|
$line = str_replace($delimiter.$key.$delimiter, $value, $line); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $line; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Finds emojis and replaces them with text enclosed by the delimiter |
175
|
|
|
* |
176
|
|
|
* @param $line |
177
|
|
|
* @param $replace |
178
|
|
|
* @param $delimiter |
179
|
|
|
* |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
protected function emojiToWordReplace($line, $replace, $delimiter) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
foreach ($replace as $key => $value) { |
185
|
|
|
$line = str_replace($key, $delimiter.$value.$delimiter, $line); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $line; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get Emoji Map Array. |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
* @throws TelegramEmojiMapFileNotFoundException |
196
|
|
|
*/ |
197
|
|
|
protected function getEmojiMap() |
198
|
|
|
{ |
199
|
|
|
if (!isset($this->emojiMapFile)) { |
200
|
|
|
$this->emojiMapFile = realpath(__DIR__.self::DEFAULT_EMOJI_MAP_FILE); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (!file_exists($this->emojiMapFile)) { |
204
|
|
|
throw new TelegramEmojiMapFileNotFoundException(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return json_decode(file_get_contents($this->emojiMapFile), true); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Setup Emoji Maps. |
212
|
|
|
* |
213
|
|
|
* @throws TelegramEmojiMapFileNotFoundException |
214
|
|
|
*/ |
215
|
|
|
protected function setupEmojiMaps() |
216
|
|
|
{ |
217
|
|
|
$this->emojiMap = $this->getEmojiMap(); |
218
|
|
|
$this->wordMap = array_flip($this->emojiMap); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Private clone method to prevent cloning of the instance of the |
223
|
|
|
* *Singleton* instance. |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
private function __clone() |
228
|
|
|
{ |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Private unserialize method to prevent unserializing of the *Singleton* |
233
|
|
|
* instance. |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
private function __wakeup() |
238
|
|
|
{ |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: