@@ -4,17 +4,17 @@ |
||
| 4 | 4 | |
| 5 | 5 | class NumberParseException extends libNumberParseException |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Country specification required static constructor. |
|
| 9 | - * |
|
| 10 | - * @param string $number |
|
| 11 | - * @return static |
|
| 12 | - */ |
|
| 13 | - public static function countryRequired($number) |
|
| 14 | - { |
|
| 15 | - return new static( |
|
| 16 | - libNumberParseException::INVALID_COUNTRY_CODE, |
|
| 17 | - 'Country specification for number "' . $number . '" required.' |
|
| 18 | - ); |
|
| 19 | - } |
|
| 7 | + /** |
|
| 8 | + * Country specification required static constructor. |
|
| 9 | + * |
|
| 10 | + * @param string $number |
|
| 11 | + * @return static |
|
| 12 | + */ |
|
| 13 | + public static function countryRequired($number) |
|
| 14 | + { |
|
| 15 | + return new static( |
|
| 16 | + libNumberParseException::INVALID_COUNTRY_CODE, |
|
| 17 | + 'Country specification for number "' . $number . '" required.' |
|
| 18 | + ); |
|
| 19 | + } |
|
| 20 | 20 | } |
| 21 | 21 | \ No newline at end of file |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | { |
| 15 | 15 | return new static( |
| 16 | 16 | libNumberParseException::INVALID_COUNTRY_CODE, |
| 17 | - 'Country specification for number "' . $number . '" required.' |
|
| 17 | + 'Country specification for number "'.$number.'" required.' |
|
| 18 | 18 | ); |
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | \ No newline at end of file |
@@ -2,14 +2,14 @@ |
||
| 2 | 2 | |
| 3 | 3 | class CountryCodeException extends \Exception |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Invalid country code static constructor. |
|
| 7 | - * |
|
| 8 | - * @param string $country |
|
| 9 | - * @return static |
|
| 10 | - */ |
|
| 11 | - public static function invalid($country) |
|
| 12 | - { |
|
| 13 | - return new static('Invalid country code "' . $country . '".'); |
|
| 14 | - } |
|
| 5 | + /** |
|
| 6 | + * Invalid country code static constructor. |
|
| 7 | + * |
|
| 8 | + * @param string $country |
|
| 9 | + * @return static |
|
| 10 | + */ |
|
| 11 | + public static function invalid($country) |
|
| 12 | + { |
|
| 13 | + return new static('Invalid country code "' . $country . '".'); |
|
| 14 | + } |
|
| 15 | 15 | } |
@@ -10,6 +10,6 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | public static function invalid($country) |
| 12 | 12 | { |
| 13 | - return new static('Invalid country code "' . $country . '".'); |
|
| 13 | + return new static('Invalid country code "'.$country.'".'); |
|
| 14 | 14 | } |
| 15 | 15 | } |
@@ -2,14 +2,14 @@ |
||
| 2 | 2 | |
| 3 | 3 | class NumberFormatException extends \Exception |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Invalid number format static constructor. |
|
| 7 | - * |
|
| 8 | - * @param string $format |
|
| 9 | - * @return static |
|
| 10 | - */ |
|
| 11 | - public static function invalid($format) |
|
| 12 | - { |
|
| 13 | - return new static('Invalid number format "' . $format . '".'); |
|
| 14 | - } |
|
| 5 | + /** |
|
| 6 | + * Invalid number format static constructor. |
|
| 7 | + * |
|
| 8 | + * @param string $format |
|
| 9 | + * @return static |
|
| 10 | + */ |
|
| 11 | + public static function invalid($format) |
|
| 12 | + { |
|
| 13 | + return new static('Invalid number format "' . $format . '".'); |
|
| 14 | + } |
|
| 15 | 15 | } |
| 16 | 16 | \ No newline at end of file |
@@ -10,6 +10,6 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | public static function invalid($format) |
| 12 | 12 | { |
| 13 | - return new static('Invalid number format "' . $format . '".'); |
|
| 13 | + return new static('Invalid number format "'.$format.'".'); |
|
| 14 | 14 | } |
| 15 | 15 | } |
| 16 | 16 | \ No newline at end of file |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | use Propaganistas\LaravelPhone\PhoneNumber; |
| 4 | 4 | |
| 5 | -if (! function_exists('phone')) { |
|
| 5 | +if (!function_exists('phone')) { |
|
| 6 | 6 | /** |
| 7 | 7 | * Get a PhoneNumber instance or a formatted string. |
| 8 | 8 | * |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | { |
| 16 | 16 | $phone = PhoneNumber::make($number, $country); |
| 17 | 17 | |
| 18 | - if (! is_null($format)) { |
|
| 18 | + if (!is_null($format)) { |
|
| 19 | 19 | return $phone->format($format); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -3,22 +3,22 @@ |
||
| 3 | 3 | use Propaganistas\LaravelPhone\PhoneNumber; |
| 4 | 4 | |
| 5 | 5 | if (! function_exists('phone')) { |
| 6 | - /** |
|
| 7 | - * Get a PhoneNumber instance or a formatted string. |
|
| 8 | - * |
|
| 9 | - * @param string $number |
|
| 10 | - * @param string|array $country |
|
| 11 | - * @param string $format |
|
| 12 | - * @return string|Propaganistas\LaravelPhone\PhoneNumber |
|
| 13 | - */ |
|
| 14 | - function phone($number, $country = [], $format = null) |
|
| 15 | - { |
|
| 16 | - $phone = PhoneNumber::make($number, $country); |
|
| 6 | + /** |
|
| 7 | + * Get a PhoneNumber instance or a formatted string. |
|
| 8 | + * |
|
| 9 | + * @param string $number |
|
| 10 | + * @param string|array $country |
|
| 11 | + * @param string $format |
|
| 12 | + * @return string|Propaganistas\LaravelPhone\PhoneNumber |
|
| 13 | + */ |
|
| 14 | + function phone($number, $country = [], $format = null) |
|
| 15 | + { |
|
| 16 | + $phone = PhoneNumber::make($number, $country); |
|
| 17 | 17 | |
| 18 | - if (! is_null($format)) { |
|
| 19 | - return $phone->format($format); |
|
| 20 | - } |
|
| 18 | + if (! is_null($format)) { |
|
| 19 | + return $phone->format($format); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - return $phone; |
|
| 23 | - } |
|
| 22 | + return $phone; |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -10,63 +10,63 @@ |
||
| 10 | 10 | |
| 11 | 11 | class PhoneServiceProvider extends ServiceProvider |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Bootstrap the application events. |
|
| 15 | - * |
|
| 16 | - * @return void |
|
| 17 | - */ |
|
| 18 | - public function boot() |
|
| 19 | - { |
|
| 20 | - $this->registerValidator(); |
|
| 13 | + /** |
|
| 14 | + * Bootstrap the application events. |
|
| 15 | + * |
|
| 16 | + * @return void |
|
| 17 | + */ |
|
| 18 | + public function boot() |
|
| 19 | + { |
|
| 20 | + $this->registerValidator(); |
|
| 21 | 21 | |
| 22 | - $this->registerRule(); |
|
| 23 | - } |
|
| 22 | + $this->registerRule(); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Register the service provider. |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function register() |
|
| 31 | - { |
|
| 32 | - $this->app->singleton('libphonenumber', function ($app) { |
|
| 33 | - return PhoneNumberUtil::getInstance(); |
|
| 34 | - }); |
|
| 25 | + /** |
|
| 26 | + * Register the service provider. |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function register() |
|
| 31 | + { |
|
| 32 | + $this->app->singleton('libphonenumber', function ($app) { |
|
| 33 | + return PhoneNumberUtil::getInstance(); |
|
| 34 | + }); |
|
| 35 | 35 | |
| 36 | - $this->app->alias('libphonenumber', PhoneNumberUtil::class); |
|
| 37 | - } |
|
| 36 | + $this->app->alias('libphonenumber', PhoneNumberUtil::class); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Register the "phone" validator. |
|
| 41 | - */ |
|
| 42 | - protected function registerValidator() |
|
| 43 | - { |
|
| 44 | - $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
| 39 | + /** |
|
| 40 | + * Register the "phone" validator. |
|
| 41 | + */ |
|
| 42 | + protected function registerValidator() |
|
| 43 | + { |
|
| 44 | + $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
| 45 | 45 | |
| 46 | - $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
| 47 | - } |
|
| 46 | + $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Register the "phone" rule macro. |
|
| 51 | - */ |
|
| 52 | - protected function registerRule() |
|
| 53 | - { |
|
| 54 | - if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
|
| 55 | - Rule::macro('phone', function () { |
|
| 56 | - return new Rules\Phone; |
|
| 57 | - }); |
|
| 58 | - } |
|
| 59 | - } |
|
| 49 | + /** |
|
| 50 | + * Register the "phone" rule macro. |
|
| 51 | + */ |
|
| 52 | + protected function registerRule() |
|
| 53 | + { |
|
| 54 | + if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
|
| 55 | + Rule::macro('phone', function () { |
|
| 56 | + return new Rules\Phone; |
|
| 57 | + }); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Determine whether we can register a dependent validator. |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - public static function canUseDependentValidation() |
|
| 67 | - { |
|
| 68 | - $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
| 61 | + /** |
|
| 62 | + * Determine whether we can register a dependent validator. |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + public static function canUseDependentValidation() |
|
| 67 | + { |
|
| 68 | + $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
| 69 | 69 | |
| 70 | - return $validator->hasMethod('extendDependent'); |
|
| 71 | - } |
|
| 70 | + return $validator->hasMethod('extendDependent'); |
|
| 71 | + } |
|
| 72 | 72 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public function register() |
| 31 | 31 | { |
| 32 | - $this->app->singleton('libphonenumber', function ($app) { |
|
| 32 | + $this->app->singleton('libphonenumber', function($app) { |
|
| 33 | 33 | return PhoneNumberUtil::getInstance(); |
| 34 | 34 | }); |
| 35 | 35 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | { |
| 44 | 44 | $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
| 45 | 45 | |
| 46 | - $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
| 46 | + $this->app['validator']->{$extend}('phone', Validation\Phone::class.'@validate'); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | protected function registerRule() |
| 53 | 53 | { |
| 54 | 54 | if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
| 55 | - Rule::macro('phone', function () { |
|
| 55 | + Rule::macro('phone', function() { |
|
| 56 | 56 | return new Rules\Phone; |
| 57 | 57 | }); |
| 58 | 58 | } |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | public function formatForCountry($country) |
| 170 | 170 | { |
| 171 | - if (! static::isValidCountryCode($country)) { |
|
| 171 | + if (!static::isValidCountryCode($country)) { |
|
| 172 | 172 | throw CountryCodeException::invalid($country); |
| 173 | 173 | } |
| 174 | 174 | |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
| 190 | 190 | { |
| 191 | - if (! static::isValidCountryCode($country)) { |
|
| 191 | + if (!static::isValidCountryCode($country)) { |
|
| 192 | 192 | throw CountryCodeException::invalid($country); |
| 193 | 193 | } |
| 194 | 194 | |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | */ |
| 207 | 207 | public function getCountry() |
| 208 | 208 | { |
| 209 | - if (! $this->country) { |
|
| 209 | + if (!$this->country) { |
|
| 210 | 210 | $this->country = $this->filterValidCountry($this->countries); |
| 211 | 211 | } |
| 212 | 212 | |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | protected function filterValidCountry($countries) |
| 224 | 224 | { |
| 225 | 225 | $result = Collection::make($countries) |
| 226 | - ->filter(function ($country) { |
|
| 226 | + ->filter(function($country) { |
|
| 227 | 227 | $instance = $this->lib->parse($this->number, $country); |
| 228 | 228 | |
| 229 | 229 | return $this->lenient |
@@ -18,369 +18,369 @@ |
||
| 18 | 18 | |
| 19 | 19 | class PhoneNumber implements Jsonable, JsonSerializable, Serializable |
| 20 | 20 | { |
| 21 | - use ParsesCountries, |
|
| 22 | - ParsesFormats, |
|
| 23 | - ParsesTypes; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The provided phone number. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $number; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The provided phone country. |
|
| 34 | - * |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - protected $countries = []; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The detected phone country. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $country; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 48 | - * |
|
| 49 | - * @var bool |
|
| 50 | - */ |
|
| 51 | - protected $lenient = false; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var \libphonenumber\PhoneNumberUtil |
|
| 55 | - */ |
|
| 56 | - protected $lib; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Phone constructor. |
|
| 60 | - * |
|
| 61 | - * @param string $number |
|
| 62 | - */ |
|
| 63 | - public function __construct($number) |
|
| 64 | - { |
|
| 65 | - $this->number = $number; |
|
| 66 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Create a phone instance. |
|
| 71 | - * |
|
| 72 | - * @param string $number |
|
| 73 | - * @param string|array $country |
|
| 74 | - * @return static |
|
| 75 | - */ |
|
| 76 | - public static function make($number, $country = []) |
|
| 77 | - { |
|
| 78 | - $instance = new static($number); |
|
| 79 | - |
|
| 80 | - return $instance->ofCountry($country); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Set the country to which the phone number belongs to. |
|
| 85 | - * |
|
| 86 | - * @param string|array $country |
|
| 87 | - * @return static |
|
| 88 | - */ |
|
| 89 | - public function ofCountry($country) |
|
| 90 | - { |
|
| 91 | - $countries = is_array($country) ? $country : func_get_args(); |
|
| 92 | - |
|
| 93 | - $instance = clone $this; |
|
| 94 | - $instance->countries = array_unique( |
|
| 95 | - array_merge($instance->countries, static::parseCountries($countries)) |
|
| 96 | - ); |
|
| 97 | - |
|
| 98 | - return $instance; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Format the phone number in international format. |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function formatInternational() |
|
| 107 | - { |
|
| 108 | - return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Format the phone number in national format. |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function formatNational() |
|
| 117 | - { |
|
| 118 | - return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Format the phone number in E164 format. |
|
| 123 | - * |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public function formatE164() |
|
| 127 | - { |
|
| 128 | - return $this->format(PhoneNumberFormat::E164); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Format the phone number in RFC3966 format. |
|
| 133 | - * |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function formatRFC3966() |
|
| 137 | - { |
|
| 138 | - return $this->format(PhoneNumberFormat::RFC3966); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Format the phone number in a given format. |
|
| 143 | - * |
|
| 144 | - * @param string $format |
|
| 145 | - * @return string |
|
| 146 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
| 147 | - */ |
|
| 148 | - public function format($format) |
|
| 149 | - { |
|
| 150 | - $parsedFormat = static::parseFormat($format); |
|
| 151 | - |
|
| 152 | - if (is_null($parsedFormat)) { |
|
| 153 | - throw NumberFormatException::invalid($format); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return $this->lib->format( |
|
| 157 | - $this->getPhoneNumberInstance(), |
|
| 158 | - $parsedFormat |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Format the phone number in a way that it can be dialled from the provided country. |
|
| 164 | - * |
|
| 165 | - * @param string $country |
|
| 166 | - * @return string |
|
| 167 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 168 | - */ |
|
| 169 | - public function formatForCountry($country) |
|
| 170 | - { |
|
| 171 | - if (! static::isValidCountryCode($country)) { |
|
| 172 | - throw CountryCodeException::invalid($country); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return $this->lib->formatOutOfCountryCallingNumber( |
|
| 176 | - $this->getPhoneNumberInstance(), |
|
| 177 | - $country |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 183 | - * |
|
| 184 | - * @param string $country |
|
| 185 | - * @param bool $removeFormatting |
|
| 186 | - * @return string |
|
| 187 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 188 | - */ |
|
| 189 | - public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 190 | - { |
|
| 191 | - if (! static::isValidCountryCode($country)) { |
|
| 192 | - throw CountryCodeException::invalid($country); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $this->lib->formatNumberForMobileDialing( |
|
| 196 | - $this->getPhoneNumberInstance(), |
|
| 197 | - $country, |
|
| 198 | - $removeFormatting |
|
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Get the phone number's country. |
|
| 204 | - * |
|
| 205 | - * @return string |
|
| 206 | - */ |
|
| 207 | - public function getCountry() |
|
| 208 | - { |
|
| 209 | - if (! $this->country) { |
|
| 210 | - $this->country = $this->filterValidCountry($this->countries); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $this->country; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Check if the phone number is of (a) given country(ies). |
|
| 218 | - * |
|
| 219 | - * @param string|array $country |
|
| 220 | - * @return bool |
|
| 221 | - */ |
|
| 222 | - public function isOfCountry($country) |
|
| 223 | - { |
|
| 224 | - $countries = static::parseCountries($country); |
|
| 225 | - |
|
| 226 | - return in_array($this->getCountry(), $countries); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Filter the provided countries to the one that is valid for the number. |
|
| 231 | - * |
|
| 232 | - * @param string|array $countries |
|
| 233 | - * @return string |
|
| 234 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
| 235 | - */ |
|
| 236 | - protected function filterValidCountry($countries) |
|
| 237 | - { |
|
| 238 | - $result = Collection::make($countries) |
|
| 239 | - ->filter(function ($country) { |
|
| 240 | - $instance = $this->lib->parse($this->number, $country); |
|
| 241 | - |
|
| 242 | - return $this->lenient |
|
| 243 | - ? $this->lib->isPossibleNumber($instance, $country) |
|
| 244 | - : $this->lib->isValidNumberForRegion($instance, $country); |
|
| 245 | - })->first(); |
|
| 246 | - |
|
| 247 | - // If we got a new result, return it. |
|
| 248 | - if ($result) { |
|
| 249 | - return $result; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - // Last resort: try to detect it from an international number. |
|
| 253 | - if ($this->numberLooksInternational()) { |
|
| 254 | - $instance = $this->lib->parse($this->number, null); |
|
| 255 | - |
|
| 256 | - if ($this->lib->isValidNumber($instance)) { |
|
| 257 | - return $this->lib->getRegionCodeForNumber($instance); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - throw NumberParseException::countryRequired($this->number); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Get the phone number's type. |
|
| 266 | - * |
|
| 267 | - * @param bool $asConstant |
|
| 268 | - * @return string|int|null |
|
| 269 | - */ |
|
| 270 | - public function getType($asConstant = false) |
|
| 271 | - { |
|
| 272 | - $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 273 | - |
|
| 274 | - if ($asConstant) { |
|
| 275 | - return $type; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
| 279 | - |
|
| 280 | - return $stringType ? strtolower($stringType) : null; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Check if the phone number is of (a) given type(s). |
|
| 285 | - * |
|
| 286 | - * @param string $type |
|
| 287 | - * @return bool |
|
| 288 | - */ |
|
| 289 | - public function isOfType($type) |
|
| 290 | - { |
|
| 291 | - $types = static::parseTypes($type); |
|
| 292 | - |
|
| 293 | - return in_array($this->getType(true), $types, true); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Get the PhoneNumber instance of the current number. |
|
| 298 | - * |
|
| 299 | - * @return \libphonenumber\PhoneNumber |
|
| 300 | - */ |
|
| 301 | - public function getPhoneNumberInstance() |
|
| 302 | - { |
|
| 303 | - return $this->lib->parse($this->number, $this->getCountry()); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Determine whether the phone number seems to be in international format. |
|
| 308 | - * |
|
| 309 | - * @return bool |
|
| 310 | - */ |
|
| 311 | - protected function numberLooksInternational() |
|
| 312 | - { |
|
| 313 | - return Str::startsWith($this->number, '+'); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Enable lenient number parsing. |
|
| 318 | - * |
|
| 319 | - * @return $this |
|
| 320 | - */ |
|
| 321 | - public function lenient() |
|
| 322 | - { |
|
| 323 | - $this->lenient = true; |
|
| 324 | - |
|
| 325 | - return $this; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Convert the phone instance to JSON. |
|
| 330 | - * |
|
| 331 | - * @param int $options |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - public function toJson($options = 0) |
|
| 335 | - { |
|
| 336 | - return json_encode($this->jsonSerialize(), $options); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Convert the phone instance into something JSON serializable. |
|
| 341 | - * |
|
| 342 | - * @return string |
|
| 343 | - */ |
|
| 344 | - public function jsonSerialize() |
|
| 345 | - { |
|
| 346 | - return $this->formatE164(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Convert the phone instance into a string representation. |
|
| 351 | - * |
|
| 352 | - * @return string |
|
| 353 | - */ |
|
| 354 | - public function serialize() |
|
| 355 | - { |
|
| 356 | - return $this->formatE164(); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Reconstructs the phone instance from a string representation. |
|
| 361 | - * |
|
| 362 | - * @param string $serialized |
|
| 363 | - */ |
|
| 364 | - public function unserialize($serialized) |
|
| 365 | - { |
|
| 366 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 367 | - $this->number = $serialized; |
|
| 368 | - $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Convert the phone instance to a formatted number. |
|
| 373 | - * |
|
| 374 | - * @return string |
|
| 375 | - */ |
|
| 376 | - public function __toString() |
|
| 377 | - { |
|
| 378 | - // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
| 379 | - // Let's just return the original number in that case. |
|
| 380 | - try { |
|
| 381 | - return $this->formatE164(); |
|
| 382 | - } catch (Exception $exception) { |
|
| 383 | - return $this->number; |
|
| 384 | - } |
|
| 385 | - } |
|
| 21 | + use ParsesCountries, |
|
| 22 | + ParsesFormats, |
|
| 23 | + ParsesTypes; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The provided phone number. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $number; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The provided phone country. |
|
| 34 | + * |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + protected $countries = []; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The detected phone country. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $country; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 48 | + * |
|
| 49 | + * @var bool |
|
| 50 | + */ |
|
| 51 | + protected $lenient = false; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var \libphonenumber\PhoneNumberUtil |
|
| 55 | + */ |
|
| 56 | + protected $lib; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Phone constructor. |
|
| 60 | + * |
|
| 61 | + * @param string $number |
|
| 62 | + */ |
|
| 63 | + public function __construct($number) |
|
| 64 | + { |
|
| 65 | + $this->number = $number; |
|
| 66 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Create a phone instance. |
|
| 71 | + * |
|
| 72 | + * @param string $number |
|
| 73 | + * @param string|array $country |
|
| 74 | + * @return static |
|
| 75 | + */ |
|
| 76 | + public static function make($number, $country = []) |
|
| 77 | + { |
|
| 78 | + $instance = new static($number); |
|
| 79 | + |
|
| 80 | + return $instance->ofCountry($country); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Set the country to which the phone number belongs to. |
|
| 85 | + * |
|
| 86 | + * @param string|array $country |
|
| 87 | + * @return static |
|
| 88 | + */ |
|
| 89 | + public function ofCountry($country) |
|
| 90 | + { |
|
| 91 | + $countries = is_array($country) ? $country : func_get_args(); |
|
| 92 | + |
|
| 93 | + $instance = clone $this; |
|
| 94 | + $instance->countries = array_unique( |
|
| 95 | + array_merge($instance->countries, static::parseCountries($countries)) |
|
| 96 | + ); |
|
| 97 | + |
|
| 98 | + return $instance; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Format the phone number in international format. |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function formatInternational() |
|
| 107 | + { |
|
| 108 | + return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Format the phone number in national format. |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function formatNational() |
|
| 117 | + { |
|
| 118 | + return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Format the phone number in E164 format. |
|
| 123 | + * |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public function formatE164() |
|
| 127 | + { |
|
| 128 | + return $this->format(PhoneNumberFormat::E164); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Format the phone number in RFC3966 format. |
|
| 133 | + * |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function formatRFC3966() |
|
| 137 | + { |
|
| 138 | + return $this->format(PhoneNumberFormat::RFC3966); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Format the phone number in a given format. |
|
| 143 | + * |
|
| 144 | + * @param string $format |
|
| 145 | + * @return string |
|
| 146 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
| 147 | + */ |
|
| 148 | + public function format($format) |
|
| 149 | + { |
|
| 150 | + $parsedFormat = static::parseFormat($format); |
|
| 151 | + |
|
| 152 | + if (is_null($parsedFormat)) { |
|
| 153 | + throw NumberFormatException::invalid($format); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return $this->lib->format( |
|
| 157 | + $this->getPhoneNumberInstance(), |
|
| 158 | + $parsedFormat |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Format the phone number in a way that it can be dialled from the provided country. |
|
| 164 | + * |
|
| 165 | + * @param string $country |
|
| 166 | + * @return string |
|
| 167 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 168 | + */ |
|
| 169 | + public function formatForCountry($country) |
|
| 170 | + { |
|
| 171 | + if (! static::isValidCountryCode($country)) { |
|
| 172 | + throw CountryCodeException::invalid($country); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return $this->lib->formatOutOfCountryCallingNumber( |
|
| 176 | + $this->getPhoneNumberInstance(), |
|
| 177 | + $country |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 183 | + * |
|
| 184 | + * @param string $country |
|
| 185 | + * @param bool $removeFormatting |
|
| 186 | + * @return string |
|
| 187 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 188 | + */ |
|
| 189 | + public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 190 | + { |
|
| 191 | + if (! static::isValidCountryCode($country)) { |
|
| 192 | + throw CountryCodeException::invalid($country); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $this->lib->formatNumberForMobileDialing( |
|
| 196 | + $this->getPhoneNumberInstance(), |
|
| 197 | + $country, |
|
| 198 | + $removeFormatting |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Get the phone number's country. |
|
| 204 | + * |
|
| 205 | + * @return string |
|
| 206 | + */ |
|
| 207 | + public function getCountry() |
|
| 208 | + { |
|
| 209 | + if (! $this->country) { |
|
| 210 | + $this->country = $this->filterValidCountry($this->countries); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $this->country; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Check if the phone number is of (a) given country(ies). |
|
| 218 | + * |
|
| 219 | + * @param string|array $country |
|
| 220 | + * @return bool |
|
| 221 | + */ |
|
| 222 | + public function isOfCountry($country) |
|
| 223 | + { |
|
| 224 | + $countries = static::parseCountries($country); |
|
| 225 | + |
|
| 226 | + return in_array($this->getCountry(), $countries); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Filter the provided countries to the one that is valid for the number. |
|
| 231 | + * |
|
| 232 | + * @param string|array $countries |
|
| 233 | + * @return string |
|
| 234 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
| 235 | + */ |
|
| 236 | + protected function filterValidCountry($countries) |
|
| 237 | + { |
|
| 238 | + $result = Collection::make($countries) |
|
| 239 | + ->filter(function ($country) { |
|
| 240 | + $instance = $this->lib->parse($this->number, $country); |
|
| 241 | + |
|
| 242 | + return $this->lenient |
|
| 243 | + ? $this->lib->isPossibleNumber($instance, $country) |
|
| 244 | + : $this->lib->isValidNumberForRegion($instance, $country); |
|
| 245 | + })->first(); |
|
| 246 | + |
|
| 247 | + // If we got a new result, return it. |
|
| 248 | + if ($result) { |
|
| 249 | + return $result; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + // Last resort: try to detect it from an international number. |
|
| 253 | + if ($this->numberLooksInternational()) { |
|
| 254 | + $instance = $this->lib->parse($this->number, null); |
|
| 255 | + |
|
| 256 | + if ($this->lib->isValidNumber($instance)) { |
|
| 257 | + return $this->lib->getRegionCodeForNumber($instance); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + throw NumberParseException::countryRequired($this->number); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Get the phone number's type. |
|
| 266 | + * |
|
| 267 | + * @param bool $asConstant |
|
| 268 | + * @return string|int|null |
|
| 269 | + */ |
|
| 270 | + public function getType($asConstant = false) |
|
| 271 | + { |
|
| 272 | + $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 273 | + |
|
| 274 | + if ($asConstant) { |
|
| 275 | + return $type; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
| 279 | + |
|
| 280 | + return $stringType ? strtolower($stringType) : null; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Check if the phone number is of (a) given type(s). |
|
| 285 | + * |
|
| 286 | + * @param string $type |
|
| 287 | + * @return bool |
|
| 288 | + */ |
|
| 289 | + public function isOfType($type) |
|
| 290 | + { |
|
| 291 | + $types = static::parseTypes($type); |
|
| 292 | + |
|
| 293 | + return in_array($this->getType(true), $types, true); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Get the PhoneNumber instance of the current number. |
|
| 298 | + * |
|
| 299 | + * @return \libphonenumber\PhoneNumber |
|
| 300 | + */ |
|
| 301 | + public function getPhoneNumberInstance() |
|
| 302 | + { |
|
| 303 | + return $this->lib->parse($this->number, $this->getCountry()); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Determine whether the phone number seems to be in international format. |
|
| 308 | + * |
|
| 309 | + * @return bool |
|
| 310 | + */ |
|
| 311 | + protected function numberLooksInternational() |
|
| 312 | + { |
|
| 313 | + return Str::startsWith($this->number, '+'); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Enable lenient number parsing. |
|
| 318 | + * |
|
| 319 | + * @return $this |
|
| 320 | + */ |
|
| 321 | + public function lenient() |
|
| 322 | + { |
|
| 323 | + $this->lenient = true; |
|
| 324 | + |
|
| 325 | + return $this; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Convert the phone instance to JSON. |
|
| 330 | + * |
|
| 331 | + * @param int $options |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + public function toJson($options = 0) |
|
| 335 | + { |
|
| 336 | + return json_encode($this->jsonSerialize(), $options); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Convert the phone instance into something JSON serializable. |
|
| 341 | + * |
|
| 342 | + * @return string |
|
| 343 | + */ |
|
| 344 | + public function jsonSerialize() |
|
| 345 | + { |
|
| 346 | + return $this->formatE164(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Convert the phone instance into a string representation. |
|
| 351 | + * |
|
| 352 | + * @return string |
|
| 353 | + */ |
|
| 354 | + public function serialize() |
|
| 355 | + { |
|
| 356 | + return $this->formatE164(); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Reconstructs the phone instance from a string representation. |
|
| 361 | + * |
|
| 362 | + * @param string $serialized |
|
| 363 | + */ |
|
| 364 | + public function unserialize($serialized) |
|
| 365 | + { |
|
| 366 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 367 | + $this->number = $serialized; |
|
| 368 | + $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Convert the phone instance to a formatted number. |
|
| 373 | + * |
|
| 374 | + * @return string |
|
| 375 | + */ |
|
| 376 | + public function __toString() |
|
| 377 | + { |
|
| 378 | + // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
| 379 | + // Let's just return the original number in that case. |
|
| 380 | + try { |
|
| 381 | + return $this->formatE164(); |
|
| 382 | + } catch (Exception $exception) { |
|
| 383 | + return $this->number; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | 386 | } |
| 387 | 387 | \ No newline at end of file |
@@ -5,31 +5,31 @@ |
||
| 5 | 5 | |
| 6 | 6 | trait ParsesCountries |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Determine whether the given country code is valid. |
|
| 10 | - * |
|
| 11 | - * @param string $country |
|
| 12 | - * @return bool |
|
| 13 | - */ |
|
| 14 | - public static function isValidCountryCode($country) |
|
| 15 | - { |
|
| 16 | - return ISO3166::isValid($country); |
|
| 17 | - } |
|
| 8 | + /** |
|
| 9 | + * Determine whether the given country code is valid. |
|
| 10 | + * |
|
| 11 | + * @param string $country |
|
| 12 | + * @return bool |
|
| 13 | + */ |
|
| 14 | + public static function isValidCountryCode($country) |
|
| 15 | + { |
|
| 16 | + return ISO3166::isValid($country); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Parse the provided phone countries to a valid array. |
|
| 21 | - * |
|
| 22 | - * @param string|array $countries |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - protected function parseCountries($countries) |
|
| 26 | - { |
|
| 27 | - return Collection::make(is_array($countries) ? $countries : func_get_args()) |
|
| 28 | - ->map(function ($country) { |
|
| 29 | - return strtoupper($country); |
|
| 30 | - }) |
|
| 31 | - ->filter(function ($value) { |
|
| 32 | - return static::isValidCountryCode($value); |
|
| 33 | - })->toArray(); |
|
| 34 | - } |
|
| 19 | + /** |
|
| 20 | + * Parse the provided phone countries to a valid array. |
|
| 21 | + * |
|
| 22 | + * @param string|array $countries |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + protected function parseCountries($countries) |
|
| 26 | + { |
|
| 27 | + return Collection::make(is_array($countries) ? $countries : func_get_args()) |
|
| 28 | + ->map(function ($country) { |
|
| 29 | + return strtoupper($country); |
|
| 30 | + }) |
|
| 31 | + ->filter(function ($value) { |
|
| 32 | + return static::isValidCountryCode($value); |
|
| 33 | + })->toArray(); |
|
| 34 | + } |
|
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -25,10 +25,10 @@ |
||
| 25 | 25 | protected function parseCountries($countries) |
| 26 | 26 | { |
| 27 | 27 | return Collection::make(is_array($countries) ? $countries : func_get_args()) |
| 28 | - ->map(function ($country) { |
|
| 28 | + ->map(function($country) { |
|
| 29 | 29 | return strtoupper($country); |
| 30 | 30 | }) |
| 31 | - ->filter(function ($value) { |
|
| 31 | + ->filter(function($value) { |
|
| 32 | 32 | return static::isValidCountryCode($value); |
| 33 | 33 | })->toArray(); |
| 34 | 34 | } |
@@ -7,74 +7,74 @@ |
||
| 7 | 7 | |
| 8 | 8 | trait ParsesTypes |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Array of available phone types. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected static $types; |
|
| 10 | + /** |
|
| 11 | + * Array of available phone types. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected static $types; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Determine whether the given type is valid. |
|
| 19 | - * |
|
| 20 | - * @param string $type |
|
| 21 | - * @return bool |
|
| 22 | - */ |
|
| 23 | - public static function isValidType($type) |
|
| 24 | - { |
|
| 25 | - return ! empty(static::parseTypes($type)); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Determine whether the given type is valid. |
|
| 19 | + * |
|
| 20 | + * @param string $type |
|
| 21 | + * @return bool |
|
| 22 | + */ |
|
| 23 | + public static function isValidType($type) |
|
| 24 | + { |
|
| 25 | + return ! empty(static::parseTypes($type)); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Parse a phone type into constant's value. |
|
| 30 | - * |
|
| 31 | - * @param string|array $types |
|
| 32 | - * @return array |
|
| 33 | - */ |
|
| 34 | - protected static function parseTypes($types) |
|
| 35 | - { |
|
| 36 | - static::loadTypes(); |
|
| 28 | + /** |
|
| 29 | + * Parse a phone type into constant's value. |
|
| 30 | + * |
|
| 31 | + * @param string|array $types |
|
| 32 | + * @return array |
|
| 33 | + */ |
|
| 34 | + protected static function parseTypes($types) |
|
| 35 | + { |
|
| 36 | + static::loadTypes(); |
|
| 37 | 37 | |
| 38 | - return Collection::make(is_array($types) ? $types : func_get_args()) |
|
| 39 | - ->map(function ($type) { |
|
| 40 | - // If the type equals a constant's value, just return it. |
|
| 41 | - if (in_array($type, static::$types, true)) { |
|
| 42 | - return $type; |
|
| 43 | - } |
|
| 38 | + return Collection::make(is_array($types) ? $types : func_get_args()) |
|
| 39 | + ->map(function ($type) { |
|
| 40 | + // If the type equals a constant's value, just return it. |
|
| 41 | + if (in_array($type, static::$types, true)) { |
|
| 42 | + return $type; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - // Otherwise we'll assume the type is the constant's name. |
|
| 46 | - return Arr::get(static::$types, strtoupper($type)); |
|
| 47 | - }) |
|
| 48 | - ->reject(function ($value) { |
|
| 49 | - return is_null($value) || $value === false; |
|
| 50 | - })->toArray(); |
|
| 51 | - } |
|
| 45 | + // Otherwise we'll assume the type is the constant's name. |
|
| 46 | + return Arr::get(static::$types, strtoupper($type)); |
|
| 47 | + }) |
|
| 48 | + ->reject(function ($value) { |
|
| 49 | + return is_null($value) || $value === false; |
|
| 50 | + })->toArray(); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Parse a phone type into its string representation. |
|
| 55 | - * |
|
| 56 | - * @param string|array $types |
|
| 57 | - * @return array |
|
| 58 | - */ |
|
| 59 | - protected static function parseTypesAsStrings($types) |
|
| 60 | - { |
|
| 61 | - static::loadTypes(); |
|
| 53 | + /** |
|
| 54 | + * Parse a phone type into its string representation. |
|
| 55 | + * |
|
| 56 | + * @param string|array $types |
|
| 57 | + * @return array |
|
| 58 | + */ |
|
| 59 | + protected static function parseTypesAsStrings($types) |
|
| 60 | + { |
|
| 61 | + static::loadTypes(); |
|
| 62 | 62 | |
| 63 | - return array_keys( |
|
| 64 | - array_intersect( |
|
| 65 | - static::$types, |
|
| 66 | - static::parseTypes($types) |
|
| 67 | - ) |
|
| 68 | - ); |
|
| 69 | - } |
|
| 63 | + return array_keys( |
|
| 64 | + array_intersect( |
|
| 65 | + static::$types, |
|
| 66 | + static::parseTypes($types) |
|
| 67 | + ) |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Load all available formats once. |
|
| 73 | - */ |
|
| 74 | - private static function loadTypes() |
|
| 75 | - { |
|
| 76 | - if (! static::$types) { |
|
| 77 | - static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
| 78 | - } |
|
| 79 | - } |
|
| 71 | + /** |
|
| 72 | + * Load all available formats once. |
|
| 73 | + */ |
|
| 74 | + private static function loadTypes() |
|
| 75 | + { |
|
| 76 | + if (! static::$types) { |
|
| 77 | + static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | } |
| 81 | 81 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | public static function isValidType($type) |
| 24 | 24 | { |
| 25 | - return ! empty(static::parseTypes($type)); |
|
| 25 | + return !empty(static::parseTypes($type)); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | static::loadTypes(); |
| 37 | 37 | |
| 38 | 38 | return Collection::make(is_array($types) ? $types : func_get_args()) |
| 39 | - ->map(function ($type) { |
|
| 39 | + ->map(function($type) { |
|
| 40 | 40 | // If the type equals a constant's value, just return it. |
| 41 | 41 | if (in_array($type, static::$types, true)) { |
| 42 | 42 | return $type; |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | // Otherwise we'll assume the type is the constant's name. |
| 46 | 46 | return Arr::get(static::$types, strtoupper($type)); |
| 47 | 47 | }) |
| 48 | - ->reject(function ($value) { |
|
| 48 | + ->reject(function($value) { |
|
| 49 | 49 | return is_null($value) || $value === false; |
| 50 | 50 | })->toArray(); |
| 51 | 51 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private static function loadTypes() |
| 75 | 75 | { |
| 76 | - if (! static::$types) { |
|
| 76 | + if (!static::$types) { |
|
| 77 | 77 | static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -6,50 +6,50 @@ |
||
| 6 | 6 | |
| 7 | 7 | trait ParsesFormats |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Array of available phone formats. |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected static $formats; |
|
| 9 | + /** |
|
| 10 | + * Array of available phone formats. |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected static $formats; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Determine whether the given format is valid. |
|
| 18 | - * |
|
| 19 | - * @param string $format |
|
| 20 | - * @return bool |
|
| 21 | - */ |
|
| 22 | - public static function isValidFormat($format) |
|
| 23 | - { |
|
| 24 | - return ! is_null(static::parseFormat($format)); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Determine whether the given format is valid. |
|
| 18 | + * |
|
| 19 | + * @param string $format |
|
| 20 | + * @return bool |
|
| 21 | + */ |
|
| 22 | + public static function isValidFormat($format) |
|
| 23 | + { |
|
| 24 | + return ! is_null(static::parseFormat($format)); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Parse a phone format. |
|
| 29 | - * |
|
| 30 | - * @param string $format |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - protected static function parseFormat($format) |
|
| 34 | - { |
|
| 35 | - static::loadFormats(); |
|
| 27 | + /** |
|
| 28 | + * Parse a phone format. |
|
| 29 | + * |
|
| 30 | + * @param string $format |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + protected static function parseFormat($format) |
|
| 34 | + { |
|
| 35 | + static::loadFormats(); |
|
| 36 | 36 | |
| 37 | - // If the format equals a constant's value, just return it. |
|
| 38 | - if (in_array($format, static::$formats, true)) { |
|
| 39 | - return $format; |
|
| 40 | - } |
|
| 37 | + // If the format equals a constant's value, just return it. |
|
| 38 | + if (in_array($format, static::$formats, true)) { |
|
| 39 | + return $format; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - // Otherwise we'll assume the format is the constant's name. |
|
| 43 | - return Arr::get(static::$formats, strtoupper($format)); |
|
| 44 | - } |
|
| 42 | + // Otherwise we'll assume the format is the constant's name. |
|
| 43 | + return Arr::get(static::$formats, strtoupper($format)); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Load all available formats once. |
|
| 48 | - */ |
|
| 49 | - private static function loadFormats() |
|
| 50 | - { |
|
| 51 | - if (! static::$formats) { |
|
| 52 | - static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
|
| 53 | - } |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * Load all available formats once. |
|
| 48 | + */ |
|
| 49 | + private static function loadFormats() |
|
| 50 | + { |
|
| 51 | + if (! static::$formats) { |
|
| 52 | + static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | } |
| 56 | 56 | \ No newline at end of file |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public static function isValidFormat($format) |
| 23 | 23 | { |
| 24 | - return ! is_null(static::parseFormat($format)); |
|
| 24 | + return !is_null(static::parseFormat($format)); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | private static function loadFormats() |
| 50 | 50 | { |
| 51 | - if (! static::$formats) { |
|
| 51 | + if (!static::$formats) { |
|
| 52 | 52 | static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
| 53 | 53 | } |
| 54 | 54 | } |