@@ -31,7 +31,7 @@  | 
                                                    ||
| 31 | 31 | public function register()  | 
                                                        
| 32 | 32 |      { | 
                                                        
| 33 | 33 | // Make libphonenumber available in the application container.  | 
                                                        
| 34 | -        $this->app->singleton('libphonenumber', function ($app) { | 
                                                        |
| 34 | +        $this->app->singleton('libphonenumber', function($app) { | 
                                                        |
| 35 | 35 | return PhoneNumberUtil::getInstance();  | 
                                                        
| 36 | 36 | });  | 
                                                        
| 37 | 37 | }  | 
                                                        
@@ -6,27 +6,27 @@  | 
                                                    ||
| 6 | 6 | class LaravelPhoneServiceProvider extends ServiceProvider  | 
                                                        
| 7 | 7 |  { | 
                                                        
| 8 | 8 | |
| 9 | - /**  | 
                                                        |
| 10 | - * Bootstrap the application events.  | 
                                                        |
| 11 | - *  | 
                                                        |
| 12 | - * @return void  | 
                                                        |
| 13 | - */  | 
                                                        |
| 14 | - public function boot()  | 
                                                        |
| 15 | -    { | 
                                                        |
| 16 | -        $this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); | 
                                                        |
| 17 | - }  | 
                                                        |
| 9 | + /**  | 
                                                        |
| 10 | + * Bootstrap the application events.  | 
                                                        |
| 11 | + *  | 
                                                        |
| 12 | + * @return void  | 
                                                        |
| 13 | + */  | 
                                                        |
| 14 | + public function boot()  | 
                                                        |
| 15 | +	{ | 
                                                        |
| 16 | +		$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); | 
                                                        |
| 17 | + }  | 
                                                        |
| 18 | 18 | |
| 19 | - /**  | 
                                                        |
| 20 | - * Register the service provider.  | 
                                                        |
| 21 | - *  | 
                                                        |
| 22 | - * @return void  | 
                                                        |
| 23 | - */  | 
                                                        |
| 24 | - public function register()  | 
                                                        |
| 25 | -    { | 
                                                        |
| 26 | -        $this->app->singleton('libphonenumber', function ($app) { | 
                                                        |
| 27 | - return PhoneNumberUtil::getInstance();  | 
                                                        |
| 28 | - });  | 
                                                        |
| 19 | + /**  | 
                                                        |
| 20 | + * Register the service provider.  | 
                                                        |
| 21 | + *  | 
                                                        |
| 22 | + * @return void  | 
                                                        |
| 23 | + */  | 
                                                        |
| 24 | + public function register()  | 
                                                        |
| 25 | +	{ | 
                                                        |
| 26 | +		$this->app->singleton('libphonenumber', function ($app) { | 
                                                        |
| 27 | + return PhoneNumberUtil::getInstance();  | 
                                                        |
| 28 | + });  | 
                                                        |
| 29 | 29 | |
| 30 | -        $this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil'); | 
                                                        |
| 31 | - }  | 
                                                        |
| 30 | +		$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil'); | 
                                                        |
| 31 | + }  | 
                                                        |
| 32 | 32 | }  | 
                                                        
@@ -4,43 +4,43 @@  | 
                                                    ||
| 4 | 4 | use libphonenumber\PhoneNumberFormat;  | 
                                                        
| 5 | 5 | |
| 6 | 6 |  if (! function_exists('phone_format')) { | 
                                                        
| 7 | - /**  | 
                                                        |
| 8 | - * Get the PhoneNumberUtil or format a phone number for display.  | 
                                                        |
| 9 | - *  | 
                                                        |
| 10 | - * @return \libphonenumber\PhoneNumberUtil|string  | 
                                                        |
| 11 | - */  | 
                                                        |
| 12 | - function phone()  | 
                                                        |
| 13 | -    { | 
                                                        |
| 14 | -        $lib = App::make('libphonenumber'); | 
                                                        |
| 7 | + /**  | 
                                                        |
| 8 | + * Get the PhoneNumberUtil or format a phone number for display.  | 
                                                        |
| 9 | + *  | 
                                                        |
| 10 | + * @return \libphonenumber\PhoneNumberUtil|string  | 
                                                        |
| 11 | + */  | 
                                                        |
| 12 | + function phone()  | 
                                                        |
| 13 | +	{ | 
                                                        |
| 14 | +		$lib = App::make('libphonenumber'); | 
                                                        |
| 15 | 15 | |
| 16 | -        if (! $arguments = func_get_args()) { | 
                                                        |
| 17 | - return $lib;  | 
                                                        |
| 18 | - }  | 
                                                        |
| 16 | +		if (! $arguments = func_get_args()) { | 
                                                        |
| 17 | + return $lib;  | 
                                                        |
| 18 | + }  | 
                                                        |
| 19 | 19 | |
| 20 | - $phone = $arguments[0];  | 
                                                        |
| 21 | - $country = isset($arguments[1]) ? $arguments[1] : App::getLocale();  | 
                                                        |
| 22 | - $format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL;  | 
                                                        |
| 20 | + $phone = $arguments[0];  | 
                                                        |
| 21 | + $country = isset($arguments[1]) ? $arguments[1] : App::getLocale();  | 
                                                        |
| 22 | + $format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL;  | 
                                                        |
| 23 | 23 | |
| 24 | - return $lib->format(  | 
                                                        |
| 25 | - $lib->parse($phone, $country),  | 
                                                        |
| 26 | - $format  | 
                                                        |
| 27 | - );  | 
                                                        |
| 28 | - }  | 
                                                        |
| 24 | + return $lib->format(  | 
                                                        |
| 25 | + $lib->parse($phone, $country),  | 
                                                        |
| 26 | + $format  | 
                                                        |
| 27 | + );  | 
                                                        |
| 28 | + }  | 
                                                        |
| 29 | 29 | }  | 
                                                        
| 30 | 30 | |
| 31 | 31 |  if (! function_exists('phone_format')) { | 
                                                        
| 32 | - /**  | 
                                                        |
| 33 | - * Formats a phone number and country for display.  | 
                                                        |
| 34 | - *  | 
                                                        |
| 35 | - * @param string $phone  | 
                                                        |
| 36 | - * @param string $country  | 
                                                        |
| 37 | - * @param int|null $format  | 
                                                        |
| 38 | - * @return string  | 
                                                        |
| 39 | - *  | 
                                                        |
| 40 | - * @deprecated 2.8.0  | 
                                                        |
| 41 | - */  | 
                                                        |
| 42 | - function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL)  | 
                                                        |
| 43 | -    { | 
                                                        |
| 44 | - return phone($phone, $country, $format);  | 
                                                        |
| 45 | - }  | 
                                                        |
| 32 | + /**  | 
                                                        |
| 33 | + * Formats a phone number and country for display.  | 
                                                        |
| 34 | + *  | 
                                                        |
| 35 | + * @param string $phone  | 
                                                        |
| 36 | + * @param string $country  | 
                                                        |
| 37 | + * @param int|null $format  | 
                                                        |
| 38 | + * @return string  | 
                                                        |
| 39 | + *  | 
                                                        |
| 40 | + * @deprecated 2.8.0  | 
                                                        |
| 41 | + */  | 
                                                        |
| 42 | + function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL)  | 
                                                        |
| 43 | +	{ | 
                                                        |
| 44 | + return phone($phone, $country, $format);  | 
                                                        |
| 45 | + }  | 
                                                        |
| 46 | 46 | }  | 
                                                        
@@ -3,7 +3,7 @@ discard block  | 
                                                    ||
| 3 | 3 | use Illuminate\Support\Facades\App;  | 
                                                        
| 4 | 4 | use libphonenumber\PhoneNumberFormat;  | 
                                                        
| 5 | 5 | |
| 6 | -if (! function_exists('phone')) { | 
                                                        |
| 6 | +if (!function_exists('phone')) { | 
                                                        |
| 7 | 7 | /**  | 
                                                        
| 8 | 8 | * Get the PhoneNumberUtil or format a phone number for display.  | 
                                                        
| 9 | 9 | *  | 
                                                        
@@ -13,7 +13,7 @@ discard block  | 
                                                    ||
| 13 | 13 |      { | 
                                                        
| 14 | 14 |          $lib = App::make('libphonenumber'); | 
                                                        
| 15 | 15 | |
| 16 | -        if (! $arguments = func_get_args()) { | 
                                                        |
| 16 | +        if (!$arguments = func_get_args()) { | 
                                                        |
| 17 | 17 | return $lib;  | 
                                                        
| 18 | 18 | }  | 
                                                        
| 19 | 19 | |
@@ -28,7 +28,7 @@ discard block  | 
                                                    ||
| 28 | 28 | }  | 
                                                        
| 29 | 29 | }  | 
                                                        
| 30 | 30 | |
| 31 | -if (! function_exists('phone_format')) { | 
                                                        |
| 31 | +if (!function_exists('phone_format')) { | 
                                                        |
| 32 | 32 | /**  | 
                                                        
| 33 | 33 | * Formats a phone number and country for display.  | 
                                                        
| 34 | 34 | *  | 
                                                        
@@ -4,13 +4,13 @@  | 
                                                    ||
| 4 | 4 | |
| 5 | 5 | class LaravelPhoneFacade extends Facade  | 
                                                        
| 6 | 6 |  { | 
                                                        
| 7 | - /**  | 
                                                        |
| 8 | - * Get the registered name of the component.  | 
                                                        |
| 9 | - *  | 
                                                        |
| 10 | - * @return string  | 
                                                        |
| 11 | - */  | 
                                                        |
| 12 | - protected static function getFacadeAccessor()  | 
                                                        |
| 13 | -    { | 
                                                        |
| 14 | - return 'libphonenumber';  | 
                                                        |
| 15 | - }  | 
                                                        |
| 7 | + /**  | 
                                                        |
| 8 | + * Get the registered name of the component.  | 
                                                        |
| 9 | + *  | 
                                                        |
| 10 | + * @return string  | 
                                                        |
| 11 | + */  | 
                                                        |
| 12 | + protected static function getFacadeAccessor()  | 
                                                        |
| 13 | +	{ | 
                                                        |
| 14 | + return 'libphonenumber';  | 
                                                        |
| 15 | + }  | 
                                                        |
| 16 | 16 | }  | 
                                                        
@@ -10,246 +10,246 @@  | 
                                                    ||
| 10 | 10 | class PhoneValidator  | 
                                                        
| 11 | 11 |  { | 
                                                        
| 12 | 12 | |
| 13 | - /**  | 
                                                        |
| 14 | - * @var \libphonenumber\PhoneNumberUtil  | 
                                                        |
| 15 | - */  | 
                                                        |
| 16 | - protected $lib;  | 
                                                        |
| 17 | -  | 
                                                        |
| 18 | - /**  | 
                                                        |
| 19 | - * Dotted validator data.  | 
                                                        |
| 20 | - *  | 
                                                        |
| 21 | - * @var array  | 
                                                        |
| 22 | - */  | 
                                                        |
| 23 | - protected $data;  | 
                                                        |
| 24 | -  | 
                                                        |
| 25 | - /**  | 
                                                        |
| 26 | - * Whether the country should be auto-detected.  | 
                                                        |
| 27 | - *  | 
                                                        |
| 28 | - * @var bool  | 
                                                        |
| 29 | - */  | 
                                                        |
| 30 | - protected $autodetect = false;  | 
                                                        |
| 31 | -  | 
                                                        |
| 32 | - /**  | 
                                                        |
| 33 | - * Whether to allow lenient checking of numbers (i.e. landline without area codes).  | 
                                                        |
| 34 | - *  | 
                                                        |
| 35 | - * @var bool  | 
                                                        |
| 36 | - */  | 
                                                        |
| 37 | - protected $lenient = false;  | 
                                                        |
| 38 | -  | 
                                                        |
| 39 | - /**  | 
                                                        |
| 40 | - * The field to use for country if not passed as a parameter.  | 
                                                        |
| 41 | - *  | 
                                                        |
| 42 | - * @var string|null  | 
                                                        |
| 43 | - */  | 
                                                        |
| 44 | - protected $countryField = null;  | 
                                                        |
| 45 | -  | 
                                                        |
| 46 | - /**  | 
                                                        |
| 47 | - * Countries to validate against.  | 
                                                        |
| 48 | - *  | 
                                                        |
| 49 | - * @var array  | 
                                                        |
| 50 | - */  | 
                                                        |
| 51 | - protected $countries = [];  | 
                                                        |
| 52 | -  | 
                                                        |
| 53 | - /**  | 
                                                        |
| 54 | - * Transformed phone number types to validate against.  | 
                                                        |
| 55 | - *  | 
                                                        |
| 56 | - * @var array  | 
                                                        |
| 57 | - */  | 
                                                        |
| 58 | - protected $types = [];  | 
                                                        |
| 59 | -  | 
                                                        |
| 60 | - /**  | 
                                                        |
| 61 | - * PhoneValidator constructor.  | 
                                                        |
| 62 | - */  | 
                                                        |
| 63 | - public function __construct(PhoneNumberUtil $lib)  | 
                                                        |
| 64 | -    { | 
                                                        |
| 65 | - $this->lib = $lib;  | 
                                                        |
| 66 | - }  | 
                                                        |
| 67 | -  | 
                                                        |
| 68 | - /**  | 
                                                        |
| 69 | - * Validates a phone number.  | 
                                                        |
| 70 | - *  | 
                                                        |
| 71 | - * @param string $attribute  | 
                                                        |
| 72 | - * @param mixed $value  | 
                                                        |
| 73 | - * @param array $parameters  | 
                                                        |
| 74 | - * @param object $validator  | 
                                                        |
| 75 | - * @return bool  | 
                                                        |
| 76 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException  | 
                                                        |
| 77 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException  | 
                                                        |
| 78 | - */  | 
                                                        |
| 79 | - public function validatePhone($attribute, $value, array $parameters, $validator)  | 
                                                        |
| 80 | -    { | 
                                                        |
| 81 | - $this->data = array_dot($validator->getData());  | 
                                                        |
| 82 | -  | 
                                                        |
| 83 | - $this->assignParameters($parameters);  | 
                                                        |
| 84 | -  | 
                                                        |
| 85 | - $this->checkCountries($attribute);  | 
                                                        |
| 86 | -  | 
                                                        |
| 87 | - // If autodetecting, let's first try without a country.  | 
                                                        |
| 88 | - // Otherwise use provided countries as default.  | 
                                                        |
| 89 | -        if ($this->autodetect || ($this->lenient && empty($this->countries))) { | 
                                                        |
| 90 | - array_unshift($this->countries, null);  | 
                                                        |
| 91 | - }  | 
                                                        |
| 92 | -  | 
                                                        |
| 93 | -        foreach ($this->countries as $country) { | 
                                                        |
| 94 | -            if ($this->isValidNumber($value, $country)) { | 
                                                        |
| 95 | - return true;  | 
                                                        |
| 96 | - }  | 
                                                        |
| 97 | - }  | 
                                                        |
| 98 | -  | 
                                                        |
| 99 | - // All specified country validations have failed.  | 
                                                        |
| 100 | - return false;  | 
                                                        |
| 101 | - }  | 
                                                        |
| 102 | -  | 
                                                        |
| 103 | - /**  | 
                                                        |
| 104 | - * Parses the supplied validator parameters.  | 
                                                        |
| 105 | - *  | 
                                                        |
| 106 | - * @param array $parameters  | 
                                                        |
| 107 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException  | 
                                                        |
| 108 | - */  | 
                                                        |
| 109 | - protected function assignParameters(array $parameters)  | 
                                                        |
| 110 | -    { | 
                                                        |
| 111 | - $types = [];  | 
                                                        |
| 112 | -  | 
                                                        |
| 113 | -        foreach ($parameters as $parameter) { | 
                                                        |
| 114 | - // First check if the parameter is some phone type configuration.  | 
                                                        |
| 115 | -            if ($this->isPhoneType($parameter)) { | 
                                                        |
| 116 | - $types[] = strtoupper($parameter);  | 
                                                        |
| 117 | -            } elseif ($this->isPhoneCountry($parameter)) { | 
                                                        |
| 118 | - $this->countries[] = strtoupper($parameter);  | 
                                                        |
| 119 | -            } elseif ($parameter == 'AUTO') { | 
                                                        |
| 120 | - $this->autodetect = true;  | 
                                                        |
| 121 | -            } elseif ($parameter == 'LENIENT') { | 
                                                        |
| 122 | - $this->lenient = true;  | 
                                                        |
| 123 | - } // Lastly check if it is an input field containing the country.  | 
                                                        |
| 124 | -            elseif ($this->isInputField($parameter)) { | 
                                                        |
| 125 | - $this->countryField = $parameter;  | 
                                                        |
| 126 | -            } else { | 
                                                        |
| 127 | - // Force developers to write proper code.  | 
                                                        |
| 128 | - throw new InvalidParameterException($parameter);  | 
                                                        |
| 129 | - }  | 
                                                        |
| 130 | - }  | 
                                                        |
| 131 | -  | 
                                                        |
| 132 | - $this->types = $this->parseTypes($types);  | 
                                                        |
| 133 | -  | 
                                                        |
| 134 | - }  | 
                                                        |
| 135 | -  | 
                                                        |
| 136 | - /**  | 
                                                        |
| 137 | - * Checks the detected countries. Overrides countries if a country field is present.  | 
                                                        |
| 138 | - * When using a country field, we should validate to false if country is empty so no exception  | 
                                                        |
| 139 | - * will be thrown.  | 
                                                        |
| 140 | - *  | 
                                                        |
| 141 | - * @param string $attribute  | 
                                                        |
| 142 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException  | 
                                                        |
| 143 | - */  | 
                                                        |
| 144 | - protected function checkCountries($attribute)  | 
                                                        |
| 145 | -    { | 
                                                        |
| 146 | - $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);  | 
                                                        |
| 147 | -  | 
                                                        |
| 148 | -        if ($this->isInputField($countryField)) { | 
                                                        |
| 149 | - $this->countries = [array_get($this->data, $countryField)];  | 
                                                        |
| 150 | -        } elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { | 
                                                        |
| 151 | - throw new NoValidCountryFoundException;  | 
                                                        |
| 152 | - }  | 
                                                        |
| 153 | - }  | 
                                                        |
| 154 | -  | 
                                                        |
| 155 | - /**  | 
                                                        |
| 156 | - * Performs the actual validation of the phone number.  | 
                                                        |
| 157 | - *  | 
                                                        |
| 158 | - * @param mixed $number  | 
                                                        |
| 159 | - * @param null $country  | 
                                                        |
| 160 | - * @return bool  | 
                                                        |
| 161 | - */  | 
                                                        |
| 162 | - protected function isValidNumber($number, $country = null)  | 
                                                        |
| 163 | -    { | 
                                                        |
| 164 | -        try { | 
                                                        |
| 165 | - // Throws NumberParseException if not parsed correctly against the supplied country.  | 
                                                        |
| 166 | - // If no country was given, tries to discover the country code from the number itself.  | 
                                                        |
| 167 | - $phoneNumber = $this->lib->parse($number, $country);  | 
                                                        |
| 168 | -  | 
                                                        |
| 169 | - // Check if type is allowed.  | 
                                                        |
| 170 | -            if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) { | 
                                                        |
| 171 | -  | 
                                                        |
| 172 | - // Lenient validation.  | 
                                                        |
| 173 | -                if ($this->lenient) { | 
                                                        |
| 174 | - return $this->lib->isPossibleNumber($phoneNumber, $country);  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | -  | 
                                                        |
| 177 | - // For automatic detection, the number should have a country code.  | 
                                                        |
| 178 | -                if ($phoneNumber->hasCountryCode()) { | 
                                                        |
| 179 | -  | 
                                                        |
| 180 | - // Automatic detection:  | 
                                                        |
| 181 | -                    if ($this->autodetect) { | 
                                                        |
| 182 | - // Validate if the international phone number is valid for its contained country.  | 
                                                        |
| 183 | - return $this->lib->isValidNumber($phoneNumber);  | 
                                                        |
| 184 | - }  | 
                                                        |
| 185 | -  | 
                                                        |
| 186 | - // Validate number against the specified country.  | 
                                                        |
| 187 | - return $this->lib->isValidNumberForRegion($phoneNumber, $country);  | 
                                                        |
| 188 | - }  | 
                                                        |
| 189 | - }  | 
                                                        |
| 190 | -  | 
                                                        |
| 191 | -        } catch (NumberParseException $e) { | 
                                                        |
| 192 | - // Proceed to default validation error.  | 
                                                        |
| 193 | - }  | 
                                                        |
| 194 | -  | 
                                                        |
| 195 | - return false;  | 
                                                        |
| 196 | - }  | 
                                                        |
| 197 | -  | 
                                                        |
| 198 | - /**  | 
                                                        |
| 199 | - * Parses the supplied phone number types.  | 
                                                        |
| 200 | - *  | 
                                                        |
| 201 | - * @param array $types  | 
                                                        |
| 202 | - * @return array  | 
                                                        |
| 203 | - */  | 
                                                        |
| 204 | - protected function parseTypes(array $types)  | 
                                                        |
| 205 | -    { | 
                                                        |
| 206 | - // Transform types to their namespaced class constant.  | 
                                                        |
| 207 | -        array_walk($types, function (&$type) { | 
                                                        |
| 208 | -            $type = constant('\libphonenumber\PhoneNumberType::' . $type); | 
                                                        |
| 209 | - });  | 
                                                        |
| 210 | -  | 
                                                        |
| 211 | - // Add in the unsure number type if applicable.  | 
                                                        |
| 212 | -        if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { | 
                                                        |
| 213 | - $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;  | 
                                                        |
| 214 | - }  | 
                                                        |
| 215 | -  | 
                                                        |
| 216 | - return $types;  | 
                                                        |
| 217 | - }  | 
                                                        |
| 218 | -  | 
                                                        |
| 219 | - /**  | 
                                                        |
| 220 | - * Checks if the given field is an actual input field.  | 
                                                        |
| 221 | - *  | 
                                                        |
| 222 | - * @param string $field  | 
                                                        |
| 223 | - * @return bool  | 
                                                        |
| 224 | - */  | 
                                                        |
| 225 | - public function isInputField($field)  | 
                                                        |
| 226 | -    { | 
                                                        |
| 227 | - return ! is_null(array_get($this->data, $field));  | 
                                                        |
| 228 | - }  | 
                                                        |
| 229 | -  | 
                                                        |
| 230 | - /**  | 
                                                        |
| 231 | - * Checks if the supplied string is a valid country code.  | 
                                                        |
| 232 | - *  | 
                                                        |
| 233 | - * @param string $country  | 
                                                        |
| 234 | - * @return bool  | 
                                                        |
| 235 | - */  | 
                                                        |
| 236 | - public function isPhoneCountry($country)  | 
                                                        |
| 237 | -    { | 
                                                        |
| 238 | - return ISO3166::isValid(strtoupper($country));  | 
                                                        |
| 239 | - }  | 
                                                        |
| 240 | -  | 
                                                        |
| 241 | - /**  | 
                                                        |
| 242 | - * Checks if the supplied string is a valid phone number type.  | 
                                                        |
| 243 | - *  | 
                                                        |
| 244 | - * @param string $type  | 
                                                        |
| 245 | - * @return bool  | 
                                                        |
| 246 | - */  | 
                                                        |
| 247 | - public function isPhoneType($type)  | 
                                                        |
| 248 | -    { | 
                                                        |
| 249 | - // Legacy support.  | 
                                                        |
| 250 | - $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);  | 
                                                        |
| 251 | -  | 
                                                        |
| 252 | -        return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); | 
                                                        |
| 253 | - }  | 
                                                        |
| 13 | + /**  | 
                                                        |
| 14 | + * @var \libphonenumber\PhoneNumberUtil  | 
                                                        |
| 15 | + */  | 
                                                        |
| 16 | + protected $lib;  | 
                                                        |
| 17 | +  | 
                                                        |
| 18 | + /**  | 
                                                        |
| 19 | + * Dotted validator data.  | 
                                                        |
| 20 | + *  | 
                                                        |
| 21 | + * @var array  | 
                                                        |
| 22 | + */  | 
                                                        |
| 23 | + protected $data;  | 
                                                        |
| 24 | +  | 
                                                        |
| 25 | + /**  | 
                                                        |
| 26 | + * Whether the country should be auto-detected.  | 
                                                        |
| 27 | + *  | 
                                                        |
| 28 | + * @var bool  | 
                                                        |
| 29 | + */  | 
                                                        |
| 30 | + protected $autodetect = false;  | 
                                                        |
| 31 | +  | 
                                                        |
| 32 | + /**  | 
                                                        |
| 33 | + * Whether to allow lenient checking of numbers (i.e. landline without area codes).  | 
                                                        |
| 34 | + *  | 
                                                        |
| 35 | + * @var bool  | 
                                                        |
| 36 | + */  | 
                                                        |
| 37 | + protected $lenient = false;  | 
                                                        |
| 38 | +  | 
                                                        |
| 39 | + /**  | 
                                                        |
| 40 | + * The field to use for country if not passed as a parameter.  | 
                                                        |
| 41 | + *  | 
                                                        |
| 42 | + * @var string|null  | 
                                                        |
| 43 | + */  | 
                                                        |
| 44 | + protected $countryField = null;  | 
                                                        |
| 45 | +  | 
                                                        |
| 46 | + /**  | 
                                                        |
| 47 | + * Countries to validate against.  | 
                                                        |
| 48 | + *  | 
                                                        |
| 49 | + * @var array  | 
                                                        |
| 50 | + */  | 
                                                        |
| 51 | + protected $countries = [];  | 
                                                        |
| 52 | +  | 
                                                        |
| 53 | + /**  | 
                                                        |
| 54 | + * Transformed phone number types to validate against.  | 
                                                        |
| 55 | + *  | 
                                                        |
| 56 | + * @var array  | 
                                                        |
| 57 | + */  | 
                                                        |
| 58 | + protected $types = [];  | 
                                                        |
| 59 | +  | 
                                                        |
| 60 | + /**  | 
                                                        |
| 61 | + * PhoneValidator constructor.  | 
                                                        |
| 62 | + */  | 
                                                        |
| 63 | + public function __construct(PhoneNumberUtil $lib)  | 
                                                        |
| 64 | +	{ | 
                                                        |
| 65 | + $this->lib = $lib;  | 
                                                        |
| 66 | + }  | 
                                                        |
| 67 | +  | 
                                                        |
| 68 | + /**  | 
                                                        |
| 69 | + * Validates a phone number.  | 
                                                        |
| 70 | + *  | 
                                                        |
| 71 | + * @param string $attribute  | 
                                                        |
| 72 | + * @param mixed $value  | 
                                                        |
| 73 | + * @param array $parameters  | 
                                                        |
| 74 | + * @param object $validator  | 
                                                        |
| 75 | + * @return bool  | 
                                                        |
| 76 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException  | 
                                                        |
| 77 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException  | 
                                                        |
| 78 | + */  | 
                                                        |
| 79 | + public function validatePhone($attribute, $value, array $parameters, $validator)  | 
                                                        |
| 80 | +	{ | 
                                                        |
| 81 | + $this->data = array_dot($validator->getData());  | 
                                                        |
| 82 | +  | 
                                                        |
| 83 | + $this->assignParameters($parameters);  | 
                                                        |
| 84 | +  | 
                                                        |
| 85 | + $this->checkCountries($attribute);  | 
                                                        |
| 86 | +  | 
                                                        |
| 87 | + // If autodetecting, let's first try without a country.  | 
                                                        |
| 88 | + // Otherwise use provided countries as default.  | 
                                                        |
| 89 | +		if ($this->autodetect || ($this->lenient && empty($this->countries))) { | 
                                                        |
| 90 | + array_unshift($this->countries, null);  | 
                                                        |
| 91 | + }  | 
                                                        |
| 92 | +  | 
                                                        |
| 93 | +		foreach ($this->countries as $country) { | 
                                                        |
| 94 | +			if ($this->isValidNumber($value, $country)) { | 
                                                        |
| 95 | + return true;  | 
                                                        |
| 96 | + }  | 
                                                        |
| 97 | + }  | 
                                                        |
| 98 | +  | 
                                                        |
| 99 | + // All specified country validations have failed.  | 
                                                        |
| 100 | + return false;  | 
                                                        |
| 101 | + }  | 
                                                        |
| 102 | +  | 
                                                        |
| 103 | + /**  | 
                                                        |
| 104 | + * Parses the supplied validator parameters.  | 
                                                        |
| 105 | + *  | 
                                                        |
| 106 | + * @param array $parameters  | 
                                                        |
| 107 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException  | 
                                                        |
| 108 | + */  | 
                                                        |
| 109 | + protected function assignParameters(array $parameters)  | 
                                                        |
| 110 | +	{ | 
                                                        |
| 111 | + $types = [];  | 
                                                        |
| 112 | +  | 
                                                        |
| 113 | +		foreach ($parameters as $parameter) { | 
                                                        |
| 114 | + // First check if the parameter is some phone type configuration.  | 
                                                        |
| 115 | +			if ($this->isPhoneType($parameter)) { | 
                                                        |
| 116 | + $types[] = strtoupper($parameter);  | 
                                                        |
| 117 | +			} elseif ($this->isPhoneCountry($parameter)) { | 
                                                        |
| 118 | + $this->countries[] = strtoupper($parameter);  | 
                                                        |
| 119 | +			} elseif ($parameter == 'AUTO') { | 
                                                        |
| 120 | + $this->autodetect = true;  | 
                                                        |
| 121 | +			} elseif ($parameter == 'LENIENT') { | 
                                                        |
| 122 | + $this->lenient = true;  | 
                                                        |
| 123 | + } // Lastly check if it is an input field containing the country.  | 
                                                        |
| 124 | +			elseif ($this->isInputField($parameter)) { | 
                                                        |
| 125 | + $this->countryField = $parameter;  | 
                                                        |
| 126 | +			} else { | 
                                                        |
| 127 | + // Force developers to write proper code.  | 
                                                        |
| 128 | + throw new InvalidParameterException($parameter);  | 
                                                        |
| 129 | + }  | 
                                                        |
| 130 | + }  | 
                                                        |
| 131 | +  | 
                                                        |
| 132 | + $this->types = $this->parseTypes($types);  | 
                                                        |
| 133 | +  | 
                                                        |
| 134 | + }  | 
                                                        |
| 135 | +  | 
                                                        |
| 136 | + /**  | 
                                                        |
| 137 | + * Checks the detected countries. Overrides countries if a country field is present.  | 
                                                        |
| 138 | + * When using a country field, we should validate to false if country is empty so no exception  | 
                                                        |
| 139 | + * will be thrown.  | 
                                                        |
| 140 | + *  | 
                                                        |
| 141 | + * @param string $attribute  | 
                                                        |
| 142 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException  | 
                                                        |
| 143 | + */  | 
                                                        |
| 144 | + protected function checkCountries($attribute)  | 
                                                        |
| 145 | +	{ | 
                                                        |
| 146 | + $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);  | 
                                                        |
| 147 | +  | 
                                                        |
| 148 | +		if ($this->isInputField($countryField)) { | 
                                                        |
| 149 | + $this->countries = [array_get($this->data, $countryField)];  | 
                                                        |
| 150 | +		} elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { | 
                                                        |
| 151 | + throw new NoValidCountryFoundException;  | 
                                                        |
| 152 | + }  | 
                                                        |
| 153 | + }  | 
                                                        |
| 154 | +  | 
                                                        |
| 155 | + /**  | 
                                                        |
| 156 | + * Performs the actual validation of the phone number.  | 
                                                        |
| 157 | + *  | 
                                                        |
| 158 | + * @param mixed $number  | 
                                                        |
| 159 | + * @param null $country  | 
                                                        |
| 160 | + * @return bool  | 
                                                        |
| 161 | + */  | 
                                                        |
| 162 | + protected function isValidNumber($number, $country = null)  | 
                                                        |
| 163 | +	{ | 
                                                        |
| 164 | +		try { | 
                                                        |
| 165 | + // Throws NumberParseException if not parsed correctly against the supplied country.  | 
                                                        |
| 166 | + // If no country was given, tries to discover the country code from the number itself.  | 
                                                        |
| 167 | + $phoneNumber = $this->lib->parse($number, $country);  | 
                                                        |
| 168 | +  | 
                                                        |
| 169 | + // Check if type is allowed.  | 
                                                        |
| 170 | +			if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) { | 
                                                        |
| 171 | +  | 
                                                        |
| 172 | + // Lenient validation.  | 
                                                        |
| 173 | +				if ($this->lenient) { | 
                                                        |
| 174 | + return $this->lib->isPossibleNumber($phoneNumber, $country);  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | +  | 
                                                        |
| 177 | + // For automatic detection, the number should have a country code.  | 
                                                        |
| 178 | +				if ($phoneNumber->hasCountryCode()) { | 
                                                        |
| 179 | +  | 
                                                        |
| 180 | + // Automatic detection:  | 
                                                        |
| 181 | +					if ($this->autodetect) { | 
                                                        |
| 182 | + // Validate if the international phone number is valid for its contained country.  | 
                                                        |
| 183 | + return $this->lib->isValidNumber($phoneNumber);  | 
                                                        |
| 184 | + }  | 
                                                        |
| 185 | +  | 
                                                        |
| 186 | + // Validate number against the specified country.  | 
                                                        |
| 187 | + return $this->lib->isValidNumberForRegion($phoneNumber, $country);  | 
                                                        |
| 188 | + }  | 
                                                        |
| 189 | + }  | 
                                                        |
| 190 | +  | 
                                                        |
| 191 | +		} catch (NumberParseException $e) { | 
                                                        |
| 192 | + // Proceed to default validation error.  | 
                                                        |
| 193 | + }  | 
                                                        |
| 194 | +  | 
                                                        |
| 195 | + return false;  | 
                                                        |
| 196 | + }  | 
                                                        |
| 197 | +  | 
                                                        |
| 198 | + /**  | 
                                                        |
| 199 | + * Parses the supplied phone number types.  | 
                                                        |
| 200 | + *  | 
                                                        |
| 201 | + * @param array $types  | 
                                                        |
| 202 | + * @return array  | 
                                                        |
| 203 | + */  | 
                                                        |
| 204 | + protected function parseTypes(array $types)  | 
                                                        |
| 205 | +	{ | 
                                                        |
| 206 | + // Transform types to their namespaced class constant.  | 
                                                        |
| 207 | +		array_walk($types, function (&$type) { | 
                                                        |
| 208 | +			$type = constant('\libphonenumber\PhoneNumberType::' . $type); | 
                                                        |
| 209 | + });  | 
                                                        |
| 210 | +  | 
                                                        |
| 211 | + // Add in the unsure number type if applicable.  | 
                                                        |
| 212 | +		if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { | 
                                                        |
| 213 | + $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;  | 
                                                        |
| 214 | + }  | 
                                                        |
| 215 | +  | 
                                                        |
| 216 | + return $types;  | 
                                                        |
| 217 | + }  | 
                                                        |
| 218 | +  | 
                                                        |
| 219 | + /**  | 
                                                        |
| 220 | + * Checks if the given field is an actual input field.  | 
                                                        |
| 221 | + *  | 
                                                        |
| 222 | + * @param string $field  | 
                                                        |
| 223 | + * @return bool  | 
                                                        |
| 224 | + */  | 
                                                        |
| 225 | + public function isInputField($field)  | 
                                                        |
| 226 | +	{ | 
                                                        |
| 227 | + return ! is_null(array_get($this->data, $field));  | 
                                                        |
| 228 | + }  | 
                                                        |
| 229 | +  | 
                                                        |
| 230 | + /**  | 
                                                        |
| 231 | + * Checks if the supplied string is a valid country code.  | 
                                                        |
| 232 | + *  | 
                                                        |
| 233 | + * @param string $country  | 
                                                        |
| 234 | + * @return bool  | 
                                                        |
| 235 | + */  | 
                                                        |
| 236 | + public function isPhoneCountry($country)  | 
                                                        |
| 237 | +	{ | 
                                                        |
| 238 | + return ISO3166::isValid(strtoupper($country));  | 
                                                        |
| 239 | + }  | 
                                                        |
| 240 | +  | 
                                                        |
| 241 | + /**  | 
                                                        |
| 242 | + * Checks if the supplied string is a valid phone number type.  | 
                                                        |
| 243 | + *  | 
                                                        |
| 244 | + * @param string $type  | 
                                                        |
| 245 | + * @return bool  | 
                                                        |
| 246 | + */  | 
                                                        |
| 247 | + public function isPhoneType($type)  | 
                                                        |
| 248 | +	{ | 
                                                        |
| 249 | + // Legacy support.  | 
                                                        |
| 250 | + $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);  | 
                                                        |
| 251 | +  | 
                                                        |
| 252 | +		return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); | 
                                                        |
| 253 | + }  | 
                                                        |
| 254 | 254 | |
| 255 | 255 | }  | 
                                                        
@@ -143,11 +143,11 @@ discard block  | 
                                                    ||
| 143 | 143 | */  | 
                                                        
| 144 | 144 | protected function checkCountries($attribute)  | 
                                                        
| 145 | 145 |      { | 
                                                        
| 146 | - $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);  | 
                                                        |
| 146 | + $countryField = (is_null($this->countryField) ? $attribute.'_country' : $this->countryField);  | 
                                                        |
| 147 | 147 | |
| 148 | 148 |          if ($this->isInputField($countryField)) { | 
                                                        
| 149 | 149 | $this->countries = [array_get($this->data, $countryField)];  | 
                                                        
| 150 | -        } elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { | 
                                                        |
| 150 | +        } elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) { | 
                                                        |
| 151 | 151 | throw new NoValidCountryFoundException;  | 
                                                        
| 152 | 152 | }  | 
                                                        
| 153 | 153 | }  | 
                                                        
@@ -204,8 +204,8 @@ discard block  | 
                                                    ||
| 204 | 204 | protected function parseTypes(array $types)  | 
                                                        
| 205 | 205 |      { | 
                                                        
| 206 | 206 | // Transform types to their namespaced class constant.  | 
                                                        
| 207 | -        array_walk($types, function (&$type) { | 
                                                        |
| 208 | -            $type = constant('\libphonenumber\PhoneNumberType::' . $type); | 
                                                        |
| 207 | +        array_walk($types, function(&$type) { | 
                                                        |
| 208 | +            $type = constant('\libphonenumber\PhoneNumberType::'.$type); | 
                                                        |
| 209 | 209 | });  | 
                                                        
| 210 | 210 | |
| 211 | 211 | // Add in the unsure number type if applicable.  | 
                                                        
@@ -224,7 +224,7 @@ discard block  | 
                                                    ||
| 224 | 224 | */  | 
                                                        
| 225 | 225 | public function isInputField($field)  | 
                                                        
| 226 | 226 |      { | 
                                                        
| 227 | - return ! is_null(array_get($this->data, $field));  | 
                                                        |
| 227 | + return !is_null(array_get($this->data, $field));  | 
                                                        |
| 228 | 228 | }  | 
                                                        
| 229 | 229 | |
| 230 | 230 | /**  | 
                                                        
@@ -249,7 +249,7 @@ discard block  | 
                                                    ||
| 249 | 249 | // Legacy support.  | 
                                                        
| 250 | 250 | $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);  | 
                                                        
| 251 | 251 | |
| 252 | -        return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); | 
                                                        |
| 252 | +        return defined('\libphonenumber\PhoneNumberType::'.strtoupper($type)); | 
                                                        |
| 253 | 253 | }  | 
                                                        
| 254 | 254 | |
| 255 | 255 | }  |