|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of questocat/epic-emoji package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) questocat <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Questocat\EpicEmoji\Devices; |
|
13
|
|
|
|
|
14
|
|
|
use Questocat\EpicEmoji\Dictionary; |
|
15
|
|
|
use Questocat\EpicEmoji\DictionaryInterface; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* 提示: emoji 表情有很多种版本, 其中包括Unified, DoCoMo, KDDI, Softbank和Google, 并且不同版本用于表示同一符号表情的 unicode 代码也不相同. |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractDevice |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The input text. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $text; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The dictionary instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @var DictionaryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $dict; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The shorthand dict name. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $shorthand = 'shorthand'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Unique device Identifier. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $deviceIdentifier; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* AbstractUnicode construct. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $text |
|
55
|
|
|
*/ |
|
56
|
15 |
|
public function __construct($text = null) |
|
57
|
|
|
{ |
|
58
|
15 |
|
$this->text = $text; |
|
59
|
15 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function __toString() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->text; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the text. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $text |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function setText($text) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$this->text = $text; |
|
77
|
|
|
|
|
78
|
2 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set the text. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $text |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function withText($text) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->text = $text; |
|
89
|
|
|
|
|
90
|
2 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the dictionary. |
|
95
|
|
|
*/ |
|
96
|
11 |
|
public function setDictionary(DictionaryInterface $dict) |
|
97
|
|
|
{ |
|
98
|
11 |
|
$this->dict = $dict; |
|
99
|
|
|
|
|
100
|
11 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns the dictionary. |
|
105
|
|
|
* |
|
106
|
|
|
* @return DictionaryInterface |
|
107
|
|
|
*/ |
|
108
|
13 |
|
public function getDictionary() |
|
109
|
|
|
{ |
|
110
|
13 |
|
if (is_null($this->dict)) { |
|
111
|
4 |
|
$path = dirname(dirname(__DIR__)).'/data/'; |
|
112
|
4 |
|
$this->dict = new Dictionary($path); |
|
113
|
4 |
|
} |
|
114
|
|
|
|
|
115
|
13 |
|
return $this->dict; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Output emoji. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function emoji() |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->text; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns shorthand of the text. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function shorthand() |
|
134
|
|
|
{ |
|
135
|
2 |
|
$shorthandDict = $this->getDictionary()->shorthandDict($this->shorthand); |
|
136
|
2 |
|
$search = array_column($shorthandDict, $this->getDeviceIdentifier()); |
|
137
|
2 |
|
$replace = array_keys($shorthandDict); |
|
138
|
|
|
|
|
139
|
2 |
|
return $this->convert($this->text, $search, $replace); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns codepoint of the unicode. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function codepoint() |
|
148
|
|
|
{ |
|
149
|
1 |
|
$unicodeDict = $this->getUnicodeDict(); |
|
150
|
1 |
|
$search = array_keys($unicodeDict); |
|
151
|
1 |
|
$replace = array_column($unicodeDict, 'codepoint'); |
|
152
|
|
|
|
|
153
|
1 |
|
return $this->convert($this->text, $search, $replace); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Converts to HTML. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function html() |
|
162
|
|
|
{ |
|
163
|
1 |
|
$calledDevice = $this->getDeviceIdentifier(); |
|
164
|
1 |
|
$dictionary = $this->getDictionary()->htmlDict($calledDevice); |
|
165
|
|
|
|
|
166
|
1 |
|
return strtr($this->text, $dictionary); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Converts to HTML entities. |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function htmlEntity() |
|
175
|
|
|
{ |
|
176
|
1 |
|
$unicodeDict = $this->getUnicodeDict(); |
|
177
|
1 |
|
$search = array_keys($unicodeDict); |
|
178
|
1 |
|
$replace = array_map(function ($codepoint) { |
|
179
|
1 |
|
return html_entity($codepoint); |
|
180
|
1 |
|
}, array_column($unicodeDict, 'codepoint')); |
|
181
|
|
|
|
|
182
|
1 |
|
return $this->convert($this->text, $search, $replace); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Converts to target device. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $device |
|
189
|
|
|
* |
|
190
|
|
|
* @throws InvalidArgumentException |
|
191
|
|
|
* |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
6 |
|
protected function deviceExchange($device) |
|
195
|
|
|
{ |
|
196
|
6 |
|
$unicodeDict = $this->getUnicodeDict(); |
|
197
|
6 |
|
$deviceName = __NAMESPACE__.'\\'.ucfirst($device); |
|
198
|
|
|
|
|
199
|
6 |
|
if (!class_exists($deviceName)) { |
|
200
|
1 |
|
throw new InvalidArgumentException("Device [$deviceName] doesn't exist."); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
5 |
|
$search = array_keys($unicodeDict); |
|
204
|
5 |
|
$replace = array_column(array_column($unicodeDict, 'unicode'), strtolower($device)); |
|
205
|
5 |
|
$replacement = $this->convert($this->text, $search, $replace); |
|
206
|
|
|
|
|
207
|
5 |
|
return new $deviceName($replacement); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Converts text. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $text |
|
214
|
|
|
* @param mixed $search |
|
215
|
|
|
* @param mixed $replace |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
9 |
|
protected function convert($text, $search, $replace) |
|
220
|
|
|
{ |
|
221
|
9 |
|
return str_replace($search, $replace, $text); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Returns the device Identifier. |
|
226
|
|
|
* |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
11 |
|
protected function getDeviceIdentifier() |
|
230
|
|
|
{ |
|
231
|
11 |
|
return $this->deviceIdentifier; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Get the unicode dictionart dict. |
|
236
|
|
|
* |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
8 |
|
protected function getUnicodeDict() |
|
240
|
|
|
{ |
|
241
|
8 |
|
$calledDevice = $this->getDeviceIdentifier(); |
|
242
|
|
|
|
|
243
|
8 |
|
return $this->getDictionary()->unicodeDict($calledDevice); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|