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