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