update v1.0.6
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user