| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Fatindeed\EmojiHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class EmojiData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * The CodePoints instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var CodePoints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $_codePoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Line output from. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $_from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Load new emoji data instance from url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $url Emoji data url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return EmojiData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @see https://unicode.org/Public/emoji/12.1/emoji-data.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |     public static function load($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $codePoints = new CodePoints(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $lines = file($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |         foreach ($lines as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |             $line = trim($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             // Skip comments or empty lines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             if (empty($line) || substr($line, 0, 1) == '#') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             $data = array_map('hexdec', explode('..', trim(strstr($line, ';', true)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             if (count($data) == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |                 $codePoints->insert($data[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             } elseif (count($data) == 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |                 for ($i = $data[0]; $i <= $data[1]; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                     $codePoints->insert($i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 trigger_error('Invalid code data - ' . $line, E_USER_ERROR); // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         return new self($codePoints); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Constructs a new emoji data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param CodePoints $codePoints The CodePoints instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     public function __construct(CodePoints $codePoints) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $this->_codePoints = $codePoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Save emoji data config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $filename Path to the file where to write the data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 | 1 |  |     public function save($filename) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |         $content = '<?php' . PHP_EOL . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |         $content .= 'return [' . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |         $seq = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |         foreach ($this->_codePoints as $number) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |             if ($number - $seq > 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |                 if ($seq) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |                     $content .= $this->getLine($seq); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |                 $this->_from = $number; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |             $seq = $number; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         $content .= $this->getLine($seq); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |         $content .= '];' . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |         file_put_contents($filename, $content); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |         echo basename($filename) . ' updated.' . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Get line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param int $seq Number. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |     private function getLine(int $seq): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         $text = self::printCode($this->_from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         if ($seq != $this->_from) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             $text .= '-' . self::printCode($seq); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         return '    \'' . $text . '\',' . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Print code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param int $nubmer Number. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |     private static function printCode(int $nubmer): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |         return '\\x{' . sprintf('%04s', dechex($nubmer)) . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  |