Skip to content

Commit

Permalink
Merge pull request #564 from giggsey/upstream-8.13.8
Browse files Browse the repository at this point in the history
Upstream changes for v8.13.8
  • Loading branch information
giggsey committed Mar 27, 2023
2 parents 281b12d + 3329fcd commit 2d3f07b
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 65 deletions.
2 changes: 1 addition & 1 deletion METADATA-VERSION.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# It can be a commit, branch or tag of the https://github.com/google/libphonenumber project
#
# For more information, look at the phing tasks in build.xml
v8.13.7
v8.13.8
6 changes: 3 additions & 3 deletions src/AsYouTypeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private function inputDigitWithOptionToRememberPosition($nextChar, $rememberPosi
case 1:
case 2:
return $this->accruedInput;
/** @noinspection PhpMissingBreakStatementInspection */
/** @noinspection PhpMissingBreakStatementInspection */
case 3:
if ($this->attemptToExtractIdd()) {
$this->isExpectingCountryCallingCode = true;
Expand All @@ -485,8 +485,8 @@ private function inputDigitWithOptionToRememberPosition($nextChar, $rememberPosi
$this->extractedNationalPrefix = $this->removeNationalPrefixFromNationalNumber();
return $this->attemptToChooseFormattingPattern();
}
// fall through
// no break
// fall through
// no break
default:
if ($this->isExpectingCountryCallingCode) {
if ($this->attemptToExtractCountryCallingCode()) {
Expand Down
30 changes: 21 additions & 9 deletions src/NumberParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,31 @@
*/
class NumberParseException extends \Exception
{
/**
* The country code supplied did not belong to a supported country or non-geographical entity.
*/
const INVALID_COUNTRY_CODE = 0;
// This generally indicates the string passed in had less than 3 digits in it. More
// specifically, the number failed to match the regular expression VALID_PHONE_NUMBER in
// PhoneNumberUtil.
/**
* This indicates the string passed is not a valid number. Either the string had less than 3
* digits in it or had an invalid phone-context parameter. More specifically, the number failed
* to match the regular expression VALID_PHONE_NUMBER, RFC3966_GLOBAL_NUMBER_DIGITS, or
* RFC3966_DOMAINNAME in PhoneNumberUtil
*/
const NOT_A_NUMBER = 1;
// This indicates the string started with an international dialing prefix, but after this was
// stripped from the number, had less digits than any valid phone number (including country
// code) could have.
/**
* This indicates the string started with an international dialing prefix, but after this was
* stripped from the number, had less digits than any valid phone number (including country
* code) could have.
*/
const TOO_SHORT_AFTER_IDD = 2;
// This indicates the string, after any country code has been stripped, had less digits than any
// valid phone number could have.
/**
* This indicates the string, after any country code has been stripped, had less digits than any
* valid phone number could have.
*/
const TOO_SHORT_NSN = 3;
// This indicates the string had more digits than any valid phone number could have.
/**
* This indicates the string had more digits than any valid phone number could have.
*/
const TOO_LONG = 4;

protected $errorType;
Expand Down
4 changes: 2 additions & 2 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public function equals(PhoneNumber $other)
(!$this->hasCountryCodeSource() || $this->getCountryCodeSource() == $other->getCountryCodeSource());
$samePrefCar = $this->hasPreferredDomesticCarrierCode() == $other->hasPreferredDomesticCarrierCode() &&
(!$this->hasPreferredDomesticCarrierCode() || $this->getPreferredDomesticCarrierCode(
) == $other->getPreferredDomesticCarrierCode());
) == $other->getPreferredDomesticCarrierCode());
return $sameType && $sameCountry && $sameNational && $sameExt && $sameLead && $sameZeros && $sameRaw && $sameCountrySource && $samePrefCar;
}

Expand Down Expand Up @@ -601,7 +601,7 @@ public function __unserialize($data)
$this->rawInput,
$this->countryCodeSource,
$this->preferredDomesticCarrierCode
) = $data;
) = $data;

if ($this->numberOfLeadingZeros > 1) {
$this->hasNumberOfLeadingZeros = true;
Expand Down
4 changes: 2 additions & 2 deletions src/PhoneNumberMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ public static function allNumberGroupsAreExactlyPresent(
// Starting from the end, go through in reverse, excluding the first group, and check the
// candidate and number groups are the same.
for ($formattedNumberGroupIndex = (\count($formattedNumberGroups) - 1);
$formattedNumberGroupIndex > 0 && $candidateNumberGroupIndex >= 0;
$formattedNumberGroupIndex--, $candidateNumberGroupIndex--) {
$formattedNumberGroupIndex > 0 && $candidateNumberGroupIndex >= 0;
$formattedNumberGroupIndex--, $candidateNumberGroupIndex--) {
if ($candidateGroups[$candidateNumberGroupIndex] != $formattedNumberGroups[$formattedNumberGroupIndex]) {
return false;
}
Expand Down
108 changes: 94 additions & 14 deletions src/PhoneNumberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ class PhoneNumberUtil
* @internal
*/
public static $EXTN_PATTERNS_FOR_MATCHING;

// Regular expression of valid global-number-digits for the phone-context parameter, following the
// syntax defined in RFC3966.
protected static $RFC3966_VISUAL_SEPARATOR = "[\\-\\.\\(\\)]?";
protected static $RFC3966_PHONE_DIGIT;
protected static $RFC3966_GLOBAL_NUMBER_DIGITS;

// Regular expression of valid domainname for the phone-context parameter, following the syntax
// defined in RFC3966.
protected static $ALPHANUM;
protected static $RFC3966_DOMAINLABEL;
protected static $RFC3966_TOPLABEL;
protected static $RFC3966_DOMAINNAME;

protected static $EXTN_PATTERN;
protected static $VALID_PHONE_NUMBER_PATTERN;
protected static $MIN_LENGTH_PHONE_NUMBER_PATTERN;
Expand Down Expand Up @@ -346,6 +360,7 @@ protected function __construct(MetadataSourceInterface $metadataSource, $country
$this->matcherAPI = RegexBasedMatcher::create();
static::initExtnPatterns();
static::initExtnPattern();
static::initRFC3966Patterns();
static::$PLUS_CHARS_PATTERN = '[' . static::PLUS_CHARS . ']+';
static::$SEPARATOR_PATTERN = '[' . static::VALID_PUNCTUATION . ']+';
static::$CAPTURING_DIGIT_PATTERN = '(' . static::DIGITS . ')';
Expand Down Expand Up @@ -474,7 +489,7 @@ public static function initExtnPatterns()
* @param int $maxLength
* @return string
*/
private static function extnDigits($maxLength)
protected static function extnDigits($maxLength)
{
return '(' . self::DIGITS . '{1,' . $maxLength . '})';
}
Expand Down Expand Up @@ -569,6 +584,17 @@ protected static function initExtnPattern()
static::$EXTN_PATTERN = '/(?:' . static::$EXTN_PATTERNS_FOR_PARSING . ')$/' . static::REGEX_FLAGS;
}

protected static function initRFC3966Patterns()
{
static::$RFC3966_PHONE_DIGIT = '(' . static::DIGITS . '|' . static::$RFC3966_VISUAL_SEPARATOR . ')';
static::$RFC3966_GLOBAL_NUMBER_DIGITS = "^\\" . static::PLUS_SIGN . static::$RFC3966_PHONE_DIGIT . "*" . static::DIGITS . static::$RFC3966_PHONE_DIGIT . "*$";

static::$ALPHANUM = static::VALID_ALPHA . static::DIGITS;
static::$RFC3966_DOMAINLABEL = '[' . static::$ALPHANUM . "]+((\\-)*[" . static::$ALPHANUM . "])*";
static::$RFC3966_TOPLABEL = '[' . static::VALID_ALPHA . "]+((\\-)*[" . static::$ALPHANUM . "])*";
static::$RFC3966_DOMAINNAME = "^(" . static::$RFC3966_DOMAINLABEL . "\\.)*" . static::$RFC3966_TOPLABEL . "\\.?$";
}

protected static function initValidPhoneNumberPatterns()
{
static::initExtnPatterns();
Expand Down Expand Up @@ -1776,6 +1802,60 @@ protected function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $c
$phoneNumber->setNationalNumber($normalizedNationalNumber);
}

/**
* Extracts the value of the phone-context parameter of numberToExtractFrom where the index of
* ";phone-context=" is the parameter indexOfPhoneContext, following the syntax defined in
* RFC3966.
*
* @param string $numberToExtractFrom
* @param int|false $indexOfPhoneContext
* @return string|null the extracted string (possibly empty), or null if no phone-context parameter is found.
*/
protected function extractPhoneContext($numberToExtractFrom, $indexOfPhoneContext)
{
// If no phone-context parameter is present
if ($indexOfPhoneContext === false) {
return null;
}

$phoneContextStart = $indexOfPhoneContext + strlen(static::RFC3966_PHONE_CONTEXT);
// If phone-context parameter is empty
if ($phoneContextStart >= mb_strlen($numberToExtractFrom)) {
return '';
}

$phoneContextEnd = strpos($numberToExtractFrom, ';', $phoneContextStart);
// If phone-context is not the last parameter
if ($phoneContextEnd !== false) {
return substr($numberToExtractFrom, $phoneContextStart, $phoneContextEnd - $phoneContextStart);
}

return substr($numberToExtractFrom, $phoneContextStart);
}

/**
* Returns whether the value of phoneContext follows the syntax defined in RFC3966.
*
* @param string|null $phoneContext
* @return bool
*/
protected function isPhoneContextValid($phoneContext)
{
if ($phoneContext === null) {
return true;
}

if ($phoneContext === '') {
return false;
}

$numberDigitsPattern = '/' . static::$RFC3966_GLOBAL_NUMBER_DIGITS . '/' . static::REGEX_FLAGS;
$domainNamePattern = '/' . static::$RFC3966_DOMAINNAME . '/' . static::REGEX_FLAGS;

// Does phone-context value match pattern of global-number-digits or domainname
return preg_match($numberDigitsPattern, $phoneContext) || preg_match($domainNamePattern, $phoneContext);
}

/**
* Returns a new phone number containing only the fields needed to uniquely identify a phone
* number, rather than any fields that capture the context in which the phone number was created.
Expand Down Expand Up @@ -1805,32 +1885,32 @@ protected static function copyCoreFieldsOnly(PhoneNumber $phoneNumberIn)
* written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber.
* @param string $numberToParse
* @param string $nationalNumber
* @throws NumberParseException
*/
protected function buildNationalNumberForParsing($numberToParse, &$nationalNumber)
{
$indexOfPhoneContext = strpos($numberToParse, static::RFC3966_PHONE_CONTEXT);
if ($indexOfPhoneContext !== false) {
$phoneContextStart = $indexOfPhoneContext + mb_strlen(static::RFC3966_PHONE_CONTEXT);
$phoneContext = $this->extractPhoneContext($numberToParse, $indexOfPhoneContext);

if (!$this->isPhoneContextValid($phoneContext)) {
throw new NumberParseException(NumberParseException::NOT_A_NUMBER, 'The phone-context valid is invalid.');
}

if ($phoneContext !== null) {
// If the phone context contains a phone number prefix, we need to capture it, whereas domains
// will be ignored.
if ($phoneContextStart < (strlen($numberToParse) - 1)
&& substr($numberToParse, $phoneContextStart, 1) == static::PLUS_SIGN) {

if (strpos($phoneContext, self::PLUS_SIGN) === 0) {
// Additional parameters might follow the phone context. If so, we will remove them here
// because the parameters after phone context are not important for parsing the
// phone number.
$phoneContextEnd = strpos($numberToParse, ';', $phoneContextStart);
if ($phoneContextEnd > 0) {
$nationalNumber .= substr($numberToParse, $phoneContextStart, $phoneContextEnd - $phoneContextStart);
} else {
$nationalNumber .= substr($numberToParse, $phoneContextStart);
}
// because the parameters after phone context are not important for parsing the phone
// number.
$nationalNumber .= $phoneContext;
}

// Now append everything between the "tel:" prefix and the phone-context. This should include
// the national number, an optional extension or isdn-subaddress component. Note we also
// handle the case when "tel:" is missing, as we have seen in some of the phone number inputs.
// In that case, we append everything from the beginning.

$indexOfRfc3966Prefix = strpos($numberToParse, static::RFC3966_PREFIX);
$indexOfNationalNumber = ($indexOfRfc3966Prefix !== false) ? $indexOfRfc3966Prefix + strlen(static::RFC3966_PREFIX) : 0;
$nationalNumber .= substr(
Expand Down
1 change: 1 addition & 0 deletions src/carrier/data/en/220.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
22051 => 'QCell',
22052 => 'QCell',
22053 => 'QCell',
22054 => 'QCell',
22058 => 'QCell',
22059 => 'QCell',
2206 => 'Comium',
Expand Down
1 change: 1 addition & 0 deletions src/carrier/data/en/60.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
601171 => 'Celecom',
6011710 => 'DiGi',
6011711 => 'DiGi',
6011720 => 'Celecom',
6011721 => 'Celecom',
6011722 => 'Maxis',
6011723 => 'Maxis',
Expand Down
4 changes: 2 additions & 2 deletions src/data/PhoneNumberMetadata_BL.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return array (
'generalDesc' =>
array (
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
array (
0 => 9,
Expand Down Expand Up @@ -88,7 +88,7 @@
),
'voip' =>
array (
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
'PossibleLength' =>
array (
Expand Down
8 changes: 4 additions & 4 deletions src/data/PhoneNumberMetadata_GF.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return array (
'generalDesc' =>
array (
'NationalNumberPattern' => '(?:[56]94|80\\d|976)\\d{6}',
'NationalNumberPattern' => '[56]94\\d{6}|(?:80|9\\d)\\d{7}',
'PossibleLength' =>
array (
0 => 9,
Expand Down Expand Up @@ -88,7 +88,7 @@
),
'voip' =>
array (
'NationalNumberPattern' => '976\\d{6}',
'NationalNumberPattern' => '9(?:396|76\\d)\\d{5}',
'ExampleNumber' => '976012345',
'PossibleLength' =>
array (
Expand Down Expand Up @@ -151,7 +151,7 @@
'format' => '$1 $2 $3 $4',
'leadingDigitsPatterns' =>
array (
0 => '[569]',
0 => '[56]|97',
),
'nationalPrefixFormattingRule' => '0$1',
'domesticCarrierCodeFormattingRule' => '',
Expand All @@ -163,7 +163,7 @@
'format' => '$1 $2 $3 $4',
'leadingDigitsPatterns' =>
array (
0 => '8',
0 => '[89]',
),
'nationalPrefixFormattingRule' => '0$1',
'domesticCarrierCodeFormattingRule' => '',
Expand Down
2 changes: 1 addition & 1 deletion src/data/PhoneNumberMetadata_GM.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
),
'mobile' =>
array (
'NationalNumberPattern' => '(?:[23679]\\d|5[0-389])\\d{5}',
'NationalNumberPattern' => '(?:[23679]\\d|5[0-489])\\d{5}',
'ExampleNumber' => '3012345',
'PossibleLength' =>
array (
Expand Down
4 changes: 2 additions & 2 deletions src/data/PhoneNumberMetadata_GP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return array (
'generalDesc' =>
array (
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
array (
0 => 9,
Expand Down Expand Up @@ -88,7 +88,7 @@
),
'voip' =>
array (
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
'PossibleLength' =>
array (
Expand Down
2 changes: 1 addition & 1 deletion src/data/PhoneNumberMetadata_KW.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
),
'mobile' =>
array (
'NationalNumberPattern' => '(?:41\\d\\d|5(?:(?:[05]\\d|1[0-7]|6[56])\\d|2(?:22|5[25])|7(?:55|77)|88[58])|6(?:(?:0[034679]|5[015-9]|6\\d)\\d|1(?:00|11|66)|222|3[36]3|444|7(?:0[013-9]|[67]\\d)|888|9(?:[069]\\d|3[039]))|9(?:(?:0[09]|22|[4679]\\d|8[057-9])\\d|1(?:1[01]|99)|3(?:00|33)|5(?:00|5\\d)))\\d{4}',
'NationalNumberPattern' => '(?:41\\d\\d|5(?:(?:[05]\\d|1[0-7]|6[56])\\d|2(?:22|5[25])|7(?:55|77)|88[58])|6(?:(?:0[034679]|5[015-9]|6\\d)\\d|1(?:00|11|66)|222|3[36]3|444|7(?:0[013-9]|[67]\\d)|888|9(?:[069]\\d|3[039]))|9(?:(?:0[09]|[4679]\\d|8[057-9])\\d|1(?:1[01]|99)|2(?:00|2\\d)|3(?:00|3[03])|5(?:00|5\\d)))\\d{4}',
'ExampleNumber' => '50012345',
'PossibleLength' =>
array (
Expand Down
4 changes: 2 additions & 2 deletions src/data/PhoneNumberMetadata_MF.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return array (
'generalDesc' =>
array (
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
array (
0 => 9,
Expand Down Expand Up @@ -88,7 +88,7 @@
),
'voip' =>
array (
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
'PossibleLength' =>
array (
Expand Down
2 changes: 1 addition & 1 deletion src/data/PhoneNumberMetadata_MY.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
),
'mobile' =>
array (
'NationalNumberPattern' => '1(?:1888[689]|4400|8(?:47|8[27])[0-4])\\d{4}|1(?:0(?:[23568]\\d|4[0-6]|7[016-9]|9[0-8])|1(?:[1-5]\\d\\d|6(?:0[5-9]|[1-9]\\d)|7(?:[0134]\\d|2[1-9]|5[0-6]))|(?:[269]\\d|[37][1-9]|4[235-9])\\d|5(?:31|9\\d\\d)|8(?:1[23]|[236]\\d|4[06]|5(?:46|[7-9])|7[016-9]|8[01]|9[0-8]))\\d{5}',
'NationalNumberPattern' => '1(?:1888[689]|4400|8(?:47|8[27])[0-4])\\d{4}|1(?:0(?:[23568]\\d|4[0-6]|7[016-9]|9[0-8])|1(?:[1-5]\\d\\d|6(?:0[5-9]|[1-9]\\d)|7(?:[0-4]\\d|5[0-6]))|(?:[269]\\d|[37][1-9]|4[235-9])\\d|5(?:31|9\\d\\d)|8(?:1[23]|[236]\\d|4[06]|5(?:46|[7-9])|7[016-9]|8[01]|9[0-8]))\\d{5}',
'ExampleNumber' => '123456789',
'PossibleLength' =>
array (
Expand Down
Loading

0 comments on commit 2d3f07b

Please sign in to comment.