From 45f1f3d0898ed8afbc314b11b12ee6fddd2c885e Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Wed, 24 Apr 2024 09:33:48 +0200 Subject: [PATCH] bugfix (fixes #250) --- src/PhoneNumber.php | 4 ++-- tests/ValidatorTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index 0f33805..ea2a336 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -62,7 +62,7 @@ public function getCountry(): ?string continue; } - if (PhoneNumberUtil::getInstance()->isValidNumberForRegion($libPhoneObject, $country)) { + if (PhoneNumberUtil::getInstance()->isValidNumberForRegion($libPhoneObject, $country ?? 'ZZ')) { return PhoneNumberUtil::getInstance()->getRegionCodeForNumber($libPhoneObject); } } @@ -169,7 +169,7 @@ public function isValid(): bool return PhoneNumberUtil::getInstance()->isValidNumberForRegion( $this->toLibPhoneObject(), - $this->getCountry(), + $this->getCountry() ?? 'ZZ', ); } catch (NumberParseException $e) { return false; diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index aa39e8f..0e6ac82 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -159,6 +159,11 @@ public function it_validates_with_custom_country_field_taking_precedence_over_im #[Test] public function it_validates_in_international_mode() { + $this->assertTrue($this->validate( + ['field' => '+3212345678'], + ['field' => (new Phone)->international()] + )->passes()); + $this->assertFalse($this->validate( ['field' => '+3212345678'], ['field' => (new Phone)->country('NL')] @@ -180,6 +185,15 @@ public function it_validates_in_international_mode() )->passes()); } + #[Test] + public function it_validates_non_parseable_international_looking_numbers() + { + $this->assertFalse($this->validate( + ['field' => '+12345678901234'], + ['field' => (new Phone)->international()] + )->passes()); + } + #[Test] public function it_validates_in_lenient_mode() {