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