Skip to content

Commit

Permalink
Merge pull request #19 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 authored Mar 27, 2023
2 parents 7e14548 + 86f41be commit 6170739
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 52 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
30 changes: 21 additions & 9 deletions src/NumberParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@
*/
class NumberParseException extends \Exception implements \Stringable
{
/**
* The country code supplied did not belong to a supported country or non-geographical entity.
*/
public 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
*/
public 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.
*/
public 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.
*/
public 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.
*/
public const TOO_LONG = 4;

protected int $errorType;
Expand Down
96 changes: 84 additions & 12 deletions src/PhoneNumberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ class PhoneNumberUtil
*/
protected static string $EXTN_PATTERNS_FOR_PARSING;
protected static string $EXTN_PATTERNS_FOR_MATCHING;
// Regular expression of valid global-number-digits for the phone-context parameter, following the
// syntax defined in RFC3966.
protected static string $RFC3966_VISUAL_SEPARATOR = '[\\-\\.\\(\\)]?';
protected static string $RFC3966_PHONE_DIGIT;
protected static string $RFC3966_GLOBAL_NUMBER_DIGITS;

// Regular expression of valid domainname for the phone-context parameter, following the syntax
// defined in RFC3966.
protected static string $ALPHANUM;
protected static string $RFC3966_DOMAINLABEL;
protected static string $RFC3966_TOPLABEL;
protected static string $RFC3966_DOMAINNAME;
protected static string $EXTN_PATTERN;
protected static string $VALID_PHONE_NUMBER_PATTERN;
protected static string $MIN_LENGTH_PHONE_NUMBER_PATTERN;
Expand Down Expand Up @@ -336,6 +348,7 @@ protected function __construct(MetadataSourceInterface $metadataSource, array $c
$this->matcherAPI = RegexBasedMatcher::create();
static::initExtnPatterns();
static::initExtnPattern();
static::initRFC3966Patterns();

static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS = [];
// Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
Expand Down Expand Up @@ -436,6 +449,17 @@ protected static function initExtnPatterns(): void
static::$EXTN_PATTERNS_FOR_MATCHING = static::createExtnPattern(false);
}

protected static function initRFC3966Patterns(): void
{
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 . '\\.?$';
}

/**
* Helper method for constructing regular expressions for parsing. Creates an expression that
* captures up to maxLength digits.
Expand Down Expand Up @@ -1627,6 +1651,54 @@ protected function parseHelper(string $numberToParse, ?string $defaultRegion, bo
$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.
* @return string|null the extracted string (possibly empty), or null if no phone-context parameter is found.
*/
protected function extractPhoneContext(string $numberToExtractFrom, int|false $indexOfPhoneContext): ?string
{
// 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.
*/
protected function isPhoneContextValid(?string $phoneContext): bool
{
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 @@ -1655,25 +1727,25 @@ protected static function copyCoreFieldsOnly(PhoneNumber $phoneNumberIn): PhoneN
* 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(string $numberToParse, string &$nationalNumber): void
{
$indexOfPhoneContext = strpos($numberToParse, static::RFC3966_PHONE_CONTEXT);
if ($indexOfPhoneContext !== false) {
$phoneContextStart = $indexOfPhoneContext + 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 (str_starts_with($phoneContext, self::PLUS_SIGN)) {
// 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
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 @@ -9,7 +9,7 @@
return [
'generalDesc' =>
[
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
[
0 => 9,
Expand Down Expand Up @@ -53,7 +53,7 @@
],
'voip' =>
[
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
],
'pager' =>
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 @@ -9,7 +9,7 @@
return [
'generalDesc' =>
[
'NationalNumberPattern' => '(?:[56]94|80\\d|976)\\d{6}',
'NationalNumberPattern' => '[56]94\\d{6}|(?:80|9\\d)\\d{7}',
'PossibleLength' =>
[
0 => 9,
Expand Down Expand Up @@ -53,7 +53,7 @@
],
'voip' =>
[
'NationalNumberPattern' => '976\\d{6}',
'NationalNumberPattern' => '9(?:396|76\\d)\\d{5}',
'ExampleNumber' => '976012345',
],
'pager' =>
Expand Down Expand Up @@ -98,7 +98,7 @@
'format' => '$1 $2 $3 $4',
'leadingDigitsPatterns' =>
[
0 => '[569]',
0 => '[56]|97',
],
'nationalPrefixFormattingRule' => '0$1',
'domesticCarrierCodeFormattingRule' => '',
Expand All @@ -110,7 +110,7 @@
'format' => '$1 $2 $3 $4',
'leadingDigitsPatterns' =>
[
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 @@ -22,7 +22,7 @@
],
'mobile' =>
[
'NationalNumberPattern' => '(?:[23679]\\d|5[0-389])\\d{5}',
'NationalNumberPattern' => '(?:[23679]\\d|5[0-489])\\d{5}',
'ExampleNumber' => '3012345',
],
'tollFree' =>
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 @@ -9,7 +9,7 @@
return [
'generalDesc' =>
[
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
[
0 => 9,
Expand Down Expand Up @@ -53,7 +53,7 @@
],
'voip' =>
[
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
],
'pager' =>
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 @@ -27,7 +27,7 @@
],
'mobile' =>
[
'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' =>
[
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 @@ -9,7 +9,7 @@
return [
'generalDesc' =>
[
'NationalNumberPattern' => '(?:590|(?:69|80)\\d|976)\\d{6}',
'NationalNumberPattern' => '590\\d{6}|(?:69|80|9\\d)\\d{7}',
'PossibleLength' =>
[
0 => 9,
Expand Down Expand Up @@ -53,7 +53,7 @@
],
'voip' =>
[
'NationalNumberPattern' => '976[01]\\d{5}',
'NationalNumberPattern' => '9(?:395|76[018])\\d{5}',
'ExampleNumber' => '976012345',
],
'pager' =>
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 @@ -39,7 +39,7 @@
],
'mobile' =>
[
'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' =>
[
Expand Down
20 changes: 14 additions & 6 deletions src/data/PhoneNumberMetadata_PM.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
return [
'generalDesc' =>
[
'NationalNumberPattern' => '(?:[45]|80\\d\\d)\\d{5}',
'NationalNumberPattern' => '[45]\\d{5}|(?:708|80\\d)\\d{6}',
'PossibleLength' =>
[
0 => 6,
Expand All @@ -27,12 +27,8 @@
],
'mobile' =>
[
'NationalNumberPattern' => '(?:4[02-4]|5[056])\\d{4}',
'NationalNumberPattern' => '(?:4[02-4]|5[056]|708[45][0-5])\\d{4}',
'ExampleNumber' => '551234',
'PossibleLength' =>
[
0 => 6,
],
],
'tollFree' =>
[
Expand Down Expand Up @@ -120,6 +116,18 @@
'nationalPrefixOptionalWhenFormatting' => false,
],
1 =>
[
'pattern' => '(\\d{3})(\\d{3})(\\d{3})',
'format' => '$1 $2 $3',
'leadingDigitsPatterns' =>
[
0 => '7',
],
'nationalPrefixFormattingRule' => '',
'domesticCarrierCodeFormattingRule' => '',
'nationalPrefixOptionalWhenFormatting' => false,
],
2 =>
[
'pattern' => '(\\d{3})(\\d{2})(\\d{2})(\\d{2})',
'format' => '$1 $2 $3 $4',
Expand Down
2 changes: 1 addition & 1 deletion src/data/PhoneNumberMetadata_PT.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
],
'uan' =>
[
'NationalNumberPattern' => '70(?:7\\d|8[17])\\d{5}',
'NationalNumberPattern' => '70(?:38[01]|596|(?:7\\d|8[17])\\d)\\d{4}',
'ExampleNumber' => '707123456',
],
'voicemail' =>
Expand Down
Loading

0 comments on commit 6170739

Please sign in to comment.