@@ -98,9 +98,6 @@ public static function buildRules(array $emojisCodePoints): Generator
98
98
];
99
99
continue ;
100
100
}
101
- if (!self ::testEmoji ($ emoji , $ locale , $ emojiCodePoints )) {
102
- continue ;
103
- }
104
101
$ codePointsCount = mb_strlen ($ emoji );
105
102
$ mapsByLocale [$ locale ][$ codePointsCount ][$ emoji ] = $ name ;
106
103
}
@@ -131,105 +128,73 @@ public static function buildRules(array $emojisCodePoints): Generator
131
128
public static function buildGitHubRules (array $ emojisCodePoints ): iterable
132
129
{
133
130
$ emojis = json_decode ((new Filesystem ())->readFile (__DIR__ .'/vendor/github-emojis.json ' ), true , flags: JSON_THROW_ON_ERROR );
134
-
135
- $ ignored = [];
136
131
$ maps = [];
137
132
138
133
foreach ($ emojis as $ shortCode => $ url ) {
139
134
$ emojiCodePoints = str_replace ('- ' , ' ' , strtolower (basename (parse_url ($ url , \PHP_URL_PATH ), '.png ' )));
135
+
140
136
if (!array_key_exists ($ emojiCodePoints , $ emojisCodePoints )) {
141
- $ ignored [] = [
142
- 'emojiCodePoints ' => $ emojiCodePoints ,
143
- 'shortCode ' => $ shortCode ,
144
- ];
145
137
continue ;
146
138
}
147
139
$ emoji = $ emojisCodePoints [$ emojiCodePoints ];
148
- if (!self ::testEmoji ($ emoji , 'github ' , $ emojiCodePoints )) {
149
- continue ;
150
- }
151
- $ codePointsCount = mb_strlen ($ emoji );
152
- $ maps [$ codePointsCount ][$ emoji ] = ": $ shortCode: " ;
140
+ $ maps [mb_strlen ($ emoji )][": $ shortCode: " ] = $ emoji ;
153
141
}
154
142
155
- $ maps = self ::createRules ($ maps );
143
+ [ $ map , $ reverse ] = self ::createRules ($ maps, true );
156
144
157
- return ['emoji-github ' => $ maps , 'github-emoji ' => array_flip ( $ maps ) ];
145
+ return ['emoji-github ' => $ map , 'github-emoji ' => $ reverse ];
158
146
}
159
147
160
148
public static function buildGitlabRules (array $ emojisCodePoints ): iterable
161
149
{
162
150
$ emojis = json_decode ((new Filesystem ())->readFile (__DIR__ .'/vendor/gitlab-emojis.json ' ), true , flags: JSON_THROW_ON_ERROR );
163
-
164
- $ ignored = [];
165
151
$ maps = [];
166
152
167
153
foreach ($ emojis as $ emojiItem ) {
168
154
$ emojiCodePoints = strtolower ($ emojiItem ['unicode ' ]);
155
+
169
156
if (!array_key_exists ($ emojiCodePoints , $ emojisCodePoints )) {
170
- $ ignored [] = [
171
- 'emojiCodePoints ' => $ emojiCodePoints ,
172
- 'name ' => $ emojiItem ['name ' ],
173
- ];
174
157
continue ;
175
158
}
176
159
$ emoji = $ emojisCodePoints [$ emojiCodePoints ];
177
- if (!self ::testEmoji ($ emoji , 'gitlab ' , $ emojiCodePoints )) {
178
- continue ;
179
- }
180
- $ codePointsCount = mb_strlen ($ emoji );
181
- $ maps [$ codePointsCount ][$ emoji ] = $ emojiItem ['shortname ' ];
160
+ $ maps [mb_strlen ($ emoji )][$ emojiItem ['shortname ' ]] = $ emoji ;
182
161
}
183
162
184
- $ maps = self ::createRules ($ maps );
163
+ [ $ map , $ reverse ] = self ::createRules ($ maps, true );
185
164
186
- return ['emoji-gitlab ' => $ maps , 'gitlab-emoji ' => array_flip ( $ maps ) ];
165
+ return ['emoji-gitlab ' => $ map , 'gitlab-emoji ' => $ reverse ];
187
166
}
188
167
189
168
public static function buildSlackRules (array $ emojisCodePoints ): iterable
190
169
{
191
170
$ emojis = json_decode ((new Filesystem ())->readFile (__DIR__ .'/vendor/slack-emojis.json ' ), true , flags: JSON_THROW_ON_ERROR );
192
-
193
- $ ignored = [];
194
- $ emojiSlackMaps = [];
195
- $ slackEmojiMaps = [];
171
+ $ maps = [];
196
172
197
173
foreach ($ emojis as $ data ) {
198
174
$ emojiCodePoints = str_replace ('- ' , ' ' , strtolower ($ data ['unified ' ]));
199
- $ shortCode = $ data ['short_name ' ];
200
- $ shortCodes = $ data ['short_names ' ];
201
- $ shortCodes = array_map (fn ($ v ) => ": $ v: " , $ shortCodes );
202
175
203
176
if (!array_key_exists ($ emojiCodePoints , $ emojisCodePoints )) {
204
- $ ignored [] = [
205
- 'emojiCodePoints ' => $ emojiCodePoints ,
206
- 'shortCode ' => $ shortCode ,
207
- ];
208
177
continue ;
209
178
}
210
179
$ emoji = $ emojisCodePoints [$ emojiCodePoints ];
211
- if (!self ::testEmoji ($ emoji , 'slack ' , $ emojiCodePoints )) {
212
- continue ;
213
- }
214
- $ codePointsCount = mb_strlen ($ emoji );
215
- $ emojiSlackMaps [$ codePointsCount ][$ emoji ] = ": $ shortCode: " ;
216
- foreach ($ shortCodes as $ short_name ) {
217
- $ slackEmojiMaps [$ codePointsCount ][$ short_name ] = $ emoji ;
180
+ $ emojiPriority = mb_strlen ($ emoji ) << 1 ;
181
+ $ maps [$ emojiPriority + 1 ][": {$ data ['short_name ' ]}: " ] = $ emoji ;
182
+
183
+ foreach ($ data ['short_names ' ] as $ shortName ) {
184
+ $ maps [$ emojiPriority ][": $ shortName: " ] = $ emoji ;
218
185
}
219
186
}
220
187
221
- return ['emoji-slack ' => self ::createRules ($ emojiSlackMaps ), 'slack-emoji ' => self ::createRules ($ slackEmojiMaps )];
188
+ [$ map , $ reverse ] = self ::createRules ($ maps , true );
189
+
190
+ return ['emoji-slack ' => $ map , 'slack-emoji ' => $ reverse ];
222
191
}
223
192
224
193
public static function buildStripRules (array $ emojisCodePoints ): iterable
225
194
{
226
195
$ maps = [];
227
- foreach ($ emojisCodePoints as $ codePoints => $ emoji ) {
228
- if (!self ::testEmoji ($ emoji , 'strip ' , $ codePoints )) {
229
- continue ;
230
- }
231
- $ codePointsCount = mb_strlen ($ emoji );
232
- $ maps [$ codePointsCount ][$ emoji ] = '' ;
196
+ foreach ($ emojisCodePoints as $ emoji ) {
197
+ $ maps [mb_strlen ($ emoji )][$ emoji ] = '' ;
233
198
}
234
199
235
200
return ['emoji-strip ' => self ::createRules ($ maps )];
@@ -269,24 +234,26 @@ public static function saveRules(iterable $rulesByLocale): void
269
234
$ fs ->dumpFile ($ file , preg_replace ('/QUICK_CHECK = .*;/m ' , "QUICK_CHECK = {$ quickCheck }; " , $ fs ->readFile ($ file )));
270
235
}
271
236
272
- private static function testEmoji ( string $ emoji , string $ locale , string $ codePoints ): bool
237
+ private static function createRules ( array $ maps , bool $ reverse = false ): array
273
238
{
274
- if (!Transliterator::createFromRules ("\\$ emoji > test ; " )) {
275
- printf ('Could not create transliterator for "%s" in "%s" locale. Code Point: "%s". Error: "%s". ' ."\n" , $ emoji , $ locale , $ codePoints , intl_get_error_message ());
239
+ // We must sort the maps by the number of code points, because the order really matters:
240
+ // 🫶🏼 must be before 🫶
241
+ krsort ($ maps );
276
242
277
- return false ;
243
+ if (!$ reverse ) {
244
+ return array_merge (...$ maps );
278
245
}
279
246
280
- return true ;
281
- }
247
+ $ emojiText = $ textEmoji = [];
282
248
283
- private static function createRules (array $ maps ): array
284
- {
285
- // We must sort the maps by the number of code points, because the order really matters:
286
- // 🫶🏼 must be before 🫶
287
- krsort ($ maps );
288
- $ maps = array_merge (...$ maps );
249
+ foreach ($ maps as $ map ) {
250
+ uksort ($ map , static fn ($ a , $ b ) => strnatcmp (substr ($ a , 1 , -1 ), substr ($ b , 1 , -1 )));
251
+ $ textEmoji = $ map + $ textEmoji ;
252
+
253
+ $ map = array_flip ($ map );
254
+ $ emojiText += $ map ;
255
+ }
289
256
290
- return $ maps ;
257
+ return [ $ emojiText , $ textEmoji ] ;
291
258
}
292
259
}
0 commit comments