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