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