update v1.0.4
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
/vendor
|
||||
composer.phar
|
||||
composer.lock
|
||||
.DS_Store
|
||||
|
||||
20
vendor/propaganistas/laravel-phone/.travis.yml
vendored
20
vendor/propaganistas/laravel-phone/.travis.yml
vendored
@@ -1,17 +1,25 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- hhvm
|
||||
matrix:
|
||||
include:
|
||||
- php: 5.4
|
||||
env: PHPUNIT=laravel4.phpunit.xml
|
||||
before_script: composer require orchestra/testbench ~2.0.0
|
||||
- php: 5.5
|
||||
env: PHPUNIT=phpunit.xml
|
||||
- php: 5.6
|
||||
env: PHPUNIT=phpunit.xml
|
||||
- php: 7.0
|
||||
env: PHPUNIT=phpunit.xml
|
||||
- php: hhvm
|
||||
env: PHPUNIT=phpunit.xml
|
||||
|
||||
before_script:
|
||||
- composer self-update
|
||||
- composer install --prefer-source --no-interaction --dev
|
||||
|
||||
script:
|
||||
- phpunit --coverage-clover=coverage.clover
|
||||
- phpunit --coverage-clover=coverage.clover --configuration $PHPUNIT
|
||||
|
||||
after_script:
|
||||
- wget https://scrutinizer-ci.com/ocular.phar
|
||||
|
||||
10
vendor/propaganistas/laravel-phone/README.md
vendored
10
vendor/propaganistas/laravel-phone/README.md
vendored
@@ -1,6 +1,6 @@
|
||||
# Laravel Phone Validator
|
||||
|
||||
[](https://travis-ci.org/Propaganistas/Laravel-Phone)
|
||||
[](https://travis-ci.org/Propaganistas/Laravel-Phone)
|
||||
[](https://scrutinizer-ci.com/g/Propaganistas/Laravel-Phone/?branch=master)
|
||||
[](https://scrutinizer-ci.com/g/Propaganistas/Laravel-Phone/?branch=master)
|
||||
[](https://packagist.org/packages/propaganistas/laravel-phone)
|
||||
@@ -20,7 +20,7 @@ Adds a phone validator to Laravel 4 and 5 based on the [PHP port](https://github
|
||||
2. Run the Composer update command
|
||||
|
||||
```bash
|
||||
$ composer update
|
||||
composer update
|
||||
```
|
||||
|
||||
3. In your app config, add the Service Provider to the end of the `$providers` array
|
||||
@@ -76,13 +76,13 @@ To validate a field using the phone validator, use the `phone` keyword in your v
|
||||
Countries::getList(App::getLocale(), 'php', 'cldr'))
|
||||
```
|
||||
|
||||
- You instruct the validator to detect which country the number belongs to using the `AUTO` keyword:
|
||||
- You instruct the validator to detect which country the number belongs to using the `AUTO` keyword (and optionally any fallback countries):
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone:AUTO',
|
||||
'phonefield' => 'phone:AUTO,US',
|
||||
```
|
||||
|
||||
The validator will try to extract the country from the number itself and then check if the number is valid for that country. Note that this will only work when phone numbers are entered in *international format* (prefixed with a `+` sign, e.g. +32 ....). Leading double zeros will **NOT** be parsed correctly as this isn't an established consistency.
|
||||
The validator will try to extract the country from the number itself and then check if the number is valid for that country. If the country could not be guessed it will be validated using the fallback countries if provided. Note that country guessing will only work when phone numbers are entered in *international format* (prefixed with a `+` sign, e.g. +32 ....). Leading double zeros will **NOT** be parsed correctly as this isn't an established consistency.
|
||||
|
||||
To specify constraints on the number type, just append the allowed types to the end of the parameters, e.g.:
|
||||
|
||||
|
||||
12
vendor/propaganistas/laravel-phone/composer.json
vendored
12
vendor/propaganistas/laravel-phone/composer.json
vendored
@@ -15,8 +15,8 @@
|
||||
"illuminate/validation": "~4.0|~5.0",
|
||||
"giggsey/libphonenumber-for-php": "~7.0"
|
||||
},
|
||||
"suggest": {
|
||||
"monarobase/country-list": "Adds a compatible (and fully translated) country list API."
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^3.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -26,5 +26,13 @@
|
||||
"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
|
||||
}
|
||||
|
||||
29
vendor/propaganistas/laravel-phone/laravel4.phpunit.xml
vendored
Normal file
29
vendor/propaganistas/laravel-phone/laravel4.phpunit.xml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Package Test Suite">
|
||||
<file>./tests/Laravel4.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">./src</directory>
|
||||
<exclude>
|
||||
<file>./src/LaravelPhoneServiceProvider.php</file>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="coverage-clover" target="./clover.xml"/>
|
||||
</logging>
|
||||
</phpunit>
|
||||
@@ -12,7 +12,7 @@
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Package Test Suite">
|
||||
<directory suffix=".php">./tests/</directory>
|
||||
<file>./tests/Laravel5.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class InvalidParameterException extends \Exception {}
|
||||
class InvalidParameterException extends \Exception {}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class NoValidCountryFoundException extends \Exception {}
|
||||
class NoValidCountryFoundException extends \Exception {}
|
||||
|
||||
@@ -5,32 +5,28 @@ use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
class LaravelPhoneServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Indicates if loading of the provider is deferred.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $defer = false;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 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,193 +1,209 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use libphonenumber\NumberParseException;
|
||||
use libphonenumber\PhoneNumberType;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException;
|
||||
use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
|
||||
|
||||
class PhoneValidator {
|
||||
class PhoneValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* The phone number attribute.
|
||||
* @var string
|
||||
*/
|
||||
protected $attribute;
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
|
||||
/**
|
||||
* Data from the validator instance.
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* Whether the country should be auto-detected.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autodetect = false;
|
||||
|
||||
/**
|
||||
* Countries to validate against.
|
||||
* @var array
|
||||
*/
|
||||
protected $allowedCountries = array();
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
|
||||
/**
|
||||
* Untransformed phone number types to validate against.
|
||||
* @var array
|
||||
*/
|
||||
protected $untransformedTypes = array();
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
* @var array
|
||||
*/
|
||||
protected $allowedTypes = array();
|
||||
/**
|
||||
* PhoneValidator constructor.
|
||||
*/
|
||||
public function __construct(PhoneNumberUtil $lib)
|
||||
{
|
||||
$this->lib = $lib;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supplied validator parameters.
|
||||
* @var array
|
||||
*/
|
||||
protected $parameters;
|
||||
/**
|
||||
* 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));
|
||||
|
||||
/**
|
||||
* Validates a phone number.
|
||||
*/
|
||||
public function validatePhone($attribute, $value, $parameters, $validator)
|
||||
{
|
||||
$this->attribute = $attribute;
|
||||
$this->data = $validator->getData();
|
||||
$this->parameters = array_map('strtoupper', $parameters);
|
||||
$this->checkCountries($attribute, $validator);
|
||||
|
||||
$this->determineCountries();
|
||||
$this->determineTypes();
|
||||
$this->checkLeftoverParameters();
|
||||
// If autodetecting, let's first try without a country.
|
||||
// Otherwise use provided countries as default.
|
||||
if ($this->autodetect) {
|
||||
array_unshift($this->countries, null);
|
||||
}
|
||||
|
||||
$phoneUtil = PhoneNumberUtil::getInstance();
|
||||
foreach ($this->countries as $country) {
|
||||
if ($this->isValidNumber($value, $country)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Perform validation.
|
||||
foreach ($this->allowedCountries as $country) {
|
||||
try {
|
||||
// For default countries or country field, the following throws NumberParseException if
|
||||
// not parsed correctly against the supplied country.
|
||||
// For automatic detection: tries to discover the country code using from the number itself.
|
||||
$phoneProto = $phoneUtil->parse($value, $country);
|
||||
// All specified country validations have failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
// For automatic detection, the number should have a country code.
|
||||
// Check if type is allowed.
|
||||
if ($phoneProto->hasCountryCode() && empty($this->allowedTypes) || in_array($phoneUtil->getNumberType($phoneProto), $this->allowedTypes)) {
|
||||
/**
|
||||
* Parses the supplied validator parameters.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
*/
|
||||
protected function assignParameters(array $parameters)
|
||||
{
|
||||
$types = array();
|
||||
|
||||
// Automatic detection:
|
||||
if ($country == 'ZZ') {
|
||||
// Validate if the international phone number is valid for its contained country.
|
||||
return $phoneUtil->isValidNumber($phoneProto);
|
||||
}
|
||||
foreach ($parameters as $parameter) {
|
||||
if ($this->isPhoneCountry($parameter)) {
|
||||
$this->countries[] = $parameter;
|
||||
} elseif ($this->isPhoneType($parameter)) {
|
||||
$types[] = $parameter;
|
||||
} elseif ($parameter == 'AUTO') {
|
||||
$this->autodetect = true;
|
||||
} else {
|
||||
// Force developers to write proper code.
|
||||
throw new InvalidParameterException($parameter);
|
||||
}
|
||||
}
|
||||
|
||||
// Force validation of number against the specified country.
|
||||
return $phoneUtil->isValidNumberForRegion($phoneProto, $country);
|
||||
}
|
||||
$this->types = $this->parseTypes($types);
|
||||
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* 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 $attribute
|
||||
* @param $validator
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function checkCountries($attribute, $validator)
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$countryField = $attribute . '_country';
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
if (isset($data[$countryField])) {
|
||||
$this->countries = array($data[$countryField]);
|
||||
} elseif (!$this->autodetect && empty($this->countries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
/**
|
||||
* 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);
|
||||
|
||||
return defined($this->constructPhoneTypeConstant($type));
|
||||
}
|
||||
// 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))) {
|
||||
|
||||
/**
|
||||
* Constructs the corresponding namespaced class constant for a phone number type.
|
||||
*
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
protected function constructPhoneTypeConstant($type)
|
||||
{
|
||||
return '\libphonenumber\PhoneNumberType::' . $type;
|
||||
}
|
||||
// Automatic detection:
|
||||
if ($this->autodetect) {
|
||||
// Validate if the international phone number is valid for its contained country.
|
||||
return $this->lib->isValidNumber($phoneNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the countries to validate against.
|
||||
*
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function determineCountries()
|
||||
{
|
||||
// Check for the existence of a country field.
|
||||
$field = $this->attribute . '_country';
|
||||
if (isset($this->data[$field])) {
|
||||
$this->allowedCountries = ($this->isPhoneCountry($this->data[$field])) ? array($this->data[$field]) : array();
|
||||
// No exception should be thrown since empty country fields should validate to false.
|
||||
}
|
||||
// Or if we need to parse for automatic detection.
|
||||
elseif (in_array('AUTO', $this->parameters)) {
|
||||
$this->allowedCountries = array('ZZ');
|
||||
}
|
||||
// Else use the supplied parameters.
|
||||
else {
|
||||
$this->allowedCountries = array_filter($this->parameters, function($item) {
|
||||
return $this->isPhoneCountry($item);
|
||||
});
|
||||
// Validate number against the specified country.
|
||||
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
|
||||
}
|
||||
|
||||
if (empty($this->allowedCountries)) {
|
||||
throw new NoValidCountryFoundException;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NumberParseException $e) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the phone number types to validate against.
|
||||
*/
|
||||
protected function determineTypes()
|
||||
{
|
||||
// Get phone types.
|
||||
$this->untransformedTypes = array_filter($this->parameters, function($item) {
|
||||
return $this->isPhoneType($item);
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// Transform valid types to their namespaced class constant.
|
||||
$this->allowedTypes = array_map(function($item) {
|
||||
return constant($this->constructPhoneTypeConstant($item));
|
||||
}, $this->untransformedTypes);
|
||||
/**
|
||||
* 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(['FIXED_LINE', 'MOBILE'], $this->parameters)) {
|
||||
$this->allowedTypes[] = 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for parameter leftovers to force developers to write proper code.
|
||||
*
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
|
||||
*/
|
||||
protected function checkLeftoverParameters()
|
||||
{
|
||||
// Remove the automatic detection option if applicable.
|
||||
$leftovers = array_diff($this->parameters, $this->allowedCountries, $this->untransformedTypes, array('AUTO'));
|
||||
if (!empty($leftovers)) {
|
||||
throw new InvalidParameterException(implode(', ', $leftovers));
|
||||
}
|
||||
}
|
||||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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::' . $type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\App;
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
|
||||
function phone_format($phone, $country, $format = null) {
|
||||
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
|
||||
$phoneProto = $phoneUtil->parse($phone, $country);
|
||||
$format = is_null($format) ? \libphonenumber\PhoneNumberFormat::INTERNATIONAL : $format;
|
||||
return $phoneUtil->format($phoneProto, $format);
|
||||
$lib = App::make('libphonenumber');
|
||||
$phoneNumber = $lib->parse($phone, $country);
|
||||
$format = is_null($format) ? PhoneNumberFormat::INTERNATIONAL : $format;
|
||||
return $lib->format($phoneNumber, $format);
|
||||
}
|
||||
|
||||
10
vendor/propaganistas/laravel-phone/tests/Laravel4.php
vendored
Normal file
10
vendor/propaganistas/laravel-phone/tests/Laravel4.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Tests;
|
||||
|
||||
class Laravel4 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders()
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
10
vendor/propaganistas/laravel-phone/tests/Laravel5.php
vendored
Normal file
10
vendor/propaganistas/laravel-phone/tests/Laravel5.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Tests;
|
||||
|
||||
class Laravel5 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
167
vendor/propaganistas/laravel-phone/tests/PhoneValidatorTest.php
vendored
Normal file
167
vendor/propaganistas/laravel-phone/tests/PhoneValidatorTest.php
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Tests;
|
||||
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
|
||||
class PhoneValidatorTest extends TestCase
|
||||
{
|
||||
|
||||
protected $validator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->validator = $this->app['validator'];
|
||||
}
|
||||
|
||||
private function performValidation($data)
|
||||
{
|
||||
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
|
||||
$validator = $this->validator->make(array_only($data, ['field', 'field_country']), ['field' => $rule]);
|
||||
|
||||
return $validator->passes();
|
||||
}
|
||||
|
||||
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 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 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']));
|
||||
|
||||
// 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']));
|
||||
|
||||
// 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, 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, 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, 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']));
|
||||
|
||||
// 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, 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']));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '003216123456', 'rule' => 'AUTO']));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '+321456', 'rule' => 'AUTO']));
|
||||
|
||||
// Validator with wrong international input but correct default country.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,BE']));
|
||||
|
||||
// Validator with wrong international input and wrong default country.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,DE']));
|
||||
}
|
||||
|
||||
public function testValidatePhoneNoDefaultCountryNoCountryField()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
|
||||
|
||||
// Validator with no country field or given country.
|
||||
$this->performValidation(['field' => '016123456']);
|
||||
|
||||
// Validator with no country field or given country, wrong type.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'mobile']);
|
||||
|
||||
// Validator with no country field or given country, correct type.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile']);
|
||||
|
||||
// Validator with no country field or given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile,xyz']);
|
||||
}
|
||||
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
|
||||
// Validator with given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'BE,mobile,xyz']);
|
||||
|
||||
// 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