44
55namespace libphonenumber \buildtools ;
66
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 ;
910
1011use function array_keys ;
1112use function count ;
1213use function file_put_contents ;
1314use function ksort ;
14- use function lcfirst ;
1515
1616/**
1717 * Tool to convert phone number metadata from the XML format to protocol buffer format.
2222class BuildMetadataPHPFromXml
2323{
2424 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
3129
3230 EOT ;
3331 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[]>
4137
4238 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 ;
5947
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 );
6252
6353 $ countryCodeToRegionCodeMap = BuildMetadataFromXml::buildCountryCodeToRegionCodeMap ($ metadataCollection );
6454 // Sort $countryCodeToRegionCodeMap just to have the regions in order
@@ -67,9 +57,9 @@ public function start(string $inputFile, string $outputDir, string $filePrefix,
6757 }
6858
6959 /**
70- * @param PhoneMetadata [] $metadataCollection
60+ * @param PhoneMetadataBuilder [] $metadataCollection
7161 */
72- private function writeMetadataToFile (array $ metadataCollection , string $ filePrefix ): void
62+ private function writeMetadataToFile (array $ metadataCollection , string $ directory , string $ namespaceAndClassPrefix ): void
7363 {
7464 foreach ($ metadataCollection as $ metadata ) {
7565 $ regionCode = $ metadata ->getId ();
@@ -79,11 +69,17 @@ private function writeMetadataToFile(array $metadataCollection, string $filePref
7969 $ regionCode = $ metadata ->getCountryCode ();
8070 }
8171
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 );
8576
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 ));
8783 }
8884 }
8985
@@ -104,29 +100,32 @@ private function writeCountryCallingCodeMappingToFile(array $countryCodeToRegion
104100
105101 $ hasCountryCodes = count ($ countryCodeToRegionCodeMap ) > 1 ;
106102
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 );
108108
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 ' );
115113
116114 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 ) ;
119117 } 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 ) ;
122120 } 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 ) ;
125123 }
126124
127- $ data .= PHP_EOL .
128- '} ' . PHP_EOL ;
125+ $ constant ->setPublic ();
126+
127+ $ printer = new PsrPrinter ();
129128
130- file_put_contents ($ outputDir . $ mappingClass . '.php ' , $ data );
129+ file_put_contents ($ outputDir . $ mappingClass . '.php ' , $ printer -> printFile ( $ file ) );
131130 }
132131}
0 commit comments