composer-update-patch
This commit is contained in:
16
vendor/propaganistas/laravel-phone/src/LaravelPhoneFacade.php
vendored
Normal file
16
vendor/propaganistas/laravel-phone/src/LaravelPhoneFacade.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class LaravelPhoneFacade extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'libphonenumber';
|
||||
}
|
||||
}
|
@@ -6,27 +6,27 @@ use libphonenumber\PhoneNumberUtil;
|
||||
class LaravelPhoneServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
|
||||
}
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// Make libphonenumber available in the application container.
|
||||
$this->app->singleton('libphonenumber', function($app) {
|
||||
return PhoneNumberUtil::getInstance();
|
||||
});
|
||||
$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil');
|
||||
}
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('libphonenumber', function ($app) {
|
||||
return PhoneNumberUtil::getInstance();
|
||||
});
|
||||
|
||||
$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil');
|
||||
}
|
||||
}
|
||||
|
@@ -10,247 +10,246 @@ use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
|
||||
class PhoneValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
|
||||
/**
|
||||
* Dotted validator data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* Dotted validator data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Whether the country should be auto-detected.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autodetect = false;
|
||||
/**
|
||||
* Whether the country should be auto-detected.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autodetect = false;
|
||||
|
||||
/**
|
||||
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $lenient = false;
|
||||
/**
|
||||
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $lenient = false;
|
||||
|
||||
/**
|
||||
* The field to use for country if not passed as a parameter.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $countryField = null;
|
||||
/**
|
||||
* The field to use for country if not passed as a parameter.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $countryField = null;
|
||||
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
|
||||
/**
|
||||
* PhoneValidator constructor.
|
||||
*/
|
||||
public function __construct(PhoneNumberUtil $lib)
|
||||
{
|
||||
$this->lib = $lib;
|
||||
}
|
||||
/**
|
||||
* PhoneValidator constructor.
|
||||
*/
|
||||
public function __construct(PhoneNumberUtil $lib)
|
||||
{
|
||||
$this->lib = $lib;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a phone number.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @param object $validator
|
||||
* @return bool
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
public function validatePhone($attribute, $value, array $parameters, $validator)
|
||||
{
|
||||
$this->data = array_dot($validator->getData());
|
||||
/**
|
||||
* Validates a phone number.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @param object $validator
|
||||
* @return bool
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
public function validatePhone($attribute, $value, array $parameters, $validator)
|
||||
{
|
||||
$this->data = array_dot($validator->getData());
|
||||
|
||||
$this->assignParameters($parameters);
|
||||
$this->assignParameters($parameters);
|
||||
|
||||
$this->checkCountries($attribute);
|
||||
$this->checkCountries($attribute);
|
||||
|
||||
// If autodetecting, let's first try without a country.
|
||||
// Otherwise use provided countries as default.
|
||||
if ($this->autodetect || ($this->lenient && empty($this->countries))) {
|
||||
array_unshift($this->countries, null);
|
||||
}
|
||||
// If autodetecting, let's first try without a country.
|
||||
// Otherwise use provided countries as default.
|
||||
if ($this->autodetect || ($this->lenient && empty($this->countries))) {
|
||||
array_unshift($this->countries, null);
|
||||
}
|
||||
|
||||
foreach ($this->countries as $country) {
|
||||
if ($this->isValidNumber($value, $country)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
foreach ($this->countries as $country) {
|
||||
if ($this->isValidNumber($value, $country)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// All specified country validations have failed.
|
||||
return false;
|
||||
}
|
||||
// All specified country validations have failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the supplied validator parameters.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
*/
|
||||
protected function assignParameters(array $parameters)
|
||||
{
|
||||
$types = array();
|
||||
/**
|
||||
* Parses the supplied validator parameters.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
*/
|
||||
protected function assignParameters(array $parameters)
|
||||
{
|
||||
$types = [];
|
||||
|
||||
foreach ($parameters as $parameter) {
|
||||
// First check if the parameter is some phone type configuration.
|
||||
if ($this->isPhoneType($parameter)) {
|
||||
$types[] = strtoupper($parameter);
|
||||
} elseif ($this->isPhoneCountry($parameter)) {
|
||||
$this->countries[] = strtoupper($parameter);
|
||||
} elseif ($parameter == 'AUTO') {
|
||||
$this->autodetect = true;
|
||||
} elseif ($parameter == 'LENIENT') {
|
||||
$this->lenient = true;
|
||||
}
|
||||
// Lastly check if it is an input field containing the country.
|
||||
elseif ($this->isInputField($parameter)) {
|
||||
$this->countryField = $parameter;
|
||||
} else {
|
||||
// Force developers to write proper code.
|
||||
throw new InvalidParameterException($parameter);
|
||||
}
|
||||
}
|
||||
foreach ($parameters as $parameter) {
|
||||
// First check if the parameter is some phone type configuration.
|
||||
if ($this->isPhoneType($parameter)) {
|
||||
$types[] = strtoupper($parameter);
|
||||
} elseif ($this->isPhoneCountry($parameter)) {
|
||||
$this->countries[] = strtoupper($parameter);
|
||||
} elseif ($parameter == 'AUTO') {
|
||||
$this->autodetect = true;
|
||||
} elseif ($parameter == 'LENIENT') {
|
||||
$this->lenient = true;
|
||||
} // Lastly check if it is an input field containing the country.
|
||||
elseif ($this->isInputField($parameter)) {
|
||||
$this->countryField = $parameter;
|
||||
} else {
|
||||
// Force developers to write proper code.
|
||||
throw new InvalidParameterException($parameter);
|
||||
}
|
||||
}
|
||||
|
||||
$this->types = $this->parseTypes($types);
|
||||
$this->types = $this->parseTypes($types);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the detected countries. Overrides countries if a country field is present.
|
||||
* When using a country field, we should validate to false if country is empty so no exception
|
||||
* will be thrown.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function checkCountries($attribute)
|
||||
{
|
||||
$countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);
|
||||
/**
|
||||
* Checks the detected countries. Overrides countries if a country field is present.
|
||||
* When using a country field, we should validate to false if country is empty so no exception
|
||||
* will be thrown.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function checkCountries($attribute)
|
||||
{
|
||||
$countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);
|
||||
|
||||
if ($this->isInputField($countryField)) {
|
||||
$this->countries = array(array_get($this->data, $countryField));
|
||||
} elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
if ($this->isInputField($countryField)) {
|
||||
$this->countries = [array_get($this->data, $countryField)];
|
||||
} elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the actual validation of the phone number.
|
||||
*
|
||||
* @param mixed $number
|
||||
* @param null $country
|
||||
* @return bool
|
||||
*/
|
||||
protected function isValidNumber($number, $country = null)
|
||||
{
|
||||
try {
|
||||
// Throws NumberParseException if not parsed correctly against the supplied country.
|
||||
// If no country was given, tries to discover the country code from the number itself.
|
||||
$phoneNumber = $this->lib->parse($number, $country);
|
||||
/**
|
||||
* Performs the actual validation of the phone number.
|
||||
*
|
||||
* @param mixed $number
|
||||
* @param null $country
|
||||
* @return bool
|
||||
*/
|
||||
protected function isValidNumber($number, $country = null)
|
||||
{
|
||||
try {
|
||||
// Throws NumberParseException if not parsed correctly against the supplied country.
|
||||
// If no country was given, tries to discover the country code from the number itself.
|
||||
$phoneNumber = $this->lib->parse($number, $country);
|
||||
|
||||
// Check if type is allowed.
|
||||
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
|
||||
// Check if type is allowed.
|
||||
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
|
||||
|
||||
// Lenient validation.
|
||||
if ($this->lenient) {
|
||||
return $this->lib->isPossibleNumber($phoneNumber, $country);
|
||||
}
|
||||
// Lenient validation.
|
||||
if ($this->lenient) {
|
||||
return $this->lib->isPossibleNumber($phoneNumber, $country);
|
||||
}
|
||||
|
||||
// For automatic detection, the number should have a country code.
|
||||
if ($phoneNumber->hasCountryCode()) {
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
// Validate number against the specified country.
|
||||
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the supplied phone number types.
|
||||
*
|
||||
* @param array $types
|
||||
* @return array
|
||||
*/
|
||||
protected function parseTypes(array $types)
|
||||
{
|
||||
// Transform types to their namespaced class constant.
|
||||
array_walk($types, function(&$type) {
|
||||
$type = constant('\libphonenumber\PhoneNumberType::' . $type);
|
||||
});
|
||||
/**
|
||||
* Parses the supplied phone number types.
|
||||
*
|
||||
* @param array $types
|
||||
* @return array
|
||||
*/
|
||||
protected function parseTypes(array $types)
|
||||
{
|
||||
// Transform types to their namespaced class constant.
|
||||
array_walk($types, function (&$type) {
|
||||
$type = constant('\libphonenumber\PhoneNumberType::' . $type);
|
||||
});
|
||||
|
||||
// Add in the unsure number type if applicable.
|
||||
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
|
||||
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
|
||||
}
|
||||
// Add in the unsure number type if applicable.
|
||||
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
|
||||
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
|
||||
}
|
||||
|
||||
return $types;
|
||||
}
|
||||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given field is an actual input field.
|
||||
*
|
||||
* @param string $field
|
||||
* @return bool
|
||||
*/
|
||||
public function isInputField($field)
|
||||
{
|
||||
return !is_null(array_get($this->data, $field));
|
||||
}
|
||||
/**
|
||||
* Checks if the given field is an actual input field.
|
||||
*
|
||||
* @param string $field
|
||||
* @return bool
|
||||
*/
|
||||
public function isInputField($field)
|
||||
{
|
||||
return ! is_null(array_get($this->data, $field));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the supplied string is a valid country code.
|
||||
*
|
||||
* @param string $country
|
||||
* @return bool
|
||||
*/
|
||||
public function isPhoneCountry($country)
|
||||
{
|
||||
return ISO3166::isValid(strtoupper($country));
|
||||
}
|
||||
/**
|
||||
* Checks if the supplied string is a valid country code.
|
||||
*
|
||||
* @param string $country
|
||||
* @return bool
|
||||
*/
|
||||
public function isPhoneCountry($country)
|
||||
{
|
||||
return ISO3166::isValid(strtoupper($country));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the supplied string is a valid phone number type.
|
||||
*
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function isPhoneType($type)
|
||||
{
|
||||
// Legacy support.
|
||||
$type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
|
||||
/**
|
||||
* Checks if the supplied string is a valid phone number type.
|
||||
*
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function isPhoneType($type)
|
||||
{
|
||||
// Legacy support.
|
||||
$type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
|
||||
|
||||
return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type));
|
||||
}
|
||||
return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,25 +3,44 @@
|
||||
use Illuminate\Support\Facades\App;
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
|
||||
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 = null, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
{
|
||||
$lib = App::make('libphonenumber');
|
||||
if (! function_exists('phone_format')) {
|
||||
/**
|
||||
* Get the PhoneNumberUtil or format a phone number for display.
|
||||
*
|
||||
* @return \libphonenumber\PhoneNumberUtil|string
|
||||
*/
|
||||
function phone()
|
||||
{
|
||||
$lib = App::make('libphonenumber');
|
||||
|
||||
if (!$country) {
|
||||
$country = App::getLocale();
|
||||
}
|
||||
if (! $arguments = func_get_args()) {
|
||||
return $lib;
|
||||
}
|
||||
|
||||
$phoneNumber = $lib->parse($phone, $country);
|
||||
$phone = $arguments[0];
|
||||
$country = isset($arguments[1]) ? $arguments[1] : App::getLocale();
|
||||
$format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL;
|
||||
|
||||
return $lib->format($phoneNumber, $format);
|
||||
}
|
||||
return $lib->format(
|
||||
$lib->parse($phone, $country),
|
||||
$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
|
||||
*
|
||||
* @deprecated 2.8.0
|
||||
*/
|
||||
function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
{
|
||||
return phone($phone, $country, $format);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user