@@ -11,122 +11,122 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Phone |
| 13 | 13 | { |
| 14 | - use ParsesCountries, |
|
| 15 | - ParsesTypes; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var \libphonenumber\PhoneNumberUtil |
|
| 19 | - */ |
|
| 20 | - protected $lib; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Phone constructor. |
|
| 24 | - */ |
|
| 25 | - public function __construct() |
|
| 26 | - { |
|
| 27 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Validates a phone number. |
|
| 32 | - * |
|
| 33 | - * @param string $attribute |
|
| 34 | - * @param mixed $value |
|
| 35 | - * @param array $parameters |
|
| 36 | - * @param object $validator |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - public function validate($attribute, $value, array $parameters, $validator) |
|
| 40 | - { |
|
| 41 | - $data = $validator->getData(); |
|
| 42 | - |
|
| 43 | - list( |
|
| 44 | - $countries, |
|
| 45 | - $types, |
|
| 46 | - $detect, |
|
| 47 | - $lenient) = $this->extractParameters($attribute, $parameters, $data); |
|
| 48 | - |
|
| 49 | - // A "null" country is prepended: |
|
| 50 | - // 1. In case of auto-detection to have the validation run first without supplying a country. |
|
| 51 | - // 2. In case of lenient validation without provided countries; we still might have some luck... |
|
| 52 | - if ($detect || ($lenient && empty($countries))) { |
|
| 53 | - array_unshift($countries, null); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - foreach ($countries as $country) { |
|
| 57 | - try { |
|
| 58 | - // Parsing the phone number also validates the country, so no need to do this explicitly. |
|
| 59 | - // It'll throw a PhoneCountryException upon failure. |
|
| 60 | - $phoneNumber = PhoneNumber::make($value, $country); |
|
| 61 | - |
|
| 62 | - // Type validation. |
|
| 63 | - if (! empty($types) && ! $phoneNumber->isOfType($types)) { |
|
| 64 | - continue; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); |
|
| 68 | - |
|
| 69 | - // Lenient validation. |
|
| 70 | - if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); |
|
| 75 | - |
|
| 76 | - // Country detection. |
|
| 77 | - if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { |
|
| 78 | - return true; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // Default number+country validation. |
|
| 82 | - if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { |
|
| 83 | - return true; |
|
| 84 | - } |
|
| 85 | - } catch (NumberParseException $e) { |
|
| 86 | - continue; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Parse and extract parameters in the appropriate validation arguments. |
|
| 95 | - * |
|
| 96 | - * @param string $attribute |
|
| 97 | - * @param array $parameters |
|
| 98 | - * @param array $data |
|
| 99 | - * @return array |
|
| 100 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
| 101 | - */ |
|
| 102 | - protected function extractParameters($attribute, array $parameters, array $data) |
|
| 103 | - { |
|
| 104 | - // Discover if an input field was provided. If not, guess the field's name. |
|
| 105 | - $inputField = Collection::make($parameters) |
|
| 106 | - ->intersect(array_keys(Arr::dot($data))) |
|
| 107 | - ->first() ?: "${attribute}_country"; |
|
| 108 | - |
|
| 109 | - // Attempt to retrieve the field's value. |
|
| 110 | - if ($inputCountry = Arr::get($data, $inputField)) { |
|
| 111 | - |
|
| 112 | - if (static::isValidType($inputField)) { |
|
| 113 | - throw InvalidParameterException::ambiguous($inputField); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // Invalid country field values should just validate to false. |
|
| 117 | - // This will also prevent parameter hijacking through the country field. |
|
| 118 | - if (static::isValidCountryCode($inputCountry)) { |
|
| 119 | - $parameters[] = $inputCountry; |
|
| 120 | - } |
|
| 121 | - } |
|
| 14 | + use ParsesCountries, |
|
| 15 | + ParsesTypes; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var \libphonenumber\PhoneNumberUtil |
|
| 19 | + */ |
|
| 20 | + protected $lib; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Phone constructor. |
|
| 24 | + */ |
|
| 25 | + public function __construct() |
|
| 26 | + { |
|
| 27 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Validates a phone number. |
|
| 32 | + * |
|
| 33 | + * @param string $attribute |
|
| 34 | + * @param mixed $value |
|
| 35 | + * @param array $parameters |
|
| 36 | + * @param object $validator |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + public function validate($attribute, $value, array $parameters, $validator) |
|
| 40 | + { |
|
| 41 | + $data = $validator->getData(); |
|
| 42 | + |
|
| 43 | + list( |
|
| 44 | + $countries, |
|
| 45 | + $types, |
|
| 46 | + $detect, |
|
| 47 | + $lenient) = $this->extractParameters($attribute, $parameters, $data); |
|
| 48 | + |
|
| 49 | + // A "null" country is prepended: |
|
| 50 | + // 1. In case of auto-detection to have the validation run first without supplying a country. |
|
| 51 | + // 2. In case of lenient validation without provided countries; we still might have some luck... |
|
| 52 | + if ($detect || ($lenient && empty($countries))) { |
|
| 53 | + array_unshift($countries, null); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + foreach ($countries as $country) { |
|
| 57 | + try { |
|
| 58 | + // Parsing the phone number also validates the country, so no need to do this explicitly. |
|
| 59 | + // It'll throw a PhoneCountryException upon failure. |
|
| 60 | + $phoneNumber = PhoneNumber::make($value, $country); |
|
| 61 | + |
|
| 62 | + // Type validation. |
|
| 63 | + if (! empty($types) && ! $phoneNumber->isOfType($types)) { |
|
| 64 | + continue; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); |
|
| 68 | + |
|
| 69 | + // Lenient validation. |
|
| 70 | + if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); |
|
| 75 | + |
|
| 76 | + // Country detection. |
|
| 77 | + if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { |
|
| 78 | + return true; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // Default number+country validation. |
|
| 82 | + if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { |
|
| 83 | + return true; |
|
| 84 | + } |
|
| 85 | + } catch (NumberParseException $e) { |
|
| 86 | + continue; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Parse and extract parameters in the appropriate validation arguments. |
|
| 95 | + * |
|
| 96 | + * @param string $attribute |
|
| 97 | + * @param array $parameters |
|
| 98 | + * @param array $data |
|
| 99 | + * @return array |
|
| 100 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
| 101 | + */ |
|
| 102 | + protected function extractParameters($attribute, array $parameters, array $data) |
|
| 103 | + { |
|
| 104 | + // Discover if an input field was provided. If not, guess the field's name. |
|
| 105 | + $inputField = Collection::make($parameters) |
|
| 106 | + ->intersect(array_keys(Arr::dot($data))) |
|
| 107 | + ->first() ?: "${attribute}_country"; |
|
| 108 | + |
|
| 109 | + // Attempt to retrieve the field's value. |
|
| 110 | + if ($inputCountry = Arr::get($data, $inputField)) { |
|
| 111 | + |
|
| 112 | + if (static::isValidType($inputField)) { |
|
| 113 | + throw InvalidParameterException::ambiguous($inputField); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // Invalid country field values should just validate to false. |
|
| 117 | + // This will also prevent parameter hijacking through the country field. |
|
| 118 | + if (static::isValidCountryCode($inputCountry)) { |
|
| 119 | + $parameters[] = $inputCountry; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - $parameters = array_map('strtolower', $parameters); |
|
| 124 | - |
|
| 125 | - return [ |
|
| 126 | - static::parseCountries($parameters), |
|
| 127 | - static::parseTypes($parameters), |
|
| 128 | - in_array('auto', $parameters), |
|
| 129 | - in_array('lenient', $parameters) |
|
| 130 | - ]; |
|
| 131 | - } |
|
| 123 | + $parameters = array_map('strtolower', $parameters); |
|
| 124 | + |
|
| 125 | + return [ |
|
| 126 | + static::parseCountries($parameters), |
|
| 127 | + static::parseTypes($parameters), |
|
| 128 | + in_array('auto', $parameters), |
|
| 129 | + in_array('lenient', $parameters) |
|
| 130 | + ]; |
|
| 131 | + } |
|
| 132 | 132 | } |
@@ -21,387 +21,387 @@ |
||
| 21 | 21 | |
| 22 | 22 | class PhoneNumber implements Jsonable, JsonSerializable, Serializable |
| 23 | 23 | { |
| 24 | - use Macroable, |
|
| 25 | - ParsesCountries, |
|
| 26 | - ParsesFormats, |
|
| 27 | - ParsesTypes; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The provided phone number. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $number; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The provided phone country. |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $countries = []; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The detected phone country. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $country; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 52 | - * |
|
| 53 | - * @var bool |
|
| 54 | - */ |
|
| 55 | - protected $lenient = false; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var \libphonenumber\PhoneNumberUtil |
|
| 59 | - */ |
|
| 60 | - protected $lib; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Phone constructor. |
|
| 64 | - * |
|
| 65 | - * @param string $number |
|
| 66 | - */ |
|
| 67 | - public function __construct($number) |
|
| 68 | - { |
|
| 69 | - $this->number = $number; |
|
| 70 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Create a phone instance. |
|
| 75 | - * |
|
| 76 | - * @param string $number |
|
| 77 | - * @param string|array $country |
|
| 78 | - * @return static |
|
| 79 | - */ |
|
| 80 | - public static function make($number, $country = []) |
|
| 81 | - { |
|
| 82 | - $instance = new static($number); |
|
| 83 | - |
|
| 84 | - return $instance->ofCountry($country); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Set the country to which the phone number belongs to. |
|
| 89 | - * |
|
| 90 | - * @param string|array $country |
|
| 91 | - * @return static |
|
| 92 | - */ |
|
| 93 | - public function ofCountry($country) |
|
| 94 | - { |
|
| 95 | - $countries = is_array($country) ? $country : func_get_args(); |
|
| 96 | - |
|
| 97 | - $instance = clone $this; |
|
| 98 | - $instance->countries = array_unique( |
|
| 99 | - array_merge($instance->countries, static::parseCountries($countries)) |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - return $instance; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Format the phone number in international format. |
|
| 107 | - * |
|
| 108 | - * @return string |
|
| 109 | - */ |
|
| 110 | - public function formatInternational() |
|
| 111 | - { |
|
| 112 | - return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Format the phone number in national format. |
|
| 117 | - * |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function formatNational() |
|
| 121 | - { |
|
| 122 | - return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Format the phone number in E164 format. |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function formatE164() |
|
| 131 | - { |
|
| 132 | - return $this->format(PhoneNumberFormat::E164); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Format the phone number in RFC3966 format. |
|
| 137 | - * |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public function formatRFC3966() |
|
| 141 | - { |
|
| 142 | - return $this->format(PhoneNumberFormat::RFC3966); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Format the phone number in a given format. |
|
| 147 | - * |
|
| 148 | - * @param string $format |
|
| 149 | - * @return string |
|
| 150 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
| 151 | - */ |
|
| 152 | - public function format($format) |
|
| 153 | - { |
|
| 154 | - $parsedFormat = static::parseFormat($format); |
|
| 155 | - |
|
| 156 | - if (is_null($parsedFormat)) { |
|
| 157 | - throw NumberFormatException::invalid($format); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $this->lib->format( |
|
| 161 | - $this->getPhoneNumberInstance(), |
|
| 162 | - $parsedFormat |
|
| 163 | - ); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Format the phone number in a way that it can be dialled from the provided country. |
|
| 168 | - * |
|
| 169 | - * @param string $country |
|
| 170 | - * @return string |
|
| 171 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 172 | - */ |
|
| 173 | - public function formatForCountry($country) |
|
| 174 | - { |
|
| 175 | - if (! static::isValidCountryCode($country)) { |
|
| 176 | - throw CountryCodeException::invalid($country); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $this->lib->formatOutOfCountryCallingNumber( |
|
| 180 | - $this->getPhoneNumberInstance(), |
|
| 181 | - $country |
|
| 182 | - ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 187 | - * |
|
| 188 | - * @param string $country |
|
| 189 | - * @param bool $removeFormatting |
|
| 190 | - * @return string |
|
| 191 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 192 | - */ |
|
| 193 | - public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 194 | - { |
|
| 195 | - if (! static::isValidCountryCode($country)) { |
|
| 196 | - throw CountryCodeException::invalid($country); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $this->lib->formatNumberForMobileDialing( |
|
| 200 | - $this->getPhoneNumberInstance(), |
|
| 201 | - $country, |
|
| 202 | - $removeFormatting |
|
| 203 | - ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Get the phone number's country. |
|
| 208 | - * |
|
| 209 | - * @return string |
|
| 210 | - */ |
|
| 211 | - public function getCountry() |
|
| 212 | - { |
|
| 213 | - if (! $this->country) { |
|
| 214 | - $this->country = $this->filterValidCountry($this->countries); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - return $this->country; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Check if the phone number is of (a) given country(ies). |
|
| 222 | - * |
|
| 223 | - * @param string|array $country |
|
| 224 | - * @return bool |
|
| 225 | - */ |
|
| 226 | - public function isOfCountry($country) |
|
| 227 | - { |
|
| 228 | - $countries = static::parseCountries($country); |
|
| 229 | - |
|
| 230 | - return in_array($this->getCountry(), $countries); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Filter the provided countries to the one that is valid for the number. |
|
| 235 | - * |
|
| 236 | - * @param string|array $countries |
|
| 237 | - * @return string |
|
| 238 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
| 239 | - */ |
|
| 240 | - protected function filterValidCountry($countries) |
|
| 241 | - { |
|
| 242 | - $result = Collection::make($countries) |
|
| 243 | - ->filter(function ($country) { |
|
| 244 | - try { |
|
| 245 | - $instance = $this->lib->parse($this->number, $country); |
|
| 246 | - |
|
| 247 | - return $this->lenient |
|
| 248 | - ? $this->lib->isPossibleNumber($instance, $country) |
|
| 249 | - : $this->lib->isValidNumberForRegion($instance, $country); |
|
| 250 | - } catch (libNumberParseException $e) { |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - })->first(); |
|
| 254 | - |
|
| 255 | - // If we got a new result, return it. |
|
| 256 | - if ($result) { |
|
| 257 | - return $result; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - // Last resort: try to detect it from an international number. |
|
| 261 | - if ($this->numberLooksInternational()) { |
|
| 262 | - $countries[] = null; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - foreach ($countries as $country) { |
|
| 266 | - $instance = $this->lib->parse($this->number, $country); |
|
| 267 | - |
|
| 268 | - if ($this->lib->isValidNumber($instance)) { |
|
| 269 | - return $this->lib->getRegionCodeForNumber($instance); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - if ($countries = array_filter($countries)) { |
|
| 274 | - throw NumberParseException::countryMismatch($this->number, $countries); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - throw NumberParseException::countryRequired($this->number); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Get the phone number's type. |
|
| 282 | - * |
|
| 283 | - * @param bool $asConstant |
|
| 284 | - * @return string|int|null |
|
| 285 | - */ |
|
| 286 | - public function getType($asConstant = false) |
|
| 287 | - { |
|
| 288 | - $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 289 | - |
|
| 290 | - if ($asConstant) { |
|
| 291 | - return $type; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
| 295 | - |
|
| 296 | - return $stringType ? strtolower($stringType) : null; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Check if the phone number is of (a) given type(s). |
|
| 301 | - * |
|
| 302 | - * @param string $type |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - public function isOfType($type) |
|
| 306 | - { |
|
| 307 | - $types = static::parseTypes($type); |
|
| 308 | - |
|
| 309 | - // Add the unsure type when applicable. |
|
| 310 | - if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
| 311 | - $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - return in_array($this->getType(true), $types, true); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Get the PhoneNumber instance of the current number. |
|
| 319 | - * |
|
| 320 | - * @return \libphonenumber\PhoneNumber |
|
| 321 | - */ |
|
| 322 | - public function getPhoneNumberInstance() |
|
| 323 | - { |
|
| 324 | - return $this->lib->parse($this->number, $this->getCountry()); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Determine whether the phone number seems to be in international format. |
|
| 329 | - * |
|
| 330 | - * @return bool |
|
| 331 | - */ |
|
| 332 | - protected function numberLooksInternational() |
|
| 333 | - { |
|
| 334 | - return Str::startsWith($this->number, '+'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Enable lenient number parsing. |
|
| 339 | - * |
|
| 340 | - * @return $this |
|
| 341 | - */ |
|
| 342 | - public function lenient() |
|
| 343 | - { |
|
| 344 | - $this->lenient = true; |
|
| 345 | - |
|
| 346 | - return $this; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Convert the phone instance to JSON. |
|
| 351 | - * |
|
| 352 | - * @param int $options |
|
| 353 | - * @return string |
|
| 354 | - */ |
|
| 355 | - public function toJson($options = 0) |
|
| 356 | - { |
|
| 357 | - return json_encode($this->jsonSerialize(), $options); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Convert the phone instance into something JSON serializable. |
|
| 362 | - * |
|
| 363 | - * @return string |
|
| 364 | - */ |
|
| 365 | - public function jsonSerialize() |
|
| 366 | - { |
|
| 367 | - return $this->formatE164(); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Convert the phone instance into a string representation. |
|
| 372 | - * |
|
| 373 | - * @return string |
|
| 374 | - */ |
|
| 375 | - public function serialize() |
|
| 376 | - { |
|
| 377 | - return $this->formatE164(); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Reconstructs the phone instance from a string representation. |
|
| 382 | - * |
|
| 383 | - * @param string $serialized |
|
| 384 | - */ |
|
| 385 | - public function unserialize($serialized) |
|
| 386 | - { |
|
| 387 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 388 | - $this->number = $serialized; |
|
| 389 | - $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Convert the phone instance to a formatted number. |
|
| 394 | - * |
|
| 395 | - * @return string |
|
| 396 | - */ |
|
| 397 | - public function __toString() |
|
| 398 | - { |
|
| 399 | - // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
| 400 | - // Let's just return the original number in that case. |
|
| 401 | - try { |
|
| 402 | - return $this->formatE164(); |
|
| 403 | - } catch (Exception $exception) { |
|
| 404 | - return (string) $this->number; |
|
| 405 | - } |
|
| 406 | - } |
|
| 24 | + use Macroable, |
|
| 25 | + ParsesCountries, |
|
| 26 | + ParsesFormats, |
|
| 27 | + ParsesTypes; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The provided phone number. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $number; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The provided phone country. |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $countries = []; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The detected phone country. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $country; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 52 | + * |
|
| 53 | + * @var bool |
|
| 54 | + */ |
|
| 55 | + protected $lenient = false; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var \libphonenumber\PhoneNumberUtil |
|
| 59 | + */ |
|
| 60 | + protected $lib; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Phone constructor. |
|
| 64 | + * |
|
| 65 | + * @param string $number |
|
| 66 | + */ |
|
| 67 | + public function __construct($number) |
|
| 68 | + { |
|
| 69 | + $this->number = $number; |
|
| 70 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Create a phone instance. |
|
| 75 | + * |
|
| 76 | + * @param string $number |
|
| 77 | + * @param string|array $country |
|
| 78 | + * @return static |
|
| 79 | + */ |
|
| 80 | + public static function make($number, $country = []) |
|
| 81 | + { |
|
| 82 | + $instance = new static($number); |
|
| 83 | + |
|
| 84 | + return $instance->ofCountry($country); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Set the country to which the phone number belongs to. |
|
| 89 | + * |
|
| 90 | + * @param string|array $country |
|
| 91 | + * @return static |
|
| 92 | + */ |
|
| 93 | + public function ofCountry($country) |
|
| 94 | + { |
|
| 95 | + $countries = is_array($country) ? $country : func_get_args(); |
|
| 96 | + |
|
| 97 | + $instance = clone $this; |
|
| 98 | + $instance->countries = array_unique( |
|
| 99 | + array_merge($instance->countries, static::parseCountries($countries)) |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + return $instance; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Format the phone number in international format. |
|
| 107 | + * |
|
| 108 | + * @return string |
|
| 109 | + */ |
|
| 110 | + public function formatInternational() |
|
| 111 | + { |
|
| 112 | + return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Format the phone number in national format. |
|
| 117 | + * |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function formatNational() |
|
| 121 | + { |
|
| 122 | + return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Format the phone number in E164 format. |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function formatE164() |
|
| 131 | + { |
|
| 132 | + return $this->format(PhoneNumberFormat::E164); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Format the phone number in RFC3966 format. |
|
| 137 | + * |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public function formatRFC3966() |
|
| 141 | + { |
|
| 142 | + return $this->format(PhoneNumberFormat::RFC3966); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Format the phone number in a given format. |
|
| 147 | + * |
|
| 148 | + * @param string $format |
|
| 149 | + * @return string |
|
| 150 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
| 151 | + */ |
|
| 152 | + public function format($format) |
|
| 153 | + { |
|
| 154 | + $parsedFormat = static::parseFormat($format); |
|
| 155 | + |
|
| 156 | + if (is_null($parsedFormat)) { |
|
| 157 | + throw NumberFormatException::invalid($format); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $this->lib->format( |
|
| 161 | + $this->getPhoneNumberInstance(), |
|
| 162 | + $parsedFormat |
|
| 163 | + ); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Format the phone number in a way that it can be dialled from the provided country. |
|
| 168 | + * |
|
| 169 | + * @param string $country |
|
| 170 | + * @return string |
|
| 171 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 172 | + */ |
|
| 173 | + public function formatForCountry($country) |
|
| 174 | + { |
|
| 175 | + if (! static::isValidCountryCode($country)) { |
|
| 176 | + throw CountryCodeException::invalid($country); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $this->lib->formatOutOfCountryCallingNumber( |
|
| 180 | + $this->getPhoneNumberInstance(), |
|
| 181 | + $country |
|
| 182 | + ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 187 | + * |
|
| 188 | + * @param string $country |
|
| 189 | + * @param bool $removeFormatting |
|
| 190 | + * @return string |
|
| 191 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
| 192 | + */ |
|
| 193 | + public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 194 | + { |
|
| 195 | + if (! static::isValidCountryCode($country)) { |
|
| 196 | + throw CountryCodeException::invalid($country); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $this->lib->formatNumberForMobileDialing( |
|
| 200 | + $this->getPhoneNumberInstance(), |
|
| 201 | + $country, |
|
| 202 | + $removeFormatting |
|
| 203 | + ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Get the phone number's country. |
|
| 208 | + * |
|
| 209 | + * @return string |
|
| 210 | + */ |
|
| 211 | + public function getCountry() |
|
| 212 | + { |
|
| 213 | + if (! $this->country) { |
|
| 214 | + $this->country = $this->filterValidCountry($this->countries); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + return $this->country; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Check if the phone number is of (a) given country(ies). |
|
| 222 | + * |
|
| 223 | + * @param string|array $country |
|
| 224 | + * @return bool |
|
| 225 | + */ |
|
| 226 | + public function isOfCountry($country) |
|
| 227 | + { |
|
| 228 | + $countries = static::parseCountries($country); |
|
| 229 | + |
|
| 230 | + return in_array($this->getCountry(), $countries); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Filter the provided countries to the one that is valid for the number. |
|
| 235 | + * |
|
| 236 | + * @param string|array $countries |
|
| 237 | + * @return string |
|
| 238 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
| 239 | + */ |
|
| 240 | + protected function filterValidCountry($countries) |
|
| 241 | + { |
|
| 242 | + $result = Collection::make($countries) |
|
| 243 | + ->filter(function ($country) { |
|
| 244 | + try { |
|
| 245 | + $instance = $this->lib->parse($this->number, $country); |
|
| 246 | + |
|
| 247 | + return $this->lenient |
|
| 248 | + ? $this->lib->isPossibleNumber($instance, $country) |
|
| 249 | + : $this->lib->isValidNumberForRegion($instance, $country); |
|
| 250 | + } catch (libNumberParseException $e) { |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + })->first(); |
|
| 254 | + |
|
| 255 | + // If we got a new result, return it. |
|
| 256 | + if ($result) { |
|
| 257 | + return $result; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + // Last resort: try to detect it from an international number. |
|
| 261 | + if ($this->numberLooksInternational()) { |
|
| 262 | + $countries[] = null; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + foreach ($countries as $country) { |
|
| 266 | + $instance = $this->lib->parse($this->number, $country); |
|
| 267 | + |
|
| 268 | + if ($this->lib->isValidNumber($instance)) { |
|
| 269 | + return $this->lib->getRegionCodeForNumber($instance); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + if ($countries = array_filter($countries)) { |
|
| 274 | + throw NumberParseException::countryMismatch($this->number, $countries); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + throw NumberParseException::countryRequired($this->number); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Get the phone number's type. |
|
| 282 | + * |
|
| 283 | + * @param bool $asConstant |
|
| 284 | + * @return string|int|null |
|
| 285 | + */ |
|
| 286 | + public function getType($asConstant = false) |
|
| 287 | + { |
|
| 288 | + $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 289 | + |
|
| 290 | + if ($asConstant) { |
|
| 291 | + return $type; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
| 295 | + |
|
| 296 | + return $stringType ? strtolower($stringType) : null; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Check if the phone number is of (a) given type(s). |
|
| 301 | + * |
|
| 302 | + * @param string $type |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + public function isOfType($type) |
|
| 306 | + { |
|
| 307 | + $types = static::parseTypes($type); |
|
| 308 | + |
|
| 309 | + // Add the unsure type when applicable. |
|
| 310 | + if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
| 311 | + $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + return in_array($this->getType(true), $types, true); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Get the PhoneNumber instance of the current number. |
|
| 319 | + * |
|
| 320 | + * @return \libphonenumber\PhoneNumber |
|
| 321 | + */ |
|
| 322 | + public function getPhoneNumberInstance() |
|
| 323 | + { |
|
| 324 | + return $this->lib->parse($this->number, $this->getCountry()); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Determine whether the phone number seems to be in international format. |
|
| 329 | + * |
|
| 330 | + * @return bool |
|
| 331 | + */ |
|
| 332 | + protected function numberLooksInternational() |
|
| 333 | + { |
|
| 334 | + return Str::startsWith($this->number, '+'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Enable lenient number parsing. |
|
| 339 | + * |
|
| 340 | + * @return $this |
|
| 341 | + */ |
|
| 342 | + public function lenient() |
|
| 343 | + { |
|
| 344 | + $this->lenient = true; |
|
| 345 | + |
|
| 346 | + return $this; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Convert the phone instance to JSON. |
|
| 351 | + * |
|
| 352 | + * @param int $options |
|
| 353 | + * @return string |
|
| 354 | + */ |
|
| 355 | + public function toJson($options = 0) |
|
| 356 | + { |
|
| 357 | + return json_encode($this->jsonSerialize(), $options); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Convert the phone instance into something JSON serializable. |
|
| 362 | + * |
|
| 363 | + * @return string |
|
| 364 | + */ |
|
| 365 | + public function jsonSerialize() |
|
| 366 | + { |
|
| 367 | + return $this->formatE164(); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Convert the phone instance into a string representation. |
|
| 372 | + * |
|
| 373 | + * @return string |
|
| 374 | + */ |
|
| 375 | + public function serialize() |
|
| 376 | + { |
|
| 377 | + return $this->formatE164(); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Reconstructs the phone instance from a string representation. |
|
| 382 | + * |
|
| 383 | + * @param string $serialized |
|
| 384 | + */ |
|
| 385 | + public function unserialize($serialized) |
|
| 386 | + { |
|
| 387 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 388 | + $this->number = $serialized; |
|
| 389 | + $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Convert the phone instance to a formatted number. |
|
| 394 | + * |
|
| 395 | + * @return string |
|
| 396 | + */ |
|
| 397 | + public function __toString() |
|
| 398 | + { |
|
| 399 | + // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
| 400 | + // Let's just return the original number in that case. |
|
| 401 | + try { |
|
| 402 | + return $this->formatE164(); |
|
| 403 | + } catch (Exception $exception) { |
|
| 404 | + return (string) $this->number; |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | 407 | } |