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