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