Skip to content

Commit

Permalink
Handle phone number input in RFC3966 with missing "tel:" prefix.
Browse files Browse the repository at this point in the history
Upstream change from Google r664
  • Loading branch information
giggsey committed May 21, 2014
1 parent a7dd971 commit 08dc5b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Tests/libphonenumber/Tests/core/PhoneNumberUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,11 @@ public function testParseNationalNumber()
);
$this->assertEquals(
self::$nzNumber,
$this->phoneUtil->parse("tel:331-6005;phone-context=+64-3", RegionCode::NZ)
$this->phoneUtil->parse("tel:331-6005;phone-context=+64-3", RegionCode::US)
);
$this->assertEquals(
self::$nzNumber,
$this->phoneUtil->parse("My number is tel:03-331-6005;phone-context=+64", RegionCode::NZ)
);
// Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear
// after the context if present.
Expand All @@ -2280,6 +2284,9 @@ public function testParseNationalNumber()
);
$this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("tel:+64-3-331-6005;isub=12345", RegionCode::NZ));

// Test parsing RFC3966 with "tel:" missing
$this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("03-331-6005;phone-context=+64", RegionCode::NZ));

// Testing international prefixes.
// Should strip country calling code.
$this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("0064 3 331 6005", RegionCode::NZ));
Expand Down
14 changes: 7 additions & 7 deletions src/libphonenumber/PhoneNumberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -1544,13 +1544,13 @@ private function buildNationalNumberForParsing($numberToParse, &$nationalNumber)
}

// Now append everything between the "tel:" prefix and the phone-context. This should include
// the national number, an optional extension or isdn-subaddress component.
$prefixLoc = strpos($numberToParse, self::RFC3966_PREFIX);
if ($prefixLoc !== false)
$prefixLoc += mb_strlen(self::RFC3966_PREFIX);
else
$prefixLoc = 0;
$nationalNumber .= substr($numberToParse, $prefixLoc, ($indexOfPhoneContext - $prefixLoc));
// 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, self::RFC3966_PREFIX);
$indexOfNationalNumber = ($indexOfRfc3966Prefix !== false) ? $indexOfRfc3966Prefix + strlen(self::RFC3966_PREFIX) : 0;
$nationalNumber .= substr($numberToParse, $indexOfNationalNumber, ($indexOfPhoneContext - $indexOfNationalNumber));
} else {
// Extract a possible number from the string passed in (this strips leading characters that
// could not be the start of a phone number.)
Expand Down

0 comments on commit 08dc5b2

Please sign in to comment.