From 2773926de05eebf7802a216549c033410d8d5055 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 14 Apr 2016 13:35:11 +0300 Subject: [PATCH 1/2] Added test to check if possible to set country throught validation context --- test/Validator/PhoneNumberTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Validator/PhoneNumberTest.php b/test/Validator/PhoneNumberTest.php index f75c4753..440f7f48 100644 --- a/test/Validator/PhoneNumberTest.php +++ b/test/Validator/PhoneNumberTest.php @@ -3202,4 +3202,14 @@ public function testInvalidTypes($country, $code, $patterns) } } } + + public function testCanSpecifyCountryWithContext() + { + Locale::setDefault('ZW'); + $validator = new PhoneNumber([ + 'country' => 'country-code', + ]); + + $this->assertTrue($validator->isValid('+37067811268', ['country-code' => 'LT'])); + } } From 37b8325f18287d80da73ef49e770096767cd1526 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 14 Apr 2016 13:41:28 +0300 Subject: [PATCH 2/2] allow to specify country in context --- src/Validator/PhoneNumber.php | 6 +++--- test/Validator/PhoneNumberTest.php | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Validator/PhoneNumber.php b/src/Validator/PhoneNumber.php index 26d2a789..f37f1e3f 100644 --- a/src/Validator/PhoneNumber.php +++ b/src/Validator/PhoneNumber.php @@ -154,7 +154,7 @@ public function getCountry() */ public function setCountry($country) { - $this->country = strtoupper($country); + $this->country = $country; return $this; } @@ -201,12 +201,12 @@ public function isValid($value = null, $context = null) $country = $this->getCountry(); - if (!$countryPattern = $this->loadPattern($country)) { + if (!$countryPattern = $this->loadPattern(strtoupper($country))) { if (isset($context[$country])) { $country = $context[$country]; } - if (!$countryPattern = $this->loadPattern($country)) { + if (!$countryPattern = $this->loadPattern(strtoupper($country))) { $this->error(self::UNSUPPORTED); return false; diff --git a/test/Validator/PhoneNumberTest.php b/test/Validator/PhoneNumberTest.php index 440f7f48..ff19b565 100644 --- a/test/Validator/PhoneNumberTest.php +++ b/test/Validator/PhoneNumberTest.php @@ -3164,10 +3164,12 @@ public function testAllowPossibleSetterGetter() $this->assertTrue($this->validator->allowPossible()); } - public function testSetCountryMethodIsCaseInsensitive() + public function testCountryIsCaseInsensitive() { - $this->validator->setCountry('us'); - $this->assertSame('US', $this->validator->getCountry()); + $this->validator->setCountry('lt'); + $this->assertTrue($this->validator->isValid('+37067811268')); + $this->validator->setCountry('LT'); + $this->assertTrue($this->validator->isValid('+37067811268')); } /**