4
4
5
5
namespace libphonenumber \buildtools ;
6
6
7
- use libphonenumber \PhoneMetadata ;
8
- use Symfony \Component \VarExporter \VarExporter ;
7
+ use libphonenumber \buildtools \Builders \PhoneMetadataBuilder ;
8
+ use Nette \PhpGenerator \PhpFile ;
9
+ use Nette \PhpGenerator \PsrPrinter ;
9
10
10
11
use function array_keys ;
11
12
use function count ;
12
13
use function file_put_contents ;
13
14
use function ksort ;
14
- use function lcfirst ;
15
15
16
16
/**
17
17
* Tool to convert phone number metadata from the XML format to protocol buffer format.
22
22
class BuildMetadataPHPFromXml
23
23
{
24
24
public const GENERATION_COMMENT = <<<EOT
25
- /**
26
- * libphonenumber-for-php data file
27
- * This file has been @generated from libphonenumber data
28
- * Do not modify!
29
- * @internal
30
- */
25
+ libphonenumber-for-php data file
26
+ This file has been @generated from libphonenumber data
27
+ Do not modify!
28
+ @internal
31
29
32
30
EOT ;
33
31
public const MAP_COMMENT = <<<EOT
34
- /**
35
- * A mapping from a country code to the region codes which denote the
36
- * country/region represented by that country code. In the case of multiple
37
- * countries sharing a calling code, such as the NANPA countries, the one
38
- * indicated with "isMainCountryForCode" in the metadata should be first.
39
- * @var array<int,string[]>
40
- */
32
+ A mapping from a country code to the region codes which denote the
33
+ country/region represented by that country code. In the case of multiple
34
+ countries sharing a calling code, such as the NANPA countries, the one
35
+ indicated with "isMainCountryForCode" in the metadata should be first.
36
+ @var array<int,string[]>
41
37
42
38
EOT ;
43
- public const COUNTRY_CODE_SET_COMMENT = <<<php
44
- /**
45
- * A set of all country calling codes for which data is available.
46
- * @var int[]
47
- */
48
- php ;
49
- public const REGION_CODE_SET_COMMENT = <<<php
50
- /**
51
- * A set of all region codes for which data is available.
52
- * @var string[]
53
- */
54
- php ;
55
-
56
- public function start (string $ inputFile , string $ outputDir , string $ filePrefix , string $ mappingClass , string $ mappingClassLocation , bool $ liteBuild ): void
57
- {
58
- $ savePath = $ outputDir . $ filePrefix ;
39
+ public const COUNTRY_CODE_SET_COMMENT = <<<EOT
40
+ A set of all country calling codes for which data is available.
41
+ @var int[]
42
+ EOT ;
43
+ public const REGION_CODE_SET_COMMENT = <<<EOT
44
+ A set of all region codes for which data is available.
45
+ @var string[]
46
+ EOT ;
59
47
60
- $ metadataCollection = BuildMetadataFromXml::buildPhoneMetadataCollection ($ inputFile , $ liteBuild , false );
61
- $ this ->writeMetadataToFile ($ metadataCollection , $ savePath );
48
+ public function start (string $ inputFile , string $ outputDir , string $ namespaceAndClassPrefix , string $ mappingClass , string $ mappingClassLocation ): void
49
+ {
50
+ $ metadataCollection = BuildMetadataFromXml::buildPhoneMetadataCollection ($ inputFile );
51
+ $ this ->writeMetadataToFile ($ metadataCollection , $ outputDir , $ namespaceAndClassPrefix );
62
52
63
53
$ countryCodeToRegionCodeMap = BuildMetadataFromXml::buildCountryCodeToRegionCodeMap ($ metadataCollection );
64
54
// Sort $countryCodeToRegionCodeMap just to have the regions in order
@@ -67,9 +57,9 @@ public function start(string $inputFile, string $outputDir, string $filePrefix,
67
57
}
68
58
69
59
/**
70
- * @param PhoneMetadata [] $metadataCollection
60
+ * @param PhoneMetadataBuilder [] $metadataCollection
71
61
*/
72
- private function writeMetadataToFile (array $ metadataCollection , string $ filePrefix ): void
62
+ private function writeMetadataToFile (array $ metadataCollection , string $ directory , string $ namespaceAndClassPrefix ): void
73
63
{
74
64
foreach ($ metadataCollection as $ metadata ) {
75
65
$ regionCode = $ metadata ->getId ();
@@ -79,11 +69,17 @@ private function writeMetadataToFile(array $metadataCollection, string $filePref
79
69
$ regionCode = $ metadata ->getCountryCode ();
80
70
}
81
71
82
- $ data = '<?php ' . PHP_EOL
83
- . self ::GENERATION_COMMENT . PHP_EOL
84
- . 'return ' . VarExporter::export ($ metadata ->toArray ()) . '; ' . PHP_EOL ;
72
+ $ pos = strrpos ($ namespaceAndClassPrefix , '\\' );
73
+
74
+ $ namespace = substr ($ namespaceAndClassPrefix , 0 , $ pos );
75
+ $ classPrefix = substr ($ namespaceAndClassPrefix , $ pos + 1 );
85
76
86
- file_put_contents ($ filePrefix . '_ ' . $ regionCode . '.php ' , $ data );
77
+ $ data = $ metadata ->toFile ($ classPrefix . '_ ' . $ regionCode , $ namespace );
78
+ $ data ->addComment (self ::GENERATION_COMMENT );
79
+
80
+ $ printer = new PsrPrinter ();
81
+
82
+ file_put_contents ($ directory . DIRECTORY_SEPARATOR . $ classPrefix . '_ ' . $ regionCode . '.php ' , $ printer ->printFile ($ data ));
87
83
}
88
84
}
89
85
@@ -104,29 +100,32 @@ private function writeCountryCallingCodeMappingToFile(array $countryCodeToRegion
104
100
105
101
$ hasCountryCodes = count ($ countryCodeToRegionCodeMap ) > 1 ;
106
102
107
- $ variableName = lcfirst ($ mappingClass );
103
+ $ variableName = strtoupper (preg_replace ('/(?<!^)[A-Z]/ ' , '_$0 ' , $ mappingClass ));
104
+
105
+ $ file = new PhpFile ();
106
+ $ file ->setStrictTypes ();
107
+ $ file ->addComment (self ::GENERATION_COMMENT );
108
108
109
- $ data = '<?php ' . PHP_EOL .
110
- 'declare(strict_types=1); ' . PHP_EOL .
111
- 'namespace libphonenumber; ' . PHP_EOL .
112
- self ::GENERATION_COMMENT . PHP_EOL .
113
- "class {$ mappingClass } { " . PHP_EOL .
114
- PHP_EOL ;
109
+ $ namespace = $ file ->addNamespace ('libphonenumber ' );
110
+
111
+ $ class = $ namespace ->addClass ($ mappingClass );
112
+ $ class ->addComment ('@internal ' );
115
113
116
114
if ($ hasRegionCodes && $ hasCountryCodes ) {
117
- $ data .= self :: MAP_COMMENT . PHP_EOL ;
118
- $ data .= " public static array \${ $ variableName } = " . VarExporter:: export ( $ countryCodeToRegionCodeMap ) . ' ; ' . PHP_EOL ;
115
+ $ constant = $ class -> addConstant ( $ variableName , $ countryCodeToRegionCodeMap ) ;
116
+ $ constant -> setComment ( self :: MAP_COMMENT ) ;
119
117
} elseif ($ hasCountryCodes ) {
120
- $ data .= self :: COUNTRY_CODE_SET_COMMENT . PHP_EOL ;
121
- $ data .= " public static array \${ $ variableName } = " . VarExporter:: export ( array_keys ( $ countryCodeToRegionCodeMap )) . ' ; ' . PHP_EOL ;
118
+ $ constant = $ class -> addConstant ( $ variableName , array_keys ( $ countryCodeToRegionCodeMap )) ;
119
+ $ constant -> setComment ( self :: COUNTRY_CODE_SET_COMMENT ) ;
122
120
} else {
123
- $ data .= self :: REGION_CODE_SET_COMMENT . PHP_EOL ;
124
- $ data .= " public static array \${ $ variableName } = " . VarExporter:: export ( $ countryCodeToRegionCodeMap [ 0 ]) . ' ; ' . PHP_EOL ;
121
+ $ constant = $ class -> addConstant ( $ variableName , $ countryCodeToRegionCodeMap [ 0 ]) ;
122
+ $ constant -> setComment ( self :: REGION_CODE_SET_COMMENT ) ;
125
123
}
126
124
127
- $ data .= PHP_EOL .
128
- '} ' . PHP_EOL ;
125
+ $ constant ->setPublic ();
126
+
127
+ $ printer = new PsrPrinter ();
129
128
130
- file_put_contents ($ outputDir . $ mappingClass . '.php ' , $ data );
129
+ file_put_contents ($ outputDir . $ mappingClass . '.php ' , $ printer -> printFile ( $ file ) );
131
130
}
132
131
}
0 commit comments