update v1.0.6

This commit is contained in:
sujitprasad
2016-02-16 22:42:08 +05:30
parent e6b579d67b
commit 073a49a8af
587 changed files with 21487 additions and 22766 deletions

View File

@@ -91,11 +91,17 @@ To specify constraints on the number type, just append the allowed types to the
```
The most common types are `mobile` and `fixed_line`, but feel free to use any of the types defined [here](https://github.com/giggsey/libphonenumber-for-php/blob/master/src/libphonenumber/PhoneNumberType.php).
You can also enable more lenient validation (for example, fixed lines without area codes) by using the `LENIENT` parameter. This feature inherently doesn't play well with country autodetection and number type validation, so use such combo at own risk.
```php
'phonefield' => 'phone:LENIENT,US',
```
### Display
Format a fetched phone value using the helper function:
```php
phone_format($phone_number, $country_code, $format = null)
phone_format($phone_number, $country_code, $format = PhoneNumberFormat::INTERNATIONAL)
```
The `$format` parameter is optional and should be a constant of `\libphonenumber\PhoneNumberFormat` (defaults to `\libphonenumber\PhoneNumberFormat::INTERNATIONAL`)
The `$format` parameter is optional and should be a constant of `libphonenumber\PhoneNumberFormat` (defaults to `libphonenumber\PhoneNumberFormat::INTERNATIONAL`)

View File

@@ -21,6 +21,13 @@ class PhoneValidator
*/
protected $autodetect = false;
/**
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
*
* @var bool
*/
protected $lenient = false;
/**
* Countries to validate against.
*
@@ -62,7 +69,7 @@ class PhoneValidator
// If autodetecting, let's first try without a country.
// Otherwise use provided countries as default.
if ($this->autodetect) {
if ($this->autodetect || ($this->lenient && empty($this->countries))) {
array_unshift($this->countries, null);
}
@@ -93,6 +100,8 @@ class PhoneValidator
$types[] = $parameter;
} elseif ($parameter == 'AUTO') {
$this->autodetect = true;
} elseif ($parameter == 'LENIENT') {
$this->lenient = true;
} else {
// Force developers to write proper code.
throw new InvalidParameterException($parameter);
@@ -108,7 +117,7 @@ class PhoneValidator
* When using a country field, we should validate to false if country is empty so no exception
* will be thrown.
*
* @param $attribute
* @param string $attribute
* @param $validator
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
*/
@@ -119,7 +128,7 @@ class PhoneValidator
if (isset($data[$countryField])) {
$this->countries = array($data[$countryField]);
} elseif (!$this->autodetect && empty($this->countries)) {
} elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
throw new NoValidCountryFoundException;
}
}
@@ -138,18 +147,26 @@ class PhoneValidator
// If no country was given, tries to discover the country code from the number itself.
$phoneNumber = $this->lib->parse($number, $country);
// For automatic detection, the number should have a country code.
// Check if type is allowed.
if ($phoneNumber->hasCountryCode() && (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types))) {
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
// Automatic detection:
if ($this->autodetect) {
// Validate if the international phone number is valid for its contained country.
return $this->lib->isValidNumber($phoneNumber);
// Lenient validation.
if ($this->lenient) {
return $this->lib->isPossibleNumber($phoneNumber, $country);
}
// Validate number against the specified country.
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
// For automatic detection, the number should have a country code.
if ($phoneNumber->hasCountryCode()) {
// Automatic detection:
if ($this->autodetect) {
// Validate if the international phone number is valid for its contained country.
return $this->lib->isValidNumber($phoneNumber);
}
// Validate number against the specified country.
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
}
}
} catch (NumberParseException $e) {
@@ -168,7 +185,7 @@ class PhoneValidator
protected function parseTypes(array $types)
{
// Transform types to their namespaced class constant.
array_walk($types, function (&$type) {
array_walk($types, function(&$type) {
$type = constant('\libphonenumber\PhoneNumberType::' . $type);
});

View File

@@ -3,9 +3,20 @@
use Illuminate\Support\Facades\App;
use libphonenumber\PhoneNumberFormat;
function phone_format($phone, $country, $format = null) {
$lib = App::make('libphonenumber');
$phoneNumber = $lib->parse($phone, $country);
$format = is_null($format) ? PhoneNumberFormat::INTERNATIONAL : $format;
return $lib->format($phoneNumber, $format);
if (!function_exists('phone_format')) {
/**
* Formats a phone number and country for display.
*
* @param string $phone
* @param string $country
* @param int|null $format
* @return string
*/
function phone_format($phone, $country, $format = PhoneNumberFormat::INTERNATIONAL)
{
$lib = App::make('libphonenumber');
$phoneNumber = $lib->parse($phone, $country);
return $lib->format($phoneNumber, $format);
}
}

View File

@@ -18,7 +18,9 @@ class PhoneValidatorTest extends TestCase
private function performValidation($data)
{
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
$validator = $this->validator->make(array_only($data, ['field', 'field_country']), ['field' => $rule]);
$validator = $this->validator->make(
array_only($data, ['field', 'field_country']), ['field' => $rule]
);
return $validator->passes();
}
@@ -130,6 +132,39 @@ class PhoneValidatorTest extends TestCase
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile,xyz']);
}
public function testValidatePhoneLenient()
{
// Validator with AU area code, lenient off
$this->assertFalse($this->performValidation(['field' => '88885555', 'rule' => 'AU']));
// Validator with AU area code, lenient on
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT,AU']));
// Validator with correct country field supplied, lenient on
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'AU']));
// Validator with wrong country field supplied, lenient on
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'BE']));
// Validator with no area code, lenient on
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT']));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT,US']));
// Validator with no area code, lenient off
$this->assertFalse($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT']));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT,US']));
// Validator with US area code, lenient off
$this->assertFalse($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT']));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT,US']));
}
public function testValidatePhoneFaultyParameters()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');

View File

@@ -1,168 +0,0 @@
<?php
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Illuminate\Validation\Validator;
use Propaganistas\LaravelPhone\PhoneValidator;
class PhoneValidatorTest extends PHPUnit_Framework_TestCase {
private $translator;
private $validator;
public function setUp()
{
$this->translator = new Translator('en', new MessageSelector());
$this->translator->addLoader('array', new ArrayLoader);
$this->validator = new Validator($this->translator, array(), array());
}
private function createPhoneValidator()
{
return new PhoneValidator();
}
private function performValidation($data)
{
$validatorData = array('field' => $data['value']);
$validatorData['field_country'] = isset($data['country']) ? $data['country'] : null;
$this->validator->setData($validatorData);
$parameters = isset($data['params']) ? explode(',',$data['params']) : array();
return $this->createPhoneValidator()
->validatePhone('field', $data['value'], $parameters, $this->validator);
}
public function testValidatePhoneWithDefaultCountryNoType()
{
// Validator with correct country value.
$this->assertTrue($this->performValidation(['value' => '016123456', 'params' => 'BE']));
// Validator with wrong country value.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'NL']));
// Validator with multiple country values, one correct.
$this->assertTrue($this->performValidation(['value' => '016123456', 'params' => 'BE,NL']));
// Validator with multiple wrong country values.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'DE,NL']));
}
public function testValidatePhoneWithCountryFieldNoType()
{
// Validator with correct country field supplied.
$this->assertTrue($this->performValidation(['value' => '016123456', 'country' => 'BE']));
// Validator with wrong country field supplied.
$this->assertFalse($this->performValidation(['value' => '016123456', 'country' => 'NL']));
}
public function testValidatePhoneWithDefaultCountryWithType()
{
// Validator with correct country value, correct type.
$this->assertTrue($this->performValidation(['value' => '0499123456', 'params' => 'BE,mobile']));
// Validator with correct country value, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'BE,mobile']));
// Validator with wrong country value, correct type.
$this->assertFalse($this->performValidation(['value' => '0499123456', 'params' => 'NL,mobile']));
// Validator with wrong country value, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'NL,mobile']));
// Validator with multiple country values, one correct, correct type.
$this->assertTrue($this->performValidation(['value' => '0499123456', 'params' => 'BE,NL,mobile']));
// Validator with multiple country values, one correct, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'BE,NL,mobile']));
// Validator with multiple country values, none correct, correct type.
$this->assertFalse($this->performValidation(['value' => '0499123456', 'params' => 'DE,NL,mobile']));
// Validator with multiple country values, none correct, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'DE,NL,mobile']));
}
public function testValidatePhoneWithCountryFieldWithType()
{
// Validator with correct country field supplied, correct type.
$this->assertTrue($this->performValidation(['value' => '0499123456', 'params' => 'mobile', 'country' => 'BE']));
// Validator with correct country field supplied, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'mobile', 'country' => 'BE']));
// Validator with wrong country field supplied, correct type.
$this->assertFalse($this->performValidation(['value' => '0499123456', 'params' => 'mobile', 'country' => 'NL']));
// Validator with wrong country field supplied, wrong type.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'mobile', 'country' => 'NL']));
}
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
{
// Validator with correct international input.
$this->assertTrue($this->performValidation(['value' => '+3216123456', 'params' => 'AUTO']));
// Validator with wrong international input.
$this->assertFalse($this->performValidation(['value' => '003216123456', 'params' => 'AUTO']));
// Validator with wrong international input.
$this->assertFalse($this->performValidation(['value' => '+321456', 'params' => 'AUTO']));
}
public function testValidatePhoneNoDefaultCountryNoCountryField()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
// Validator with no country field or given country.
$this->performValidation(['value' => '016123456']);
// Validator with no country field or given country, wrong type.
$this->performValidation(['value' => '016123456', 'params' => 'mobile']);
// Validator with no country field or given country, correct type.
$this->performValidation(['value' => '0499123456', 'params' => 'mobile']);
// Validator with no country field or given country, correct type, faulty parameter.
$this->performValidation(['value' => '0499123456', 'params' => 'mobile,xyz']);
}
public function testValidatePhoneFaultyParameters()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
// Validator with given country, correct type, faulty parameter.
$this->performValidation(['value' => '016123456', 'params' => 'BE,mobile,xyz']);
// Validator with country field, correct type, faulty parameter.
$this->performValidation(['value' => '016123456', 'params' => 'mobile,xyz', 'country' => 'BE']);
}
public function testPhoneFormatHelperFunction()
{
// Test landline number without format parameter.
$actual = phone_format('016123456', 'BE');
$expected = '+32 16 12 34 56';
$this->assertEquals($expected, $actual);
// Test landline number with format parameter.
$actual = phone_format('016123456', 'BE', \libphonenumber\PhoneNumberFormat::NATIONAL);
$expected = '016 12 34 56';
$this->assertEquals($expected, $actual);
// Test mobile number without format parameter.
$actual = phone_format('0499123456', 'BE');
$expected = '+32 499 12 34 56';
$this->assertEquals($expected, $actual);
// Test mobile number with format parameter.
$actual = phone_format('0499123456', 'BE', \libphonenumber\PhoneNumberFormat::NATIONAL);
$expected = '0499 12 34 56';
$this->assertEquals($expected, $actual);
}
}