|
1
|
|
|
<?php |
|
2
|
|
|
namespace libphonenumber\buildtools; |
|
3
|
|
|
|
|
4
|
|
|
use libphonenumber\PhoneMetadata; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Tool to convert phone number metadata from the XML format to protocol buffer format. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Davide Mendolia |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
class BuildMetadataPHPFromXml |
|
13
|
|
|
{ |
|
14
|
|
|
const GENERATION_COMMENT = <<<EOT |
|
15
|
|
|
/** |
|
16
|
|
|
* This file has been @generated by a phing task by {@link BuildMetadataPHPFromXml}. |
|
17
|
|
|
* See [README.md](README.md#generating-data) for more information. |
|
18
|
|
|
* |
|
19
|
|
|
* Pull requests changing data in these files will not be accepted. See the |
|
20
|
|
|
* [FAQ in the README](README.md#problems-with-invalid-numbers] on how to make |
|
21
|
|
|
* metadata changes. |
|
22
|
|
|
* |
|
23
|
|
|
* Do not modify this file directly! |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
EOT; |
|
28
|
|
|
const MAP_COMMENT = <<<EOT |
|
29
|
|
|
// A mapping from a country code to the region codes which denote the |
|
30
|
|
|
// country/region represented by that country code. In the case of multiple |
|
31
|
|
|
// countries sharing a calling code, such as the NANPA countries, the one |
|
32
|
|
|
// indicated with "isMainCountryForCode" in the metadata should be first. |
|
33
|
|
|
|
|
34
|
|
|
EOT; |
|
35
|
|
|
const COUNTRY_CODE_SET_COMMENT = |
|
36
|
|
|
" // A set of all country codes for which data is available.\n"; |
|
37
|
|
|
const REGION_CODE_SET_COMMENT = |
|
38
|
|
|
" // A set of all region codes for which data is available.\n"; |
|
39
|
|
|
|
|
40
|
|
|
public function start($inputFile, $outputDir, $filePrefix, $mappingClass, $mappingClassLocation, $liteBuild) |
|
41
|
|
|
{ |
|
42
|
|
|
$savePath = $outputDir . $filePrefix; |
|
43
|
|
|
|
|
44
|
|
|
$metadataCollection = BuildMetadataFromXml::buildPhoneMetadataCollection($inputFile, $liteBuild, false); |
|
45
|
|
|
$this->writeMetadataToFile($metadataCollection, $savePath); |
|
46
|
|
|
|
|
47
|
|
|
$countryCodeToRegionCodeMap = BuildMetadataFromXml::buildCountryCodeToRegionCodeMap($metadataCollection); |
|
48
|
|
|
// Sort $countryCodeToRegionCodeMap just to have the regions in order |
|
49
|
|
|
\ksort($countryCodeToRegionCodeMap); |
|
50
|
|
|
$this->writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMap, $mappingClassLocation, $mappingClass); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $metadataCollection PhoneMetadata[] |
|
55
|
|
|
* @param $filePrefix |
|
56
|
|
|
*/ |
|
57
|
|
|
private function writeMetadataToFile($metadataCollection, $filePrefix) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($metadataCollection as $metadata) { |
|
60
|
|
|
/** @var $phoneMetadata PhoneMetadata */ |
|
61
|
|
|
$regionCode = $metadata->getId(); |
|
62
|
|
|
// For non-geographical country calling codes (e.g. +800), use the country calling codes |
|
63
|
|
|
// instead of the region code to form the file name. |
|
64
|
|
|
if ($regionCode === '001' || $regionCode == '') { |
|
65
|
|
|
$regionCode = $metadata->getCountryCode(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$data = '<?php' . PHP_EOL |
|
69
|
|
|
. self::GENERATION_COMMENT . PHP_EOL |
|
70
|
|
|
. 'return ' . \var_export($metadata->toArray(), true) . ';' . PHP_EOL; |
|
71
|
|
|
|
|
72
|
|
|
\file_put_contents($filePrefix . '_' . $regionCode . '.php', $data); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMap, $outputDir, $mappingClass) |
|
77
|
|
|
{ |
|
78
|
|
|
// Find out whether the countryCodeToRegionCodeMap has any region codes or country |
|
79
|
|
|
// calling codes listed in it. |
|
80
|
|
|
$hasRegionCodes = false; |
|
81
|
|
|
foreach ($countryCodeToRegionCodeMap as $key => $listWithRegionCode) { |
|
82
|
|
|
if (\count($listWithRegionCode) > 0) { |
|
83
|
|
|
$hasRegionCodes = true; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$hasCountryCodes = (\count($countryCodeToRegionCodeMap) > 1); |
|
89
|
|
|
|
|
90
|
|
|
$variableName = \lcfirst($mappingClass); |
|
91
|
|
|
|
|
92
|
|
|
$data = '<?php' . PHP_EOL . |
|
93
|
|
|
self::GENERATION_COMMENT . PHP_EOL . |
|
94
|
|
|
'namespace libphonenumber;' . PHP_EOL . |
|
95
|
|
|
"class {$mappingClass} {" . PHP_EOL . |
|
96
|
|
|
PHP_EOL; |
|
97
|
|
|
|
|
98
|
|
|
if ($hasRegionCodes && $hasCountryCodes) { |
|
99
|
|
|
$data .= self::MAP_COMMENT . PHP_EOL; |
|
100
|
|
|
$data .= " public static \${$variableName} = " . \var_export( |
|
101
|
|
|
$countryCodeToRegionCodeMap, |
|
102
|
|
|
true |
|
103
|
|
|
) . ';' . PHP_EOL; |
|
104
|
|
|
} elseif ($hasCountryCodes) { |
|
105
|
|
|
$data .= self::COUNTRY_CODE_SET_COMMENT . PHP_EOL; |
|
106
|
|
|
$data .= " public static \${$variableName} = " . \var_export( |
|
107
|
|
|
\array_keys($countryCodeToRegionCodeMap), |
|
108
|
|
|
true |
|
109
|
|
|
) . ';' . PHP_EOL; |
|
110
|
|
|
} else { |
|
111
|
|
|
$data .= self::REGION_CODE_SET_COMMENT . PHP_EOL; |
|
112
|
|
|
$data .= " public static \${$variableName} = " . \var_export( |
|
113
|
|
|
$countryCodeToRegionCodeMap[0], |
|
114
|
|
|
true |
|
115
|
|
|
) . ';' . PHP_EOL; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$data .= PHP_EOL . |
|
119
|
|
|
'}' . PHP_EOL; |
|
120
|
|
|
|
|
121
|
|
|
\file_put_contents($outputDir . $mappingClass . '.php', $data); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|