Skip to content

Commit

Permalink
Use symfony/var-exporter to reduce metadata size
Browse files Browse the repository at this point in the history
  • Loading branch information
giggsey committed Oct 4, 2024
1 parent 39461f9 commit 93957d5
Show file tree
Hide file tree
Showing 583 changed files with 54,283 additions and 84,245 deletions.
40 changes: 15 additions & 25 deletions build/BuildMetadataPHPFromXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace libphonenumber\buildtools;

use libphonenumber\PhoneMetadata;
use Symfony\Component\VarExporter\VarExporter;

/**
* Tool to convert phone number metadata from the XML format to protocol buffer format.
Expand All @@ -14,17 +15,12 @@ class BuildMetadataPHPFromXml
{
public const GENERATION_COMMENT = <<<EOT
/**
* This file has been @generated by a phing task by {@link BuildMetadataPHPFromXml}.
* See [README.md](README.md#generating-data) for more information.
*
* Pull requests changing data in these files will not be accepted. See the
* [FAQ in the README](README.md#problems-with-invalid-numbers] on how to make
* metadata changes.
*
* Do not modify this file directly!
* libphonenumber-for-php-lite data file
* This file has been @generated from libphonenumber data
* Do not modify!
* @internal
*/
EOT;
public const MAP_COMMENT = <<<EOT
// A mapping from a country code to the region codes which denote the
Expand Down Expand Up @@ -67,25 +63,28 @@ private function writeMetadataToFile($metadataCollection, $filePrefix)

$data = '<?php' . PHP_EOL
. self::GENERATION_COMMENT . PHP_EOL
. 'return ' . \var_export($metadata->toArray(), true) . ';' . PHP_EOL;
. 'return ' . VarExporter::export($metadata->toArray()) . ';' . PHP_EOL;

\file_put_contents($filePrefix . '_' . $regionCode . '.php', $data);
}
}

private function writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMap, $outputDir, $mappingClass)
/**
* @param array<int,array<string>> $countryCodeToRegionCodeMap
*/
private function writeCountryCallingCodeMappingToFile(array $countryCodeToRegionCodeMap, string $outputDir, string $mappingClass): void
{
// Find out whether the countryCodeToRegionCodeMap has any region codes or country
// calling codes listed in it.
$hasRegionCodes = false;
foreach ($countryCodeToRegionCodeMap as $key => $listWithRegionCode) {
if (\count($listWithRegionCode) > 0) {
if ((is_countable($listWithRegionCode) ? \count($listWithRegionCode) : 0) > 0) {
$hasRegionCodes = true;
break;
}
}

$hasCountryCodes = (\count($countryCodeToRegionCodeMap) > 1);
$hasCountryCodes = \count($countryCodeToRegionCodeMap) > 1;

$variableName = \lcfirst($mappingClass);

Expand All @@ -97,22 +96,13 @@ private function writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMa

if ($hasRegionCodes && $hasCountryCodes) {
$data .= self::MAP_COMMENT . PHP_EOL;
$data .= " public static \${$variableName} = " . \var_export(
$countryCodeToRegionCodeMap,
true
) . ';' . PHP_EOL;
$data .= " public static \${$variableName} = " . VarExporter::export($countryCodeToRegionCodeMap) . ';' . PHP_EOL;
} elseif ($hasCountryCodes) {
$data .= self::COUNTRY_CODE_SET_COMMENT . PHP_EOL;
$data .= " public static \${$variableName} = " . \var_export(
\array_keys($countryCodeToRegionCodeMap),
true
) . ';' . PHP_EOL;
$data .= " public static \${$variableName} = " . VarExporter::export(\array_keys($countryCodeToRegionCodeMap)) . ';' . PHP_EOL;
} else {
$data .= self::REGION_CODE_SET_COMMENT . PHP_EOL;
$data .= " public static \${$variableName} = " . \var_export(
$countryCodeToRegionCodeMap[0],
true
) . ';' . PHP_EOL;
$data .= " public static \${$variableName} = " . VarExporter::export($countryCodeToRegionCodeMap[0]) . ';' . PHP_EOL;
}

$data .= PHP_EOL .
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
"pear/versioncontrol_git": "^0.7",
"pear/pear-core-minimal": "^1.10",
"pear/pear_exception": "^1.0",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^9.6",
"symfony/console": "^v5.2",
"symfony/var-exporter": "^5.2",
"php-coveralls/php-coveralls": "^2.0",
"friendsofphp/php-cs-fixer": "^3.64"
},
Expand Down
106 changes: 51 additions & 55 deletions src/AlternateFormatsCountryCodeSet.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?php
/**
* This file has been @generated by a phing task by {@link BuildMetadataPHPFromXml}.
* See [README.md](README.md#generating-data) for more information.
*
* Pull requests changing data in these files will not be accepted. See the
* [FAQ in the README](README.md#problems-with-invalid-numbers] on how to make
* metadata changes.
*
* Do not modify this file directly!
* libphonenumber-for-php-lite data file
* This file has been @generated from libphonenumber data
* Do not modify!
* @internal
*/

namespace libphonenumber;
Expand All @@ -16,53 +12,53 @@ class AlternateFormatsCountryCodeSet
{
// A set of all country codes for which data is available.

public static $alternateFormatsCountryCodeSet = [
0 => 7,
1 => 27,
2 => 30,
3 => 31,
4 => 34,
5 => 36,
6 => 39,
7 => 43,
8 => 44,
9 => 49,
10 => 52,
11 => 54,
12 => 55,
13 => 58,
14 => 61,
15 => 62,
16 => 64,
17 => 66,
18 => 81,
19 => 84,
20 => 90,
21 => 91,
22 => 94,
23 => 95,
24 => 255,
25 => 350,
26 => 351,
27 => 352,
28 => 358,
29 => 359,
30 => 372,
31 => 373,
32 => 380,
33 => 381,
34 => 385,
35 => 505,
36 => 506,
37 => 595,
38 => 675,
39 => 676,
40 => 679,
41 => 855,
42 => 856,
43 => 971,
44 => 972,
45 => 995,
public static $alternateFormatsCountryCodeSet = [
7,
27,
30,
31,
34,
36,
39,
43,
44,
49,
52,
54,
55,
58,
61,
62,
64,
66,
81,
84,
90,
91,
94,
95,
255,
350,
351,
352,
358,
359,
372,
373,
380,
381,
385,
505,
506,
595,
675,
676,
679,
855,
856,
971,
972,
995,
];

}
Loading

0 comments on commit 93957d5

Please sign in to comment.