Skip to content

Commit

Permalink
Merge pull request #61 from giggsey/smaller-metadata
Browse files Browse the repository at this point in the history
🐛 Handle missing country codes
  • Loading branch information
giggsey authored Apr 29, 2024
2 parents 49f59f6 + 5fe6357 commit c551444
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PhoneNumber implements \Serializable, \Stringable
* The country calling code for this number, as defined by the International Telecommunication Union
* (ITU). For example, this would be 1 for NANPA countries, and 33 for France.
*/
protected ?int $countryCode = null;
protected int $countryCode = 0;
/**
* National (significant) Number is defined in International Telecommunication Union (ITU)
* Recommendation E.164. It is a language/country-neutral representation of a phone number at a
Expand Down Expand Up @@ -76,6 +76,7 @@ class PhoneNumber implements \Serializable, \Stringable
* The number of leading zeros of this phone number.
*/
protected int $numberOfLeadingZeros = 1;
private bool $hasCountryCode = false;

/**
* Clears this phone number.
Expand Down Expand Up @@ -104,7 +105,8 @@ public function clear(): PhoneNumber
*/
public function clearCountryCode(): PhoneNumber
{
$this->countryCode = null;
$this->countryCode = 0;
$this->hasCountryCode = false;
return $this;
}

Expand Down Expand Up @@ -229,15 +231,10 @@ public function mergeFrom(PhoneNumber $other): PhoneNumber
*/
public function hasCountryCode(): bool
{
return $this->countryCode !== null;
return $this->hasCountryCode;
}

/**
* Returns the country code of this phone number.
*
* @return int|null The country code, or null if not set.
*/
public function getCountryCode(): ?int
public function getCountryCode(): int
{
return $this->countryCode;
}
Expand All @@ -251,6 +248,7 @@ public function getCountryCode(): ?int
*/
public function setCountryCode(int $value): PhoneNumber
{
$this->hasCountryCode = true;
$this->countryCode = $value;
return $this;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Issues/UKNumbersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace libphonenumber\Tests\Issues;

use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
Expand Down Expand Up @@ -159,4 +160,13 @@ public function testInvalidNumber(): void
$valid = $this->phoneUtil->isValidNumber($phoneObject);
$this->assertFalse($valid, 'Checking phone number is invalid');
}

public function testFormattingNumberWithNoCountryCode(): void
{
$number = new PhoneNumber();
$number->setNationalNumber('999');

$formatted = $this->phoneUtil->format($number, PhoneNumberFormat::E164);
$this->assertEquals('+0999', $formatted);
}
}

0 comments on commit c551444

Please sign in to comment.