@@ -68,15 +68,10 @@ public static function buildRules(array $emojisCodePoints): Generator
68
68
$ ignored = [];
69
69
$ mapsByLocale = [];
70
70
71
- // Build a map of all emojis, ordered by code points length
72
- $ orderedEmojis = [...$ emojisCodePoints ];
73
- uksort ($ orderedEmojis , fn ($ a , $ b ) => mb_strlen ($ b , 'utf-8 ' ) <=> mb_strlen ($ a , 'utf-8 ' ));
74
- $ emojiMap = array_fill_keys ($ orderedEmojis , null );
75
-
76
71
foreach ($ files as $ file ) {
77
72
$ locale = $ file ->getBasename ('.xml ' );
78
73
79
- $ mapsByLocale [$ locale ] ??= [... $ emojiMap ];
74
+ $ mapsByLocale [$ locale ] ??= [];
80
75
81
76
$ document = new DOMDocument ();
82
77
$ document ->loadXML (file_get_contents ($ file ));
@@ -104,24 +99,29 @@ public static function buildRules(array $emojisCodePoints): Generator
104
99
if (!self ::testEmoji ($ emoji , $ locale , $ emojiCodePoints )) {
105
100
continue ;
106
101
}
107
- $ mapsByLocale [$ locale ][$ emoji ] = $ name ;
102
+ $ codePointsCount = mb_strlen ($ emoji );
103
+ $ mapsByLocale [$ locale ][$ codePointsCount ][$ emoji ] = $ name ;
108
104
}
109
105
}
110
106
111
107
ksort ($ mapsByLocale );
112
108
113
- foreach ($ mapsByLocale as $ locale => $ localeMap ) {
109
+ foreach ($ mapsByLocale as $ locale => $ localeMaps ) {
114
110
$ parentLocale = $ locale ;
115
111
116
112
while (false !== $ i = strrpos ($ parentLocale , '_ ' )) {
117
113
$ parentLocale = substr ($ parentLocale , 0 , $ i );
118
- // Override parent map with child one, ensuring the result map contains
119
- // all the emojis while keeping a shared order between locales
120
- $ localeMap = [...($ mapsByLocale [$ parentLocale ] ?? []), ...array_filter ($ localeMap )];
114
+ $ parentMaps = $ mapsByLocale [$ parentLocale ] ?? [];
115
+ foreach ($ parentMaps as $ codePointsCount => $ parentMap ) {
116
+ // Ensuring the result map contains all the emojis from the parent map
117
+ // if not already defined by the current locale
118
+ $ localeMaps [$ codePointsCount ] = [...$ parentMap , ...$ localeMaps [$ codePointsCount ] ?? []];
119
+ }
121
120
}
122
121
123
- if ($ localeMap = [...array_filter ($ localeMap )]) {
124
- yield strtolower ("emoji- $ locale " ) => $ localeMap ;
122
+ // Skip locales without any emoji
123
+ if ($ localeRules = self ::createRules ($ localeMaps )) {
124
+ yield strtolower ("emoji- $ locale " ) => $ localeRules ;
125
125
}
126
126
}
127
127
}
0 commit comments