@@ -68,15 +68,10 @@ public static function buildRules(array $emojisCodePoints): Generator
6868 $ ignored = [];
6969 $ mapsByLocale = [];
7070
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-
7671 foreach ($ files as $ file ) {
7772 $ locale = $ file ->getBasename ('.xml ' );
7873
79- $ mapsByLocale [$ locale ] ??= [... $ emojiMap ];
74+ $ mapsByLocale [$ locale ] ??= [];
8075
8176 $ document = new DOMDocument ();
8277 $ document ->loadXML (file_get_contents ($ file ));
@@ -104,24 +99,29 @@ public static function buildRules(array $emojisCodePoints): Generator
10499 if (!self ::testEmoji ($ emoji , $ locale , $ emojiCodePoints )) {
105100 continue ;
106101 }
107- $ mapsByLocale [$ locale ][$ emoji ] = $ name ;
102+ $ codePointsCount = mb_strlen ($ emoji );
103+ $ mapsByLocale [$ locale ][$ codePointsCount ][$ emoji ] = $ name ;
108104 }
109105 }
110106
111107 ksort ($ mapsByLocale );
112108
113- foreach ($ mapsByLocale as $ locale => $ localeMap ) {
109+ foreach ($ mapsByLocale as $ locale => $ localeMaps ) {
114110 $ parentLocale = $ locale ;
115111
116112 while (false !== $ i = strrpos ($ parentLocale , '_ ' )) {
117113 $ 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+ }
121120 }
122121
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 ;
125125 }
126126 }
127127 }
0 commit comments