Update v1.0.6
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
/vendor
|
||||
composer.phar
|
||||
composer.lock
|
||||
@@ -1,17 +0,0 @@
|
||||
filter:
|
||||
paths:
|
||||
- 'src/*'
|
||||
excluded_paths:
|
||||
- 'tests/*'
|
||||
- 'bin/*'
|
||||
- 'spec/*'
|
||||
- 'stub/*'
|
||||
- 'coverage/*'
|
||||
- 'vendor/*'
|
||||
checks:
|
||||
php: true
|
||||
tools:
|
||||
external_code_coverage:
|
||||
timeout: 1200
|
||||
runs: 3
|
||||
php_code_coverage: false
|
||||
26
vendor/propaganistas/laravel-phone/.travis.yml
vendored
26
vendor/propaganistas/laravel-phone/.travis.yml
vendored
@@ -1,26 +0,0 @@
|
||||
language: php
|
||||
|
||||
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 --configuration $PHPUNIT
|
||||
|
||||
after_script:
|
||||
- wget https://scrutinizer-ci.com/ocular.phar
|
||||
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
|
||||
20
vendor/propaganistas/laravel-phone/LICENSE
vendored
20
vendor/propaganistas/laravel-phone/LICENSE
vendored
@@ -1,20 +0,0 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Propaganistas
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
107
vendor/propaganistas/laravel-phone/README.md
vendored
107
vendor/propaganistas/laravel-phone/README.md
vendored
@@ -1,107 +0,0 @@
|
||||
# Laravel Phone Validator
|
||||
|
||||
[](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)
|
||||
[](https://packagist.org/packages/propaganistas/laravel-phone)
|
||||
[](https://packagist.org/packages/propaganistas/laravel-phone)
|
||||
|
||||
Adds a phone validator to Laravel 4 and 5 based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://github.com/googlei18n/libphonenumber) by [giggsey](https://github.com/giggsey).
|
||||
|
||||
### Installation
|
||||
|
||||
1. In the `require` key of `composer.json` file add the following
|
||||
|
||||
```json
|
||||
"propaganistas/laravel-phone": "~2.0"
|
||||
```
|
||||
|
||||
2. Run the Composer update command
|
||||
|
||||
```bash
|
||||
composer update
|
||||
```
|
||||
|
||||
3. In your app config, add the Service Provider to the end of the `$providers` array
|
||||
|
||||
**Laravel 5**
|
||||
```php
|
||||
'providers' => [
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
...
|
||||
Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class,
|
||||
],
|
||||
```
|
||||
|
||||
**Laravel 4**
|
||||
```php
|
||||
'providers' => [
|
||||
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
|
||||
'Illuminate\Auth\AuthServiceProvider',
|
||||
...
|
||||
'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
|
||||
],
|
||||
```
|
||||
|
||||
4. In your languages directory, add for each language an extra language line for the validator:
|
||||
|
||||
```php
|
||||
"phone" => "The :attribute field contains an invalid number.",
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
To validate a field using the phone validator, use the `phone` keyword in your validation rules array. The phone validator is able to operate in **three** ways.
|
||||
|
||||
- You either specify [*ISO 3166-1 alpha-2 compliant*](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) country codes yourself as parameters for the validator, e.g.:
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone:US,BE',
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone',
|
||||
'phonefield_country' => 'required_with:phonefield',
|
||||
```
|
||||
|
||||
If using the CountryList package, you could then use the following snippet to populate a country selection list:
|
||||
|
||||
```php
|
||||
Countries::getList(App::getLocale(), 'php', 'cldr'))
|
||||
```
|
||||
|
||||
- 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,US',
|
||||
```
|
||||
|
||||
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.:
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone:US,BE,mobile',
|
||||
```
|
||||
The most common types are `mobile` and `fixed_line`, but feel free to use any of the types defined [here](https://github.com/giggsey/libphonenumber-for-php/blob/master/src/libphonenumber/PhoneNumberType.php).
|
||||
|
||||
You can also enable more lenient validation (for example, fixed lines without area codes) by using the `LENIENT` parameter. This feature inherently doesn't play well with country autodetection and number type validation, so use such combo at own risk.
|
||||
|
||||
```php
|
||||
'phonefield' => 'phone:LENIENT,US',
|
||||
```
|
||||
|
||||
### Display
|
||||
Format a fetched phone value using the helper function:
|
||||
|
||||
```php
|
||||
phone_format($phone_number, $country_code, $format = PhoneNumberFormat::INTERNATIONAL)
|
||||
```
|
||||
|
||||
The `$format` parameter is optional and should be a constant of `libphonenumber\PhoneNumberFormat` (defaults to `libphonenumber\PhoneNumberFormat::INTERNATIONAL`)
|
||||
38
vendor/propaganistas/laravel-phone/composer.json
vendored
38
vendor/propaganistas/laravel-phone/composer.json
vendored
@@ -1,38 +0,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"
|
||||
},
|
||||
"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
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?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>
|
||||
29
vendor/propaganistas/laravel-phone/phpunit.xml
vendored
29
vendor/propaganistas/laravel-phone/phpunit.xml
vendored
@@ -1,29 +0,0 @@
|
||||
<?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/Laravel5.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>
|
||||
@@ -1,3 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class InvalidParameterException extends \Exception {}
|
||||
@@ -1,3 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class NoValidCountryFoundException extends \Exception {}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,226 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use libphonenumber\NumberParseException;
|
||||
use libphonenumber\PhoneNumberType;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException;
|
||||
use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
|
||||
|
||||
class PhoneValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \libphonenumber\PhoneNumberUtil
|
||||
*/
|
||||
protected $lib;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Countries to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $countries = [];
|
||||
|
||||
/**
|
||||
* Transformed phone number types to validate against.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = [];
|
||||
|
||||
/**
|
||||
* 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->assignParameters(array_map('strtoupper', $parameters));
|
||||
|
||||
$this->checkCountries($attribute, $validator);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
* @param $validator
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
|
||||
*/
|
||||
protected function checkCountries($attribute, $validator)
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$countryField = $attribute . '_country';
|
||||
|
||||
if (isset($data[$countryField])) {
|
||||
$this->countries = array($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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// Proceed to default validation error.
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
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);
|
||||
|
||||
return $lib->format($phoneNumber, $format);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Tests;
|
||||
|
||||
class Laravel4 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders()
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Tests;
|
||||
|
||||
class Laravel5 extends PhoneValidatorTest
|
||||
{
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
|
||||
}
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
<?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 testValidatePhoneLenient()
|
||||
{
|
||||
// Validator with AU area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '88885555', 'rule' => 'AU']));
|
||||
|
||||
// Validator with AU area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT,AU']));
|
||||
|
||||
// Validator with correct country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'AU']));
|
||||
|
||||
// Validator with wrong country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'BE']));
|
||||
|
||||
// Validator with no area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT']));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT,US']));
|
||||
|
||||
// Validator with no area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT']));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT,US']));
|
||||
|
||||
// Validator with US area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT']));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT,US']));
|
||||
}
|
||||
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
|
||||
// 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