| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Utility script to extract supported countries for the showphone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * command, meant to be opened in a browser. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @subpackage Tools | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Sebastian Mordziol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @see https://countrycode.org/countryCode/downloadCountryCodes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use AppUtils\CSVHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use AppUtils\FileHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use libphonenumber\PhoneNumberFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use libphonenumber\PhoneNumberUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | require_once 'prepend.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | $outputFile = '../src/Mailcode/Commands/Command/ShowPhone/numbers.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | $countries = generateList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | FileHelper::saveAsJSON($countries, $outputFile, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | ?><!DOCTYPE html> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | <html lang="en"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     <head> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         <meta charset="utf-8"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         <meta http-equiv="X-UA-Compatible" content="IE=edge"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         <meta name="viewport" content="width=device-width, initial-scale=1"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         <title>Mailcode - Phone format countries</title> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         <style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             BODY{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 padding:2em 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             TEXTAREA{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 font-family: monospace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         </style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     </head> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     <body> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         <div class="container"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             <h1>Phone number countries extraction</h1> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 This uses Google's <a href="https://github.com/google/libphonenumber">libphonenumber</a> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 library (via its PHP port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 <a href="https://github.com/giggsey/libphonenumber-for-php">giggsey/libphonenumber-for-php</a>). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 It generates a list of all supported countries for use in the <code>{showphone}</code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 command class, to validate the source country phone format parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 Target class: <code>Mailcode_Commands_Command_ShowPhone</code><br> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 Target data file: <code><?php echo str_replace('../', '/', $outputFile) ?></code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 The command reads the file to access the countries. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 <strong>Extracted JSON data:</strong> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             <textarea rows="20" class="form-control"><?php echo json_encode($countries, JSON_PRETTY_PRINT) ?></textarea> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         </div> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     </body> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | </html> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | <?php | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | function generateList() : array | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     $labels = extractLabels(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     $phoneNumberUtil = PhoneNumberUtil::getInstance(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     $regions = $phoneNumberUtil->getSupportedRegions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     $data = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     foreach ($regions as $code) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $meta = $phoneNumberUtil->getMetadataForRegion($code); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         if (!$meta) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             die('No metadata for ' . $code); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $exampleNumber = $phoneNumberUtil->getExampleNumber($code); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $local = $phoneNumberUtil->formatInOriginalFormat($exampleNumber, $code); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $international = $phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::INTERNATIONAL); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $label = $code; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         if (isset($labels[$code])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             $label = $labels[$code]; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $data[$code] = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             'label' => $label, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             'local' => $local, | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             'international' => $international | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     ksort($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | function extractLabels() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     $lines = CSVHelper::parseFile('countrycodes.csv'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     $result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     foreach ($lines as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $result[$line['ISO2']] = $line['Country Name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     return $result; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } | 
            
                        
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: