Skip to content

Commit

Permalink
Merge b239ef7 into 0442531
Browse files Browse the repository at this point in the history
  • Loading branch information
giggsey committed Nov 10, 2018
2 parents 0442531 + b239ef7 commit 3b7acba
Show file tree
Hide file tree
Showing 67 changed files with 3,129 additions and 3,120 deletions.
2 changes: 1 addition & 1 deletion METADATA-VERSION.txt
Expand Up @@ -2,4 +2,4 @@
# It can be a commit, branch or tag of the https://github.com/googlei18n/libphonenumber project
#
# For more information, look at the phing tasks in build.xml
v8.9.16
865da605da12b01053c4f053310bac7c5fbb7935
134 changes: 69 additions & 65 deletions build/BuildMetadataFromXml.php
Expand Up @@ -15,45 +15,45 @@
class BuildMetadataFromXml
{
// String constants used to fetch the XML nodes and attributes.
const CARRIER_CODE_FORMATTING_RULE = "carrierCodeFormattingRule";
const COUNTRY_CODE = "countryCode";
const EMERGENCY = "emergency";
const EXAMPLE_NUMBER = "exampleNumber";
const FIXED_LINE = "fixedLine";
const FORMAT = "format";
const GENERAL_DESC = "generalDesc";
const INTERNATIONAL_PREFIX = "internationalPrefix";
const INTL_FORMAT = "intlFormat";
const LEADING_DIGITS = "leadingDigits";
const MOBILE_NUMBER_PORTABLE_REGION = "mobileNumberPortableRegion";
const MAIN_COUNTRY_FOR_CODE = "mainCountryForCode";
const MOBILE = "mobile";
const NATIONAL_NUMBER_PATTERN = "nationalNumberPattern";
const NATIONAL_PREFIX = "nationalPrefix";
const NATIONAL_PREFIX_FORMATTING_RULE = "nationalPrefixFormattingRule";
const NATIONAL_PREFIX_OPTIONAL_WHEN_FORMATTING = "nationalPrefixOptionalWhenFormatting";
const NATIONAL_PREFIX_FOR_PARSING = "nationalPrefixForParsing";
const NATIONAL_PREFIX_TRANSFORM_RULE = "nationalPrefixTransformRule";
const NO_INTERNATIONAL_DIALLING = "noInternationalDialling";
const NUMBER_FORMAT = "numberFormat";
const PAGER = "pager";
const CARRIER_CODE_FORMATTING_RULE = 'carrierCodeFormattingRule';
const COUNTRY_CODE = 'countryCode';
const EMERGENCY = 'emergency';
const EXAMPLE_NUMBER = 'exampleNumber';
const FIXED_LINE = 'fixedLine';
const FORMAT = 'format';
const GENERAL_DESC = 'generalDesc';
const INTERNATIONAL_PREFIX = 'internationalPrefix';
const INTL_FORMAT = 'intlFormat';
const LEADING_DIGITS = 'leadingDigits';
const MOBILE_NUMBER_PORTABLE_REGION = 'mobileNumberPortableRegion';
const MAIN_COUNTRY_FOR_CODE = 'mainCountryForCode';
const MOBILE = 'mobile';
const NATIONAL_NUMBER_PATTERN = 'nationalNumberPattern';
const NATIONAL_PREFIX = 'nationalPrefix';
const NATIONAL_PREFIX_FORMATTING_RULE = 'nationalPrefixFormattingRule';
const NATIONAL_PREFIX_OPTIONAL_WHEN_FORMATTING = 'nationalPrefixOptionalWhenFormatting';
const NATIONAL_PREFIX_FOR_PARSING = 'nationalPrefixForParsing';
const NATIONAL_PREFIX_TRANSFORM_RULE = 'nationalPrefixTransformRule';
const NO_INTERNATIONAL_DIALLING = 'noInternationalDialling';
const NUMBER_FORMAT = 'numberFormat';
const PAGER = 'pager';
const CARRIER_SPECIFIC = 'carrierSpecific';
const PATTERN = "pattern";
const PERSONAL_NUMBER = "personalNumber";
const POSSIBLE_LENGTHS = "possibleLengths";
const NATIONAL = "national";
const LOCAL_ONLY = "localOnly";
const PREFERRED_EXTN_PREFIX = "preferredExtnPrefix";
const PREFERRED_INTERNATIONAL_PREFIX = "preferredInternationalPrefix";
const PREMIUM_RATE = "premiumRate";
const SHARED_COST = "sharedCost";
const SHORT_CODE = "shortCode";
const SMS_SERVICES = "smsServices";
const STANDARD_RATE = "standardRate";
const TOLL_FREE = "tollFree";
const UAN = "uan";
const VOICEMAIL = "voicemail";
const VOIP = "voip";
const PATTERN = 'pattern';
const PERSONAL_NUMBER = 'personalNumber';
const POSSIBLE_LENGTHS = 'possibleLengths';
const NATIONAL = 'national';
const LOCAL_ONLY = 'localOnly';
const PREFERRED_EXTN_PREFIX = 'preferredExtnPrefix';
const PREFERRED_INTERNATIONAL_PREFIX = 'preferredInternationalPrefix';
const PREMIUM_RATE = 'premiumRate';
const SHARED_COST = 'sharedCost';
const SHORT_CODE = 'shortCode';
const SMS_SERVICES = 'smsServices';
const STANDARD_RATE = 'standardRate';
const TOLL_FREE = 'tollFree';
const UAN = 'uan';
const VOICEMAIL = 'voicemail';
const VOIP = 'voip';

private static $phoneNumberDescsWithoutMatchingTypes = array(
self::NO_INTERNATIONAL_DIALLING
Expand All @@ -70,13 +70,13 @@ public static function validateRE($regex, $removeWhitespace = false)
$compressedRegex = $removeWhitespace ? preg_replace('/\\s/', '', $regex) : $regex;
// Match regex against an empty string to check the regex is valid
if (preg_match('/' . $compressedRegex . '/', '') === false) {
throw new \RuntimeException("Regex error: " . preg_last_error());
throw new \RuntimeException('Regex error: ' . preg_last_error());
}
// We don't ever expect to see | followed by a ) in our metadata - this would be an indication
// of a bug. If one wants to make something optional, we prefer ? to using an empty group.
$errorIndex = strpos($compressedRegex, '|)');
if ($errorIndex !== false) {
throw new \RuntimeException("| followed by )");
throw new \RuntimeException('| followed by )');
}
// return the regex if it is of correct syntax, i.e. compile did not fail with a
return $compressedRegex;
Expand Down Expand Up @@ -104,7 +104,7 @@ public static function buildPhoneMetadataCollection($inputXmlFile, $liteBuild, $
$isAlternateFormatsMetadata = strpos($inputXmlFile, 'PhoneNumberAlternateFormats');
}

$territories = $document->getElementsByTagName("territory");
$territories = $document->getElementsByTagName('territory');
$metadataCollection = array();

$metadataFilter = self::getMetadataFilter($liteBuild, $specialBuild);
Expand All @@ -113,10 +113,10 @@ public static function buildPhoneMetadataCollection($inputXmlFile, $liteBuild, $
/** @var $territoryElement \DOMElement */
// For the main metadata file this should always be set, but for other supplementary data
// files the country calling code may be all that is needed.
if ($territoryElement->hasAttribute("id")) {
$regionCode = $territoryElement->getAttribute("id");
if ($territoryElement->hasAttribute('id')) {
$regionCode = $territoryElement->getAttribute('id');
} else {
$regionCode = "";
$regionCode = '';
}
$metadata = self::loadCountryMetadata($regionCode, $territoryElement, $isShortNumberMetadata, $isAlternateFormatsMetadata);
$metadataFilter->filterMetadata($metadata);
Expand Down Expand Up @@ -157,7 +157,7 @@ public static function getMetadataFilter($liteBuild, $specialBuild)
{
if ($specialBuild) {
if ($liteBuild) {
throw new \RuntimeException("liteBuild and specialBuild may not both be set");
throw new \RuntimeException('liteBuild and specialBuild may not both be set');
}
return MetadataFilter::forSpecialBuild();
}
Expand All @@ -176,7 +176,7 @@ public static function getMetadataFilter($liteBuild, $specialBuild)
*/
public static function getNationalPrefix(\DOMElement $element)
{
return $element->hasAttribute(self::NATIONAL_PREFIX) ? $element->getAttribute(self::NATIONAL_PREFIX) : "";
return $element->hasAttribute(self::NATIONAL_PREFIX) ? $element->getAttribute(self::NATIONAL_PREFIX) : '';
}

/**
Expand All @@ -190,8 +190,8 @@ public static function getNationalPrefixFormattingRuleFromElement(\DOMElement $e
{
$nationalPrefixFormattingRule = $element->getAttribute(self::NATIONAL_PREFIX_FORMATTING_RULE);
// Replace $NP with national prefix and $FG with the first group ($1).
$nationalPrefixFormattingRule = str_replace('$NP', $nationalPrefix, $nationalPrefixFormattingRule);
$nationalPrefixFormattingRule = str_replace('$FG', '$1', $nationalPrefixFormattingRule);
$nationalPrefixFormattingRule = str_replace(array('$NP', '$FG'), array($nationalPrefix, '$1'),
$nationalPrefixFormattingRule);
return $nationalPrefixFormattingRule;
}

Expand Down Expand Up @@ -264,7 +264,7 @@ public static function loadAvailableFormats(
$nationalPrefixFormattingRule,
$nationalPrefixOptionalWhenFormatting
) {
$carrierCodeFormattingRule = "";
$carrierCodeFormattingRule = '';
if ($element->hasAttribute(self::CARRIER_CODE_FORMATTING_RULE)) {
$carrierCodeFormattingRule = self::validateRE(self::getDomesticCarrierCodeFormattingRuleFromElement($element, $nationalPrefix));
}
Expand All @@ -286,7 +286,7 @@ public static function loadAvailableFormats(
$format->setNationalPrefixFormattingRule($nationalPrefixFormattingRule);
}
if ($numberFormatElement->hasAttribute(self::NATIONAL_PREFIX_OPTIONAL_WHEN_FORMATTING)) {
$format->setNationalPrefixOptionalWhenFormatting($numberFormatElement->getAttribute(self::NATIONAL_PREFIX_OPTIONAL_WHEN_FORMATTING) == 'true' ? true : false);
$format->setNationalPrefixOptionalWhenFormatting($numberFormatElement->getAttribute(self::NATIONAL_PREFIX_OPTIONAL_WHEN_FORMATTING) === 'true' ? true : false);
} else {
$format->setNationalPrefixOptionalWhenFormatting($nationalPrefixOptionalWhenFormatting);
}
Expand Down Expand Up @@ -324,8 +324,8 @@ public static function getDomesticCarrierCodeFormattingRuleFromElement(\DOMEleme
{
$carrierCodeFormattingRule = $element->getAttribute(self::CARRIER_CODE_FORMATTING_RULE);
// Replace $FG with the first group ($1) and $NP with the national prefix.
$carrierCodeFormattingRule = str_replace('$NP', $nationalPrefix, $carrierCodeFormattingRule);
$carrierCodeFormattingRule = str_replace('$FG', '$1', $carrierCodeFormattingRule);
$carrierCodeFormattingRule = str_replace(array('$NP', '$FG'), array($nationalPrefix, '$1'),
$carrierCodeFormattingRule);
return $carrierCodeFormattingRule;
}

Expand All @@ -349,7 +349,7 @@ public static function loadNationalFormat(
$formatPattern = $numberFormatElement->getElementsByTagName(self::FORMAT);
if ($formatPattern->length != 1) {
$countryId = strlen($metadata->getId()) > 0 ? $metadata->getId() : $metadata->getCountryCode();
throw new \RuntimeException("Invalid number of format patterns for country: " . $countryId);
throw new \RuntimeException('Invalid number of format patterns for country: ' . $countryId);
}
$nationalFormat = $formatPattern->item(0)->firstChild->nodeValue;
$format->setFormat($nationalFormat);
Expand Down Expand Up @@ -393,15 +393,17 @@ public static function loadInternationalFormat(

if ($intlFormatPattern->length > 1) {
$countryId = strlen($metadata->getId()) > 0 ? $metadata->getId() : $metadata->getCountryCode();
throw new \RuntimeException("Invalid number of intlFormat patterns for country: " . $countryId);
} elseif ($intlFormatPattern->length == 0) {
throw new \RuntimeException('Invalid number of intlFormat patterns for country: ' . $countryId);
}

if ($intlFormatPattern->length == 0) {
// Default to use the same as the national pattern if none is defined.
$intlFormat->mergeFrom($nationalFormat);
} else {
$intlFormat->setPattern($numberFormatElement->getAttribute(self::PATTERN));
self::setLeadingDigitsPatterns($numberFormatElement, $intlFormat);
$intlFormatPatternValue = $intlFormatPattern->item(0)->firstChild->nodeValue;
if ($intlFormatPatternValue !== "NA") {
if ($intlFormatPatternValue !== 'NA') {
$intlFormat->setFormat($intlFormatPatternValue);
}
$hasExplicitIntlFormatDefined = true;
Expand Down Expand Up @@ -467,20 +469,22 @@ public static function setRelevantDescPatterns(PhoneMetadata $metadata, \DOMElem
private static function parsePossibleLengthStringToSet($possibleLengthString)
{
if (strlen($possibleLengthString) === 0) {
throw new \RuntimeException("Empty possibleLength string found.");
throw new \RuntimeException('Empty possibleLength string found.');
}

$lengths = explode(",", $possibleLengthString);
$lengths = explode(',', $possibleLengthString);
$lengthSet = array();


$lengthLength = count($lengths);
for ($i = 0; $i < $lengthLength; $i++) {
$lengthSubstring = $lengths[$i];
if (strlen($lengthSubstring) === 0) {
throw new \RuntimeException("Leading, trailing or adjacent commas in possible "
throw new \RuntimeException('Leading, trailing or adjacent commas in possible '
. "length string {$possibleLengthString}, these should only separate numbers or ranges.");
} elseif (substr($lengthSubstring, 0, 1) === '[') {
}

if (substr($lengthSubstring, 0, 1) === '[') {
if (substr($lengthSubstring, -1) !== ']') {
throw new \RuntimeException("Missing end of range character in possible length string {$possibleLengthString}.");
}
Expand All @@ -501,7 +505,7 @@ private static function parsePossibleLengthStringToSet($possibleLengthString)
if (in_array($j, $lengthSet)) {
throw new \RuntimeException("Duplicate length element found ({$j}) in possibleLength string {$possibleLengthString}.");
}
$lengthSet[] = (int)$j;
$lengthSet[] = $j;
}
} else {
$length = $lengthSubstring;
Expand Down Expand Up @@ -544,7 +548,7 @@ private static function populatePossibleLengthSets(\DOMElement $data, &$lengths,
$thisElementLocalOnlyLengths = self::parsePossibleLengthStringToSet($localLengths);
$intersection = array_intersect($thisElementLengths, $thisElementLocalOnlyLengths);
if (count($intersection) > 0) {
throw new \RuntimeException("Possible length(s) found specified as a normal and local-only length: [" . implode(',', $intersection) . '].');
throw new \RuntimeException('Possible length(s) found specified as a normal and local-only length: [' . implode(',', $intersection) . '].');
}
// We check again when we set these lengths on the metadata itself in setPossibleLengths
// that the elements in localOnly are not also in lengths. For e.g. the generalDesc, it
Expand Down Expand Up @@ -613,7 +617,7 @@ public static function setPossibleLengthsGeneralDesc(PhoneNumberDesc $generalDes
self::populatePossibleLengthSets($shortCodeDesc, $lengths, $localOnlyLengths);
}
if (count($localOnlyLengths) > 0) {
throw new \RuntimeException("Found local-only lengths in short-number metadata");
throw new \RuntimeException('Found local-only lengths in short-number metadata');
}
}
self::setPossibleLengths($lengths, $localOnlyLengths, null, $generalDesc);
Expand Down Expand Up @@ -708,10 +712,10 @@ public static function processPhoneNumberDescElement(
}

if ($parentDesc != null) {
if ($parentDesc->getNationalNumberPattern() !== "") {
if ($parentDesc->getNationalNumberPattern() !== '') {
$numberDesc->setNationalNumberPattern($parentDesc->getNationalNumberPattern());
}
if ($parentDesc->getExampleNumber() !== "") {
if ($parentDesc->getExampleNumber() !== '') {
$numberDesc->setExampleNumber($parentDesc->getExampleNumber());
}
}
Expand Down
12 changes: 6 additions & 6 deletions build/BuildMetadataPHPFromXml.php
Expand Up @@ -69,7 +69,7 @@ private function writeMetadataToFile($metadataCollection, $filePrefix)
. self::GENERATION_COMMENT . PHP_EOL
. 'return ' . var_export($metadata->toArray(), true) . ';' . PHP_EOL;

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

Expand All @@ -91,7 +91,7 @@ private function writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMa

$data = '<?php' . PHP_EOL .
self::GENERATION_COMMENT . PHP_EOL .
"namespace libphonenumber;" . PHP_EOL .
'namespace libphonenumber;' . PHP_EOL .
"class {$mappingClass} {" . PHP_EOL .
PHP_EOL;

Expand All @@ -100,23 +100,23 @@ private function writeCountryCallingCodeMappingToFile($countryCodeToRegionCodeMa
$data .= " public static \${$variableName} = " . var_export(
$countryCodeToRegionCodeMap,
true
) . ";" . PHP_EOL;
) . ';' . PHP_EOL;
} elseif ($hasCountryCodes) {
$data .= self::COUNTRY_CODE_SET_COMMENT . PHP_EOL;
$data .= " public static \${$variableName} = " . var_export(
array_keys($countryCodeToRegionCodeMap),
true
) . ";" . PHP_EOL;
) . ';' . PHP_EOL;
} else {
$data .= self::REGION_CODE_SET_COMMENT . PHP_EOL;
$data .= " public static \${$variableName} = " . var_export(
$countryCodeToRegionCodeMap[0],
true
) . ";" . PHP_EOL;
) . ';' . PHP_EOL;
}

$data .= PHP_EOL .
"}" . PHP_EOL;
'}' . PHP_EOL;

file_put_contents($outputDir . $mappingClass . '.php', $data);
}
Expand Down
11 changes: 5 additions & 6 deletions build/GeneratePhonePrefixData.php
Expand Up @@ -192,8 +192,7 @@ private function parseTextFile($filePath, \Closure $handler)

foreach ($data as $line) {
// Remove \n
$line = str_replace("\n", "", $line);
$line = str_replace("\r", "", $line);
$line = str_replace(array("\n", "\r"), '', $line);
$line = trim($line);

if (strlen($line) == 0 || substr($line, 0, 1) == '#') {
Expand Down Expand Up @@ -289,12 +288,12 @@ private function getLanguageFromTextFile($textFile)
*/
private function removeEmptyEnglishMappings(&$mappings, $language)
{
if ($language != "en") {
if ($language != 'en') {
return;
}

foreach ($mappings as $k => $v) {
if ($v == "") {
if ($v == '') {
unset($mappings[$k]);
}
}
Expand Down Expand Up @@ -326,7 +325,7 @@ private function makeDataFallbackToEnglish($textFile, &$mappings)

private function getEnglishDataPath($textFile)
{
return "en" . DIRECTORY_SEPARATOR . substr($textFile, 3);
return 'en' . DIRECTORY_SEPARATOR . substr($textFile, 3);
}

private function compressAccordingToEnglishData($englishMap, &$nonEnglishMap)
Expand All @@ -338,7 +337,7 @@ private function compressAccordingToEnglishData($englishMap, &$nonEnglishMap)
if (!$this->hasOverlappingPrefix($prefix, $nonEnglishMap)) {
unset($nonEnglishMap[$prefix]);
} else {
$nonEnglishMap[$prefix] = "";
$nonEnglishMap[$prefix] = '';
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions build/GenerateTimeZonesMapData.php
Expand Up @@ -33,7 +33,7 @@ public function __construct($inputFile, $outputDir)
$this->inputTextFile = $inputFile;

if (!is_readable($this->inputTextFile)) {
throw new \RuntimeException("The provided input text file does not exist.");
throw new \RuntimeException('The provided input text file does not exist.');
}

$data = $this->parseTextFile();
Expand All @@ -52,8 +52,7 @@ private function parseTextFile()

foreach ($data as $line) {
// Remove \n
$line = str_replace("\n", "", $line);
$line = str_replace("\r", "", $line);
$line = str_replace(array("\n", "\r"), '', $line);
$line = trim($line);

if (strlen($line) == 0 || substr($line, 0, 1) == '#') {
Expand Down

0 comments on commit 3b7acba

Please sign in to comment.