@@ -232,7 +232,7 @@ |
||
| 232 | 232 | /** |
| 233 | 233 | * Throw a IndeterminablePhoneCountryException. |
| 234 | 234 | * |
| 235 | - * @param $message |
|
| 235 | + * @param string $message |
|
| 236 | 236 | * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
| 237 | 237 | */ |
| 238 | 238 | protected function throwCountryException($message) |
@@ -16,290 +16,290 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Phone implements Jsonable, JsonSerializable, Serializable |
| 18 | 18 | { |
| 19 | - use ParsesCountries, |
|
| 20 | - ParsesFormats, |
|
| 21 | - ParsesTypes; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The provided phone number. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $number; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The provided phone countries. |
|
| 32 | - * |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $countries = []; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var \libphonenumber\PhoneNumberUtil |
|
| 39 | - */ |
|
| 40 | - protected $lib; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Phone constructor. |
|
| 44 | - * |
|
| 45 | - * @param string $number |
|
| 46 | - */ |
|
| 47 | - public function __construct($number) |
|
| 48 | - { |
|
| 49 | - $this->number = $number; |
|
| 50 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Create a phone instance. |
|
| 55 | - * |
|
| 56 | - * @param string $number |
|
| 57 | - * @param string|array $country |
|
| 58 | - * @return static |
|
| 59 | - */ |
|
| 60 | - public static function make($number, $country = null) |
|
| 61 | - { |
|
| 62 | - $instance = new static($number); |
|
| 63 | - |
|
| 64 | - return $instance->ofCountry($country); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Set the country to which the phone number belongs to. |
|
| 69 | - * |
|
| 70 | - * @param string|array $country |
|
| 71 | - * @return $this |
|
| 72 | - */ |
|
| 73 | - public function ofCountry($country) |
|
| 74 | - { |
|
| 75 | - $instance = clone $this; |
|
| 76 | - $instance->countries = $instance->parseCountries($country); |
|
| 77 | - |
|
| 78 | - return $instance; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Format the phone number in international format. |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function formatInternational() |
|
| 87 | - { |
|
| 88 | - return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Format the phone number in national format. |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function formatNational() |
|
| 97 | - { |
|
| 98 | - return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Format the phone number in E164 format. |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function formatE164() |
|
| 107 | - { |
|
| 108 | - return $this->format(PhoneNumberFormat::E164); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Format the phone number in RFC3966 format. |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function formatRFC3966() |
|
| 117 | - { |
|
| 118 | - return $this->format(PhoneNumberFormat::RFC3966); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Format the phone number in a given format. |
|
| 123 | - * |
|
| 124 | - * @param string $format |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public function format($format) |
|
| 128 | - { |
|
| 129 | - if (! ($format = static::parseFormat($format))) { |
|
| 130 | - return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $country = Arr::get($this->countries, 0); |
|
| 134 | - |
|
| 135 | - if (! $country && ! Str::startsWith($this->number, '+')) { |
|
| 136 | - return $this->throwFormatException('A country should be provided or the number should be in international format'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $this->lib->format( |
|
| 140 | - $this->getPhoneNumberInstance(), |
|
| 141 | - $format |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Format the phone number in a way that it can be dialled from the provided country. |
|
| 147 | - * |
|
| 148 | - * @param string $country |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - public function formatForCountry($country) |
|
| 152 | - { |
|
| 153 | - if (! static::isCountryCode($country)) { |
|
| 154 | - return $this->throwCountryException($country); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $this->lib->formatOutOfCountryCallingNumber( |
|
| 158 | - $this->getPhoneNumberInstance(), |
|
| 159 | - $country |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 165 | - * |
|
| 166 | - * @param string $country |
|
| 167 | - * @param bool $removeFormatting |
|
| 168 | - * @return string |
|
| 169 | - */ |
|
| 170 | - public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 171 | - { |
|
| 172 | - if (! static::isCountryCode($country)) { |
|
| 173 | - return $this->throwCountryException($country); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $this->lib->formatNumberForMobileDialing( |
|
| 177 | - $this->getPhoneNumberInstance(), |
|
| 178 | - $country, |
|
| 179 | - $removeFormatting |
|
| 180 | - ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Get the phone number's country. |
|
| 185 | - * |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - public function getCountry() |
|
| 189 | - { |
|
| 190 | - return $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get the phone number's type. |
|
| 195 | - * |
|
| 196 | - * @param bool $asConstant |
|
| 197 | - * @return string|int |
|
| 198 | - */ |
|
| 199 | - public function getType($asConstant = false) |
|
| 200 | - { |
|
| 201 | - $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 202 | - |
|
| 203 | - return $asConstant ? $type : array_search($type, static::$types); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Get the PhoneNumber instance of the current number. |
|
| 208 | - * |
|
| 209 | - * @return \libphonenumber\PhoneNumber |
|
| 210 | - */ |
|
| 211 | - public function getPhoneNumberInstance() |
|
| 212 | - { |
|
| 213 | - // Let's try each provided country. |
|
| 214 | - foreach ($this->countries as $country) { |
|
| 215 | - try { |
|
| 216 | - return $this->lib->parse($this->number, $country); |
|
| 217 | - } catch (NumberParseException $exception) { |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // Otherwise let's try to autodetect the country if the number is in international format. |
|
| 222 | - if (Str::startsWith($this->number, '+')) { |
|
| 223 | - try { |
|
| 224 | - return $this->lib->parse($this->number, null); |
|
| 225 | - } catch (NumberParseException $exception) { |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return $this->throwCountryException($this->number); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Throw a IndeterminablePhoneCountryException. |
|
| 234 | - * |
|
| 235 | - * @param $message |
|
| 236 | - * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
|
| 237 | - */ |
|
| 238 | - protected function throwCountryException($message) |
|
| 239 | - { |
|
| 240 | - throw new PhoneCountryException($message); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Throw a PhoneFormatException. |
|
| 245 | - * |
|
| 246 | - * @param string $message |
|
| 247 | - * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneFormatException |
|
| 248 | - */ |
|
| 249 | - protected function throwFormatException($message) |
|
| 250 | - { |
|
| 251 | - throw new PhoneFormatException($message); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Convert the phone instance to JSON. |
|
| 256 | - * |
|
| 257 | - * @param int $options |
|
| 258 | - * @return string |
|
| 259 | - */ |
|
| 260 | - public function toJson($options = 0) |
|
| 261 | - { |
|
| 262 | - return json_encode($this->jsonSerialize(), $options); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Convert the phone instance into something JSON serializable. |
|
| 267 | - * |
|
| 268 | - * @return string |
|
| 269 | - */ |
|
| 270 | - public function jsonSerialize() |
|
| 271 | - { |
|
| 272 | - return $this->formatE164(); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Convert the phone instance into a string representation. |
|
| 277 | - * |
|
| 278 | - * @return string |
|
| 279 | - */ |
|
| 280 | - public function serialize() |
|
| 281 | - { |
|
| 282 | - return $this->formatE164(); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Reconstructs the phone instance from a string representation. |
|
| 287 | - * |
|
| 288 | - * @param string $serialized |
|
| 289 | - */ |
|
| 290 | - public function unserialize($serialized) |
|
| 291 | - { |
|
| 292 | - $this->number = $serialized; |
|
| 293 | - $this->countries = []; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Convert the phone instance to a formatted number. |
|
| 298 | - * |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 301 | - public function __toString() |
|
| 302 | - { |
|
| 303 | - return $this->formatE164(); |
|
| 304 | - } |
|
| 19 | + use ParsesCountries, |
|
| 20 | + ParsesFormats, |
|
| 21 | + ParsesTypes; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The provided phone number. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $number; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The provided phone countries. |
|
| 32 | + * |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $countries = []; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var \libphonenumber\PhoneNumberUtil |
|
| 39 | + */ |
|
| 40 | + protected $lib; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Phone constructor. |
|
| 44 | + * |
|
| 45 | + * @param string $number |
|
| 46 | + */ |
|
| 47 | + public function __construct($number) |
|
| 48 | + { |
|
| 49 | + $this->number = $number; |
|
| 50 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Create a phone instance. |
|
| 55 | + * |
|
| 56 | + * @param string $number |
|
| 57 | + * @param string|array $country |
|
| 58 | + * @return static |
|
| 59 | + */ |
|
| 60 | + public static function make($number, $country = null) |
|
| 61 | + { |
|
| 62 | + $instance = new static($number); |
|
| 63 | + |
|
| 64 | + return $instance->ofCountry($country); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Set the country to which the phone number belongs to. |
|
| 69 | + * |
|
| 70 | + * @param string|array $country |
|
| 71 | + * @return $this |
|
| 72 | + */ |
|
| 73 | + public function ofCountry($country) |
|
| 74 | + { |
|
| 75 | + $instance = clone $this; |
|
| 76 | + $instance->countries = $instance->parseCountries($country); |
|
| 77 | + |
|
| 78 | + return $instance; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Format the phone number in international format. |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function formatInternational() |
|
| 87 | + { |
|
| 88 | + return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Format the phone number in national format. |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function formatNational() |
|
| 97 | + { |
|
| 98 | + return $this->format(PhoneNumberFormat::NATIONAL); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Format the phone number in E164 format. |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function formatE164() |
|
| 107 | + { |
|
| 108 | + return $this->format(PhoneNumberFormat::E164); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Format the phone number in RFC3966 format. |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function formatRFC3966() |
|
| 117 | + { |
|
| 118 | + return $this->format(PhoneNumberFormat::RFC3966); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Format the phone number in a given format. |
|
| 123 | + * |
|
| 124 | + * @param string $format |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public function format($format) |
|
| 128 | + { |
|
| 129 | + if (! ($format = static::parseFormat($format))) { |
|
| 130 | + return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $country = Arr::get($this->countries, 0); |
|
| 134 | + |
|
| 135 | + if (! $country && ! Str::startsWith($this->number, '+')) { |
|
| 136 | + return $this->throwFormatException('A country should be provided or the number should be in international format'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $this->lib->format( |
|
| 140 | + $this->getPhoneNumberInstance(), |
|
| 141 | + $format |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Format the phone number in a way that it can be dialled from the provided country. |
|
| 147 | + * |
|
| 148 | + * @param string $country |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + public function formatForCountry($country) |
|
| 152 | + { |
|
| 153 | + if (! static::isCountryCode($country)) { |
|
| 154 | + return $this->throwCountryException($country); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $this->lib->formatOutOfCountryCallingNumber( |
|
| 158 | + $this->getPhoneNumberInstance(), |
|
| 159 | + $country |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
| 165 | + * |
|
| 166 | + * @param string $country |
|
| 167 | + * @param bool $removeFormatting |
|
| 168 | + * @return string |
|
| 169 | + */ |
|
| 170 | + public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
| 171 | + { |
|
| 172 | + if (! static::isCountryCode($country)) { |
|
| 173 | + return $this->throwCountryException($country); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $this->lib->formatNumberForMobileDialing( |
|
| 177 | + $this->getPhoneNumberInstance(), |
|
| 178 | + $country, |
|
| 179 | + $removeFormatting |
|
| 180 | + ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Get the phone number's country. |
|
| 185 | + * |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + public function getCountry() |
|
| 189 | + { |
|
| 190 | + return $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get the phone number's type. |
|
| 195 | + * |
|
| 196 | + * @param bool $asConstant |
|
| 197 | + * @return string|int |
|
| 198 | + */ |
|
| 199 | + public function getType($asConstant = false) |
|
| 200 | + { |
|
| 201 | + $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
| 202 | + |
|
| 203 | + return $asConstant ? $type : array_search($type, static::$types); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Get the PhoneNumber instance of the current number. |
|
| 208 | + * |
|
| 209 | + * @return \libphonenumber\PhoneNumber |
|
| 210 | + */ |
|
| 211 | + public function getPhoneNumberInstance() |
|
| 212 | + { |
|
| 213 | + // Let's try each provided country. |
|
| 214 | + foreach ($this->countries as $country) { |
|
| 215 | + try { |
|
| 216 | + return $this->lib->parse($this->number, $country); |
|
| 217 | + } catch (NumberParseException $exception) { |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // Otherwise let's try to autodetect the country if the number is in international format. |
|
| 222 | + if (Str::startsWith($this->number, '+')) { |
|
| 223 | + try { |
|
| 224 | + return $this->lib->parse($this->number, null); |
|
| 225 | + } catch (NumberParseException $exception) { |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return $this->throwCountryException($this->number); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Throw a IndeterminablePhoneCountryException. |
|
| 234 | + * |
|
| 235 | + * @param $message |
|
| 236 | + * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
|
| 237 | + */ |
|
| 238 | + protected function throwCountryException($message) |
|
| 239 | + { |
|
| 240 | + throw new PhoneCountryException($message); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Throw a PhoneFormatException. |
|
| 245 | + * |
|
| 246 | + * @param string $message |
|
| 247 | + * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneFormatException |
|
| 248 | + */ |
|
| 249 | + protected function throwFormatException($message) |
|
| 250 | + { |
|
| 251 | + throw new PhoneFormatException($message); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Convert the phone instance to JSON. |
|
| 256 | + * |
|
| 257 | + * @param int $options |
|
| 258 | + * @return string |
|
| 259 | + */ |
|
| 260 | + public function toJson($options = 0) |
|
| 261 | + { |
|
| 262 | + return json_encode($this->jsonSerialize(), $options); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Convert the phone instance into something JSON serializable. |
|
| 267 | + * |
|
| 268 | + * @return string |
|
| 269 | + */ |
|
| 270 | + public function jsonSerialize() |
|
| 271 | + { |
|
| 272 | + return $this->formatE164(); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Convert the phone instance into a string representation. |
|
| 277 | + * |
|
| 278 | + * @return string |
|
| 279 | + */ |
|
| 280 | + public function serialize() |
|
| 281 | + { |
|
| 282 | + return $this->formatE164(); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Reconstructs the phone instance from a string representation. |
|
| 287 | + * |
|
| 288 | + * @param string $serialized |
|
| 289 | + */ |
|
| 290 | + public function unserialize($serialized) |
|
| 291 | + { |
|
| 292 | + $this->number = $serialized; |
|
| 293 | + $this->countries = []; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Convert the phone instance to a formatted number. |
|
| 298 | + * |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | + public function __toString() |
|
| 302 | + { |
|
| 303 | + return $this->formatE164(); |
|
| 304 | + } |
|
| 305 | 305 | } |
| 306 | 306 | \ No newline at end of file |
@@ -126,13 +126,13 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public function format($format) |
| 128 | 128 | { |
| 129 | - if (! ($format = static::parseFormat($format))) { |
|
| 130 | - return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
| 129 | + if (!($format = static::parseFormat($format))) { |
|
| 130 | + return $this->throwFormatException('Unknown format "'.(string) $format.'"'); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | $country = Arr::get($this->countries, 0); |
| 134 | 134 | |
| 135 | - if (! $country && ! Str::startsWith($this->number, '+')) { |
|
| 135 | + if (!$country && !Str::startsWith($this->number, '+')) { |
|
| 136 | 136 | return $this->throwFormatException('A country should be provided or the number should be in international format'); |
| 137 | 137 | } |
| 138 | 138 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | public function formatForCountry($country) |
| 152 | 152 | { |
| 153 | - if (! static::isCountryCode($country)) { |
|
| 153 | + if (!static::isCountryCode($country)) { |
|
| 154 | 154 | return $this->throwCountryException($country); |
| 155 | 155 | } |
| 156 | 156 | |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
| 171 | 171 | { |
| 172 | - if (! static::isCountryCode($country)) { |
|
| 172 | + if (!static::isCountryCode($country)) { |
|
| 173 | 173 | return $this->throwCountryException($country); |
| 174 | 174 | } |
| 175 | 175 | |
@@ -1,3 +1,3 @@ |
||
| 1 | 1 | <?php namespace Propaganistas\LaravelPhone\Exceptions; |
| 2 | 2 | |
| 3 | -class PhoneCountryException extends \Exception{} |
|
| 3 | +class PhoneCountryException extends \Exception {} |
|
@@ -4,31 +4,31 @@ |
||
| 4 | 4 | |
| 5 | 5 | trait ParsesCountries |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Determine whether the given country code is valid. |
|
| 9 | - * |
|
| 10 | - * @param string $country |
|
| 11 | - * @return bool |
|
| 12 | - */ |
|
| 13 | - public static function isCountryCode($country) |
|
| 14 | - { |
|
| 15 | - return ISO3166::isValid($country); |
|
| 16 | - } |
|
| 7 | + /** |
|
| 8 | + * Determine whether the given country code is valid. |
|
| 9 | + * |
|
| 10 | + * @param string $country |
|
| 11 | + * @return bool |
|
| 12 | + */ |
|
| 13 | + public static function isCountryCode($country) |
|
| 14 | + { |
|
| 15 | + return ISO3166::isValid($country); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Parse the provided phone countries to a valid array. |
|
| 20 | - * |
|
| 21 | - * @param string|array $countries |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - protected function parseCountries($countries) |
|
| 25 | - { |
|
| 26 | - $countries = is_array($countries) ? $countries : func_get_args(); |
|
| 18 | + /** |
|
| 19 | + * Parse the provided phone countries to a valid array. |
|
| 20 | + * |
|
| 21 | + * @param string|array $countries |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + protected function parseCountries($countries) |
|
| 25 | + { |
|
| 26 | + $countries = is_array($countries) ? $countries : func_get_args(); |
|
| 27 | 27 | |
| 28 | - return array_filter($countries, function ($code) { |
|
| 29 | - $code = strtoupper($code); |
|
| 28 | + return array_filter($countries, function ($code) { |
|
| 29 | + $code = strtoupper($code); |
|
| 30 | 30 | |
| 31 | - return static::isCountryCode($code) ? $code : null; |
|
| 32 | - }); |
|
| 33 | - } |
|
| 31 | + return static::isCountryCode($code) ? $code : null; |
|
| 32 | + }); |
|
| 33 | + } |
|
| 34 | 34 | } |
| 35 | 35 | \ No newline at end of file |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | { |
| 26 | 26 | $countries = is_array($countries) ? $countries : func_get_args(); |
| 27 | 27 | |
| 28 | - return array_filter($countries, function ($code) { |
|
| 28 | + return array_filter($countries, function($code) { |
|
| 29 | 29 | $code = strtoupper($code); |
| 30 | 30 | |
| 31 | 31 | return static::isCountryCode($code) ? $code : null; |
@@ -6,80 +6,80 @@ |
||
| 6 | 6 | |
| 7 | 7 | trait ParsesTypes |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Array of available phone formats. |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected static $types; |
|
| 9 | + /** |
|
| 10 | + * Array of available phone formats. |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected static $types; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Determine whether the given type is valid. |
|
| 18 | - * |
|
| 19 | - * @param string $type |
|
| 20 | - * @return bool |
|
| 21 | - */ |
|
| 22 | - public static function isType($type) |
|
| 23 | - { |
|
| 24 | - return ! is_null(static::parseTypes($type)); |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Determine whether the given type is valid. |
|
| 18 | + * |
|
| 19 | + * @param string $type |
|
| 20 | + * @return bool |
|
| 21 | + */ |
|
| 22 | + public static function isType($type) |
|
| 23 | + { |
|
| 24 | + return ! is_null(static::parseTypes($type)); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Parse a phone type. |
|
| 29 | - * |
|
| 30 | - * @param string|array $types |
|
| 31 | - * @return array |
|
| 32 | - */ |
|
| 33 | - protected static function parseTypes($types) |
|
| 34 | - { |
|
| 35 | - static::loadTypes(); |
|
| 27 | + /** |
|
| 28 | + * Parse a phone type. |
|
| 29 | + * |
|
| 30 | + * @param string|array $types |
|
| 31 | + * @return array |
|
| 32 | + */ |
|
| 33 | + protected static function parseTypes($types) |
|
| 34 | + { |
|
| 35 | + static::loadTypes(); |
|
| 36 | 36 | |
| 37 | - $types = is_array($types) ? $types : func_get_args(); |
|
| 37 | + $types = is_array($types) ? $types : func_get_args(); |
|
| 38 | 38 | |
| 39 | - return array_filter($types, function ($type) { |
|
| 40 | - // If the type equals a constant's value, just return it. |
|
| 41 | - if (in_array($type, static::$types)) { |
|
| 42 | - return $type; |
|
| 43 | - } |
|
| 39 | + return array_filter($types, function ($type) { |
|
| 40 | + // If the type equals a constant's value, just return it. |
|
| 41 | + if (in_array($type, static::$types)) { |
|
| 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 | - } |
|
| 45 | + // Otherwise we'll assume the type is the constant's name. |
|
| 46 | + return Arr::get(static::$types, strtoupper($type)); |
|
| 47 | + }); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Parse a phone type into its string representation. |
|
| 52 | - * |
|
| 53 | - * @param string|array $types |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - protected static function parseTypesAsStrings($types) |
|
| 57 | - { |
|
| 58 | - static::loadTypes(); |
|
| 50 | + /** |
|
| 51 | + * Parse a phone type into its string representation. |
|
| 52 | + * |
|
| 53 | + * @param string|array $types |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + protected static function parseTypesAsStrings($types) |
|
| 57 | + { |
|
| 58 | + static::loadTypes(); |
|
| 59 | 59 | |
| 60 | - $types = is_array($types) ? $types : func_get_args(); |
|
| 60 | + $types = is_array($types) ? $types : func_get_args(); |
|
| 61 | 61 | |
| 62 | - return array_filter($types, function ($type) { |
|
| 63 | - $type = strtoupper($type); |
|
| 62 | + return array_filter($types, function ($type) { |
|
| 63 | + $type = strtoupper($type); |
|
| 64 | 64 | |
| 65 | - // If the type equals a constant's name, just return it. |
|
| 66 | - if (isset(static::$types[$type])) { |
|
| 67 | - return static::$types[$type]; |
|
| 68 | - } |
|
| 65 | + // If the type equals a constant's name, just return it. |
|
| 66 | + if (isset(static::$types[$type])) { |
|
| 67 | + return static::$types[$type]; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - // Otherwise we'll assume the type is the constant's value. |
|
| 71 | - return array_search($type, static::$types); |
|
| 72 | - }); |
|
| 73 | - } |
|
| 70 | + // Otherwise we'll assume the type is the constant's value. |
|
| 71 | + return array_search($type, static::$types); |
|
| 72 | + }); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Load all available formats once. |
|
| 78 | - */ |
|
| 79 | - private static function loadTypes() |
|
| 80 | - { |
|
| 81 | - if (! static::$types) { |
|
| 82 | - static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
| 83 | - } |
|
| 84 | - } |
|
| 76 | + /** |
|
| 77 | + * Load all available formats once. |
|
| 78 | + */ |
|
| 79 | + private static function loadTypes() |
|
| 80 | + { |
|
| 81 | + if (! static::$types) { |
|
| 82 | + static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | } |
| 86 | 86 | \ No newline at end of file |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public static function isType($type) |
| 23 | 23 | { |
| 24 | - return ! is_null(static::parseTypes($type)); |
|
| 24 | + return !is_null(static::parseTypes($type)); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | $types = is_array($types) ? $types : func_get_args(); |
| 38 | 38 | |
| 39 | - return array_filter($types, function ($type) { |
|
| 39 | + return array_filter($types, function($type) { |
|
| 40 | 40 | // If the type equals a constant's value, just return it. |
| 41 | 41 | if (in_array($type, static::$types)) { |
| 42 | 42 | return $type; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | $types = is_array($types) ? $types : func_get_args(); |
| 61 | 61 | |
| 62 | - return array_filter($types, function ($type) { |
|
| 62 | + return array_filter($types, function($type) { |
|
| 63 | 63 | $type = strtoupper($type); |
| 64 | 64 | |
| 65 | 65 | // If the type equals a constant's name, just return it. |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | private static function loadTypes() |
| 80 | 80 | { |
| 81 | - if (! static::$types) { |
|
| 81 | + if (!static::$types) { |
|
| 82 | 82 | static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
| 83 | 83 | } |
| 84 | 84 | } |
@@ -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 isFormat($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 isFormat($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)) { |
|
| 39 | - return $format; |
|
| 40 | - } |
|
| 37 | + // If the format equals a constant's value, just return it. |
|
| 38 | + if (in_array($format, static::$formats)) { |
|
| 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 isFormat($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 | } |
@@ -5,103 +5,103 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Phone |
| 7 | 7 | { |
| 8 | - use ParsesCountries, |
|
| 9 | - ParsesTypes; |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * The provided phone countries. |
|
| 13 | - * |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $countries = []; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The provided phone types. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $types = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Whether the number's country should be auto-detected. |
|
| 27 | - * |
|
| 28 | - * @var bool |
|
| 29 | - */ |
|
| 30 | - protected $detect = false; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 34 | - * |
|
| 35 | - * @var bool |
|
| 36 | - */ |
|
| 37 | - protected $lenient = false; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Set the phone countries. |
|
| 41 | - * |
|
| 42 | - * @param string|array $country |
|
| 43 | - * @return $this |
|
| 44 | - */ |
|
| 45 | - public function country($country) |
|
| 46 | - { |
|
| 47 | - $countries = is_array($country) ? $country : func_get_args(); |
|
| 48 | - |
|
| 49 | - $this->countries = static::parseCountries($countries); |
|
| 50 | - |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Set the phone types. |
|
| 56 | - * |
|
| 57 | - * @param string|array $type |
|
| 58 | - * @return $this |
|
| 59 | - */ |
|
| 60 | - public function type($type) |
|
| 61 | - { |
|
| 62 | - $types = is_array($type) ? $type : func_get_args(); |
|
| 63 | - |
|
| 64 | - $this->types = static::parseTypesAsStrings($types); |
|
| 65 | - |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Enable automatic country detection. |
|
| 71 | - * |
|
| 72 | - * @return $this |
|
| 73 | - */ |
|
| 74 | - public function detect() |
|
| 75 | - { |
|
| 76 | - $this->detect = true; |
|
| 77 | - |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Enable lenient number checking. |
|
| 83 | - * |
|
| 84 | - * @return $this |
|
| 85 | - */ |
|
| 86 | - public function lenient() |
|
| 87 | - { |
|
| 88 | - $this->lenient = true; |
|
| 89 | - |
|
| 90 | - return $this; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Convert the rule to a validation string. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function __toString() |
|
| 99 | - { |
|
| 100 | - return rtrim(sprintf('phone:%s,%s,%s,%s', |
|
| 101 | - implode(',', $this->countries), |
|
| 102 | - implode(',', $this->types), |
|
| 103 | - $this->detect ? 'AUTO' : '', |
|
| 104 | - $this->lenient ? 'LENIENT' : '' |
|
| 105 | - ), ','); |
|
| 106 | - } |
|
| 8 | + use ParsesCountries, |
|
| 9 | + ParsesTypes; |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * The provided phone countries. |
|
| 13 | + * |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $countries = []; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The provided phone types. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $types = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Whether the number's country should be auto-detected. |
|
| 27 | + * |
|
| 28 | + * @var bool |
|
| 29 | + */ |
|
| 30 | + protected $detect = false; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
| 34 | + * |
|
| 35 | + * @var bool |
|
| 36 | + */ |
|
| 37 | + protected $lenient = false; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Set the phone countries. |
|
| 41 | + * |
|
| 42 | + * @param string|array $country |
|
| 43 | + * @return $this |
|
| 44 | + */ |
|
| 45 | + public function country($country) |
|
| 46 | + { |
|
| 47 | + $countries = is_array($country) ? $country : func_get_args(); |
|
| 48 | + |
|
| 49 | + $this->countries = static::parseCountries($countries); |
|
| 50 | + |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Set the phone types. |
|
| 56 | + * |
|
| 57 | + * @param string|array $type |
|
| 58 | + * @return $this |
|
| 59 | + */ |
|
| 60 | + public function type($type) |
|
| 61 | + { |
|
| 62 | + $types = is_array($type) ? $type : func_get_args(); |
|
| 63 | + |
|
| 64 | + $this->types = static::parseTypesAsStrings($types); |
|
| 65 | + |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Enable automatic country detection. |
|
| 71 | + * |
|
| 72 | + * @return $this |
|
| 73 | + */ |
|
| 74 | + public function detect() |
|
| 75 | + { |
|
| 76 | + $this->detect = true; |
|
| 77 | + |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Enable lenient number checking. |
|
| 83 | + * |
|
| 84 | + * @return $this |
|
| 85 | + */ |
|
| 86 | + public function lenient() |
|
| 87 | + { |
|
| 88 | + $this->lenient = true; |
|
| 89 | + |
|
| 90 | + return $this; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Convert the rule to a validation string. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function __toString() |
|
| 99 | + { |
|
| 100 | + return rtrim(sprintf('phone:%s,%s,%s,%s', |
|
| 101 | + implode(',', $this->countries), |
|
| 102 | + implode(',', $this->types), |
|
| 103 | + $this->detect ? 'AUTO' : '', |
|
| 104 | + $this->lenient ? 'LENIENT' : '' |
|
| 105 | + ), ','); |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | \ No newline at end of file |