|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use libphonenumber\PhoneNumber; |
|
7
|
|
|
use libphonenumber\PhoneNumberType; |
|
8
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
9
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
10
|
|
|
use libphonenumber\PhoneNumberToCarrierMapper; |
|
11
|
|
|
|
|
12
|
|
|
class LibPhoneNumberValidator implements PhoneNumberValidator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var libphonenumber\PhoneNumberUtil |
|
|
|
|
|
|
16
|
|
|
*/ |
|
17
|
|
|
protected $util; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var libphonenumber\PhoneNumberToCarrierMapper |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
protected $carrierMapper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Number to validate. |
|
26
|
|
|
* @var libphonenumber\PhoneNumber |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
protected $number; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $isValid = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Instantiate the class and get an instance |
|
37
|
|
|
* of the libphonenumber util. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(PhoneNumberUtil $util, PhoneNumberToCarrierMapper $carrierMapper) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->util = $util; |
|
|
|
|
|
|
42
|
|
|
$this->carrierMapper = $carrierMapper; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// Default the phone number to prevent exceptions and allow control flow |
|
45
|
|
|
// to be handled by the developer with the 'isValid...' methods |
|
46
|
|
|
$this->number = new PhoneNumber; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Validate the given number. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $number |
|
53
|
|
|
* @param null|string $countryCode |
|
54
|
|
|
* @return App\PhoneNumberValidator |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
|
|
public function make($number, string $countryCode = null): PhoneNumberValidator |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
$this->number = $this->util->parse($number, $countryCode); |
|
60
|
|
|
} catch (Exception $e) { |
|
61
|
|
|
// Let the 'isValid' methods handle the control-flow. |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Is the number valid for the given country code. |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $countryCode |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isValidForCountry($countryCode): bool |
|
74
|
|
|
{ |
|
75
|
|
|
foreach (array_wrap($countryCode) as $code) { |
|
76
|
|
|
if ($this->util->isValidNumberForRegion($this->number, mb_strtoupper($code))) { |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Is the number a valid mobile. |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function isValidMobile(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->util->getNumberType($this->number) === PhoneNumberType::MOBILE; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Is the number a valid landline/fixed-line. |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isValidFixedLine(): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->util->getNumberType($this->number) === PhoneNumberType::FIXED_LINE; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return the carrier name. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getCarrierName(): string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->carrierMapper->getNameForNumber($this->number, 'en'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Return the phone number. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getNumber(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->util->format($this->number, PhoneNumberFormat::INTERNATIONAL); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get a new instance of the validator. |
|
126
|
|
|
* |
|
127
|
|
|
* @param libphonenumber\PhoneNumberToCarrierMapper $mapper |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function getInstance() |
|
130
|
|
|
{ |
|
131
|
|
|
return new static(PhoneNumberUtil::getInstance(), PhoneNumberToCarrierMapper::getInstance()); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|