From fd6dcd3b6368045af31bd1cc4d311d8f812b6522 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Fri, 27 Jan 2023 08:39:03 +0100 Subject: [PATCH] Fixes #213 --- src/PhoneNumber.php | 14 ++++++++++++-- tests/PhoneNumberTest.php | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index 615c2c2..4ff7772 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -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); } @@ -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)); } /** diff --git a/tests/PhoneNumberTest.php b/tests/PhoneNumberTest.php index 30892f8..ad5b149 100644 --- a/tests/PhoneNumberTest.php +++ b/tests/PhoneNumberTest.php @@ -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() {