update v 1.0.7.5
This commit is contained in:
15
vendor/propaganistas/laravel-phone/README.md
vendored
15
vendor/propaganistas/laravel-phone/README.md
vendored
@@ -63,11 +63,16 @@ To validate a field using the phone validator, use the `phone` keyword in your v
|
||||
|
||||
The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.
|
||||
|
||||
- You don't specify any parameters but you plug in a dedicated country input field (keyed by *ISO 3166-1 compliant* country codes) to allow end users to supply a country on their own. The easiest method by far is to install the [CountryList package by monarobase](https://github.com/Monarobase/country-list). The country field has to be named similar to the phone field but with `_country` appended:
|
||||
- You don't specify any parameters but you plug in a dedicated country input field (keyed by *ISO 3166-1 compliant* country codes) to allow end users to supply a country on their own. The easiest method by far is to install the [CountryList package by monarobase](https://github.com/Monarobase/country-list). Make sure the country field is named similar as the phone field but with *_country* appended for automatic discovery, or provide your custom country field name as a parameter to the validator:
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone',
|
||||
'phonefield_country' => 'required_with:phonefield',
|
||||
'phonefield' => 'phone',
|
||||
'phonefield_country' => 'required_with:phonefield',
|
||||
```
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone:custom_country_field',
|
||||
'custom_country_field' => 'required_with:phonefield',
|
||||
```
|
||||
|
||||
If using the CountryList package, you could then use the following snippet to populate a country selection list:
|
||||
@@ -101,7 +106,7 @@ You can also enable more lenient validation (for example, fixed lines without ar
|
||||
Format a fetched phone value using the helper function. `$country_code` is the country the phone number belongs to.
|
||||
|
||||
```php
|
||||
phone_format($phone_number, $country_code, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
phone_format($phone_number, $country_code = null, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
```
|
||||
|
||||
If no `$country_code` was given, the current application locale will be used as default.
|
||||
The `$format` parameter is optional and should be a constant of `libphonenumber\PhoneNumberFormat` (defaults to `libphonenumber\PhoneNumberFormat::INTERNATIONAL`)
|
||||
|
||||
76
vendor/propaganistas/laravel-phone/composer.json
vendored
76
vendor/propaganistas/laravel-phone/composer.json
vendored
@@ -1,38 +1,44 @@
|
||||
{
|
||||
"name": "propaganistas/laravel-phone",
|
||||
"description": "Adds a phone validator to Laravel based on Google's libphonenumber API.",
|
||||
"keywords": ["laravel", "libphonenumber", "validation", "phone"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Propaganistas",
|
||||
"email": "Propaganistas@users.noreply.github.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/support": "~4.0|~5.0",
|
||||
"illuminate/validation": "~4.0|~5.0",
|
||||
"giggsey/libphonenumber-for-php": "~7.0"
|
||||
"name": "propaganistas/laravel-phone",
|
||||
"description": "Adds a phone validator to Laravel based on Google's libphonenumber API.",
|
||||
"keywords": [
|
||||
"laravel",
|
||||
"libphonenumber",
|
||||
"validation",
|
||||
"phone"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Propaganistas",
|
||||
"email": "Propaganistas@users.noreply.github.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/support": "~4.0|~5.0",
|
||||
"illuminate/validation": "~4.0|~5.0",
|
||||
"giggsey/libphonenumber-for-php": "~7.0",
|
||||
"julien-c/iso3166": "~2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^3.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Propaganistas\\LaravelPhone\\": "src/"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^3.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Propaganistas\\LaravelPhone\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Propaganistas\\LaravelPhone\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"monarobase/country-list": "Adds a compatible (and fully translated) country list API."
|
||||
},
|
||||
"prefer-stable": true
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Propaganistas\\LaravelPhone\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"monarobase/country-list": "Adds a compatible (and fully translated) country list API."
|
||||
},
|
||||
"prefer-stable": true
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class InvalidParameterException extends \Exception {}
|
||||
class InvalidParameterException extends \Exception {}
|
||||
@@ -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()
|
||||
{
|
||||
// Make libphonenumber available in the application container.
|
||||
$this->app->singleton('libphonenumber', function($app) {
|
||||
return PhoneNumberUtil::getInstance();
|
||||
});
|
||||
$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use Iso3166\Codes as ISO3166;
|
||||
use libphonenumber\NumberParseException;
|
||||
use libphonenumber\PhoneNumberType;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
@@ -9,218 +10,247 @@ use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
|
||||
class PhoneValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
|
||||
/**
|
||||
* Whether the country should be auto-detected.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autodetect = false;
|
||||
/**
|
||||
* Dotted validator data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $lenient = false;
|
||||
/**
|
||||
* Whether the country should be auto-detected.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autodetect = false;
|
||||
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
/**
|
||||
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $lenient = false;
|
||||
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
/**
|
||||
* The field to use for country if not passed as a parameter.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $countryField = null;
|
||||
|
||||
/**
|
||||
* PhoneValidator constructor.
|
||||
*/
|
||||
public function __construct(PhoneNumberUtil $lib)
|
||||
{
|
||||
$this->lib = $lib;
|
||||
}
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
|
||||
/**
|
||||
* 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->assignParameters(array_map('strtoupper', $parameters));
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
|
||||
$this->checkCountries($attribute, $validator);
|
||||
/**
|
||||
* PhoneValidator constructor.
|
||||
*/
|
||||
public function __construct(PhoneNumberUtil $lib)
|
||||
{
|
||||
$this->lib = $lib;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
/**
|
||||
* 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());
|
||||
|
||||
foreach ($this->countries as $country) {
|
||||
if ($this->isValidNumber($value, $country)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$this->assignParameters($parameters);
|
||||
|
||||
// All specified country validations have failed.
|
||||
return false;
|
||||
}
|
||||
$this->checkCountries($attribute);
|
||||
|
||||
/**
|
||||
* Parses the supplied validator parameters.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
*/
|
||||
protected function assignParameters(array $parameters)
|
||||
{
|
||||
$types = array();
|
||||
// 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 ($parameters as $parameter) {
|
||||
if ($this->isPhoneCountry($parameter)) {
|
||||
$this->countries[] = $parameter;
|
||||
} elseif ($this->isPhoneType($parameter)) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
foreach ($this->countries as $country) {
|
||||
if ($this->isValidNumber($value, $country)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$this->types = $this->parseTypes($types);
|
||||
// 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();
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param $validator
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function checkCountries($attribute, $validator)
|
||||
{
|
||||
$data = array_dot($validator->getData());
|
||||
$countryField = $attribute . '_country';
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data[$countryField])) {
|
||||
$this->countries = array($data[$countryField]);
|
||||
} elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
$this->types = $this->parseTypes($types);
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
// Lenient validation.
|
||||
if ($this->lenient) {
|
||||
return $this->lib->isPossibleNumber($phoneNumber, $country);
|
||||
}
|
||||
if ($this->isInputField($countryField)) {
|
||||
$this->countries = array(array_get($this->data, $countryField));
|
||||
} elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
|
||||
// For automatic detection, the number should have a country code.
|
||||
if ($phoneNumber->hasCountryCode()) {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
// Automatic detection:
|
||||
if ($this->autodetect) {
|
||||
// Validate if the international phone number is valid for its contained country.
|
||||
return $this->lib->isValidNumber($phoneNumber);
|
||||
}
|
||||
// Check if type is allowed.
|
||||
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
|
||||
|
||||
// Validate number against the specified country.
|
||||
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
|
||||
}
|
||||
}
|
||||
// Lenient validation.
|
||||
if ($this->lenient) {
|
||||
return $this->lib->isPossibleNumber($phoneNumber, $country);
|
||||
}
|
||||
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
// For automatic detection, the number should have a country code.
|
||||
if ($phoneNumber->hasCountryCode()) {
|
||||
|
||||
return false;
|
||||
}
|
||||
// Automatic detection:
|
||||
if ($this->autodetect) {
|
||||
// Validate if the international phone number is valid for its contained country.
|
||||
return $this->lib->isValidNumber($phoneNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
});
|
||||
// Validate number against the specified country.
|
||||
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
|
||||
}
|
||||
}
|
||||
|
||||
// Add in the unsure number type if applicable.
|
||||
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
|
||||
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
|
||||
}
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
|
||||
return $types;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the supplied string is a valid country code using some arbitrary country validation.
|
||||
* If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'.
|
||||
*
|
||||
* @param string $country
|
||||
* @return bool
|
||||
*/
|
||||
public function isPhoneCountry($country)
|
||||
{
|
||||
return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ');
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
});
|
||||
|
||||
/**
|
||||
* 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);
|
||||
// Add in the unsure number type if applicable.
|
||||
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
|
||||
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
|
||||
}
|
||||
|
||||
return defined('\libphonenumber\PhoneNumberType::' . $type);
|
||||
}
|
||||
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 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);
|
||||
|
||||
return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,19 +4,24 @@ 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, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
{
|
||||
$lib = App::make('libphonenumber');
|
||||
$phoneNumber = $lib->parse($phone, $country);
|
||||
/**
|
||||
* 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');
|
||||
|
||||
return $lib->format($phoneNumber, $format);
|
||||
}
|
||||
if (!$country) {
|
||||
$country = App::getLocale();
|
||||
}
|
||||
|
||||
$phoneNumber = $lib->parse($phone, $country);
|
||||
|
||||
return $lib->format($phoneNumber, $format);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
class Laravel4 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders()
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
protected function getPackageProviders()
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
class Laravel5 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,205 +6,361 @@ use Orchestra\Testbench\TestCase;
|
||||
class PhoneValidatorTest extends TestCase
|
||||
{
|
||||
|
||||
protected $validator;
|
||||
protected $validator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->validator = $this->app['validator'];
|
||||
}
|
||||
$this->validator = $this->app['validator'];
|
||||
}
|
||||
|
||||
private function performValidation($data)
|
||||
{
|
||||
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
|
||||
private function performValidation($data)
|
||||
{
|
||||
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
|
||||
|
||||
$input = array_only($data, ['field', 'field_country']);
|
||||
$input['nested']['field'] = $input['field'];
|
||||
if (isset($input['field_country'])) {
|
||||
$input['nested']['field_country'] = $input['field_country'];
|
||||
}
|
||||
$input = array_except($data, ['rule']);
|
||||
$input['nested']['field'] = $input['field'];
|
||||
if (isset($input['field_country'])) {
|
||||
$input['nested']['field_country'] = $input['field_country'];
|
||||
}
|
||||
|
||||
$validator = $this->validator->make($input,
|
||||
['field' => $rule,
|
||||
'nested.field' => $rule]
|
||||
);
|
||||
$validator = $this->validator->make($input,
|
||||
[
|
||||
'field' => $rule,
|
||||
'nested.field' => $rule
|
||||
]
|
||||
);
|
||||
|
||||
return $validator->passes();
|
||||
}
|
||||
return $validator->passes();
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithDefaultCountryNoType()
|
||||
{
|
||||
// Validator with correct country field.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'BE']));
|
||||
public function testValidatePhoneWithDefaultCountryNoType()
|
||||
{
|
||||
// Validator with correct country field.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'NL']));
|
||||
// Validator with wrong country value.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'BE,NL']));
|
||||
// Validator with multiple country values, one correct.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,NL'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, value correct for second country in list.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'NL,BE']));
|
||||
// Validator with multiple country values, value correct for second country in list.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL,BE'
|
||||
]));
|
||||
|
||||
// Validator with multiple wrong country values.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'DE,NL']));
|
||||
}
|
||||
// Validator with multiple wrong country values.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'DE,NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithCountryFieldNoType()
|
||||
{
|
||||
// Validator with correct country field supplied.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'field_country' => 'BE']));
|
||||
public function testValidatePhoneWithCountryFieldNoType()
|
||||
{
|
||||
// Validator with correct country field supplied.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'field_country' => 'NL']));
|
||||
}
|
||||
// Validator with wrong country field supplied.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithDefaultCountryWithType()
|
||||
{
|
||||
// Validator with correct country value, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'BE,mobile']));
|
||||
public function testValidatePhoneWithDefaultCountryWithType()
|
||||
{
|
||||
// Validator with correct country value, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'BE,mobile'
|
||||
]));
|
||||
|
||||
// Validator with correct country value, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'BE,mobile']));
|
||||
// Validator with correct country value, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,mobile'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'NL,mobile']));
|
||||
// Validator with wrong country value, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'NL,mobile']));
|
||||
// Validator with wrong country value, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'BE,NL,mobile']));
|
||||
// Validator with multiple country values, one correct, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'BE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'BE,NL,mobile']));
|
||||
// Validator with multiple country values, one correct, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, none correct, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'DE,NL,mobile']));
|
||||
// Validator with multiple country values, none correct, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'DE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, none correct, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'DE,NL,mobile']));
|
||||
}
|
||||
// Validator with multiple country values, none correct, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'DE,NL,mobile'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithCountryFieldWithType()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'mobile', 'field_country' => 'BE']));
|
||||
public function testValidatePhoneWithCountryFieldWithType()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'mobile', 'field_country' => 'BE'
|
||||
]));
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'mobile', 'field_country' => 'NL'
|
||||
]));
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'mobile', 'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
|
||||
{
|
||||
// Validator with correct international input.
|
||||
$this->assertTrue($this->performValidation(['field' => '+3216123456', 'rule' => 'AUTO']));
|
||||
public function testValidatePhoneWithCustomCountryField()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '003216123456', 'rule' => 'AUTO']));
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '+321456', 'rule' => 'AUTO']));
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input but correct default country.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,BE']));
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
// Validator with wrong international input and wrong default country.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,DE']));
|
||||
}
|
||||
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
|
||||
{
|
||||
// Validator with correct international input.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '+3216123456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
public function testValidatePhoneNoDefaultCountryNoCountryField()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '003216123456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country.
|
||||
$this->performValidation(['field' => '016123456']);
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '+321456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country, wrong type.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'mobile']);
|
||||
// Validator with wrong international input but correct default country.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'AUTO,NL,BE'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country, correct type.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile']);
|
||||
// Validator with wrong international input and wrong default country.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'AUTO,NL,DE'
|
||||
]));
|
||||
}
|
||||
|
||||
// Validator with no country field or given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile,xyz']);
|
||||
}
|
||||
public function testValidatePhoneNoDefaultCountryNoCountryField()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
|
||||
|
||||
public function testValidatePhoneLenient()
|
||||
{
|
||||
// Validator with AU area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '88885555', 'rule' => 'AU']));
|
||||
// Validator with no country field or given country.
|
||||
$this->performValidation([
|
||||
'field' => '016123456'
|
||||
]);
|
||||
|
||||
// Validator with AU area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT,AU']));
|
||||
// Validator with no country field or given country, wrong type.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile'
|
||||
]);
|
||||
|
||||
// Validator with correct country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'AU']));
|
||||
// Validator with no country field or given country, correct type.
|
||||
$this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile'
|
||||
]);
|
||||
|
||||
// Validator with wrong country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'BE']));
|
||||
// Validator with no country field or given country, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,xyz'
|
||||
]);
|
||||
}
|
||||
|
||||
// Validator with no area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT']));
|
||||
public function testValidatePhoneLenient()
|
||||
{
|
||||
// Validator with AU area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'AU'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT,US']));
|
||||
// Validator with AU area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT,AU'
|
||||
]));
|
||||
|
||||
// Validator with no area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT']));
|
||||
// Validator with correct country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT',
|
||||
'field_country' => 'AU'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT,US']));
|
||||
// Validator with wrong country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT']));
|
||||
// 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' => '2530000', 'rule' => 'LENIENT,US']));
|
||||
}
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '+16502530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
// Validator with no area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '6502530000',
|
||||
'rule' => 'LENIENT'
|
||||
]));
|
||||
|
||||
// Validator with given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'BE,mobile,xyz']);
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '6502530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
|
||||
// Validator with country field, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'mobile,xyz', 'field_country' => 'BE']);
|
||||
}
|
||||
// Validator with US area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '2530000',
|
||||
'rule' => 'LENIENT'
|
||||
]));
|
||||
|
||||
public function testPhoneFormatHelperFunction()
|
||||
{
|
||||
// Test landline number without format parameter.
|
||||
$actual = phone_format('016123456', 'BE');
|
||||
$expected = '+32 16 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '2530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
}
|
||||
|
||||
// Test landline number with format parameter.
|
||||
$actual = phone_format('016123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '016 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
|
||||
// Test mobile number without format parameter.
|
||||
$actual = phone_format('0499123456', 'BE');
|
||||
$expected = '+32 499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
// Validator with given country, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,mobile,xyz'
|
||||
]);
|
||||
|
||||
// Test mobile number with format parameter.
|
||||
$actual = phone_format('0499123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '0499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
// Validator with country field, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,xyz',
|
||||
'field_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', 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', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '0499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user