Completed
Push — master ( 408da6...f65d4e )
by Propa
03:02
created
src/PhoneNumber.php 1 patch
Indentation   +383 added lines, -383 removed lines patch added patch discarded remove patch
@@ -21,387 +21,387 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.