Skip to content

Commit

Permalink
Fixes #213
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas committed Jan 27, 2023
1 parent 8debcc5 commit fd6dcd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ protected function filterValidCountry($countries)
}
}

if ($countries = array_filter($countries)) {
$countries = array_filter($countries);

if (! empty($countries)) {
throw NumberParseException::countryMismatch($this->number, $countries);
}

Expand Down Expand Up @@ -375,7 +377,15 @@ public function getPhoneNumberInstance()
*/
public function numberLooksInternational()
{
return Str::startsWith($this->number, '+');
if (empty($this->number)) {
return false;
}

if (Str::startsWith($this->number, '+')) {
return true;
}

return strpos($this->number, '+', 2) && static::isValidCountryCode(Str::substr($this->number, 0, 2));
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ public function it_can_format_international_numbers_without_given_country()
$this->assertEquals('012 34 56 78', $object->format(PhoneNumberFormat::NATIONAL));
}

/** @test */
public function it_can_format_international_numbers_prefixed_with_correct_country()
{
$object = new PhoneNumber('BE+3212345678');
$this->assertEquals('012 34 56 78', $object->format(PhoneNumberFormat::NATIONAL));
}

/** @test */
public function it_can_format_international_numbers_prefixed_with_wrong_country()
{
$object = new PhoneNumber('US+3212345678');
$this->assertEquals('012 34 56 78', $object->format(PhoneNumberFormat::NATIONAL));
}

/** @test */
public function it_can_format_lenient_international_numbers_without_given_country()
{
Expand Down

0 comments on commit fd6dcd3

Please sign in to comment.