1 | <?php |
||||||
2 | |||||||
3 | namespace Yeelight\Models\Basic; |
||||||
4 | |||||||
5 | use Illuminate\Contracts\Support\Arrayable; |
||||||
6 | use libphonenumber\CountryCodeSource; |
||||||
7 | use libphonenumber\geocoding\PhoneNumberOfflineGeocoder; |
||||||
8 | use libphonenumber\PhoneNumber; |
||||||
9 | use libphonenumber\PhoneNumberFormat; |
||||||
10 | use libphonenumber\PhoneNumberToCarrierMapper; |
||||||
11 | use libphonenumber\PhoneNumberToTimeZonesMapper; |
||||||
12 | use libphonenumber\PhoneNumberType; |
||||||
13 | use libphonenumber\PhoneNumberUtil; |
||||||
14 | |||||||
15 | /** |
||||||
16 | * Class PhoneNumberModel |
||||||
17 | * |
||||||
18 | * @category Yeelight |
||||||
19 | * |
||||||
20 | * @package Yeelight\Models\Basic |
||||||
21 | * |
||||||
22 | * @author Sheldon Lee <[email protected]> |
||||||
23 | * |
||||||
24 | * @license https://opensource.org/licenses/MIT MIT |
||||||
25 | * |
||||||
26 | * @link https://www.yeelight.com |
||||||
27 | */ |
||||||
28 | class PhoneNumberModel implements Arrayable |
||||||
29 | { |
||||||
30 | /** |
||||||
31 | * @var PhoneNumberUtil |
||||||
32 | */ |
||||||
33 | private $phoneUtil; |
||||||
34 | |||||||
35 | /** |
||||||
36 | * @var PhoneNumber|null |
||||||
37 | */ |
||||||
38 | private $phoneNumberProto; |
||||||
39 | |||||||
40 | private $countryCodeSourcesText = [ |
||||||
41 | CountryCodeSource::FROM_DEFAULT_COUNTRY => 'FROM_DEFAULT_COUNTRY', |
||||||
42 | CountryCodeSource::FROM_NUMBER_WITH_IDD => 'FROM_NUMBER_WITH_IDD', |
||||||
43 | CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN => 'FROM_NUMBER_WITH_PLUS_SIGN', |
||||||
44 | CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN => 'FROM_NUMBER_WITHOUT_PLUS_SIGN', |
||||||
45 | ]; |
||||||
46 | |||||||
47 | private $phoneNumberTypesText = [ |
||||||
48 | PhoneNumberType::FIXED_LINE => 'FIXED_LINE', |
||||||
49 | PhoneNumberType::FIXED_LINE_OR_MOBILE => 'FIXED_LINE_OR_MOBILE', |
||||||
50 | PhoneNumberType::MOBILE => 'MOBILE', |
||||||
51 | PhoneNumberType::EMERGENCY => 'EMERGENCY', |
||||||
52 | PhoneNumberType::SHORT_CODE => 'SHORT_CODE', |
||||||
53 | PhoneNumberType::UNKNOWN => 'UNKNOWN', |
||||||
54 | ]; |
||||||
55 | |||||||
56 | /** |
||||||
57 | * Fields from libphonenumber\PhoneNumber. |
||||||
58 | */ |
||||||
59 | public $country_code; // 65 |
||||||
60 | public $country_code_plus_sign; // +65 |
||||||
61 | public $national_number; |
||||||
62 | public $extension; |
||||||
63 | public $country_code_source; // 0 |
||||||
64 | public $country_code_source_text; // FROM_NUMBER_WITH_PLUS_SIGN |
||||||
65 | public $preferred_domestic_carrier_code; |
||||||
66 | public $raw_input; |
||||||
67 | public $raw_input_country; |
||||||
68 | |||||||
69 | /** |
||||||
70 | * Validation Results. |
||||||
71 | */ |
||||||
72 | public $is_possible_number = false; |
||||||
73 | public $is_valid_number = false; |
||||||
74 | public $is_mobile_number = false; |
||||||
75 | public $region_code_for_number; // SG |
||||||
76 | public $number_type; // 1 |
||||||
77 | public $number_type_text; // MOBILE |
||||||
78 | |||||||
79 | /** |
||||||
80 | * From Formatting. |
||||||
81 | */ |
||||||
82 | public $format_e164; // +6590919293 |
||||||
83 | public $format_national; // 9091 9293 |
||||||
84 | public $format_international; // +65 9091 9293 |
||||||
85 | public $format_rfc3966; // tel:+65-9091-9293 |
||||||
86 | |||||||
87 | /** |
||||||
88 | * From additional. |
||||||
89 | */ |
||||||
90 | public $description; // Singapore |
||||||
91 | public $carrier_name; // M1 |
||||||
92 | public $timezones; // Asia/Singapore |
||||||
93 | |||||||
94 | /** |
||||||
95 | * PhoneNumberModel constructor. |
||||||
96 | * |
||||||
97 | * @param string $phone_number |
||||||
98 | * @param string $country_code An ISO 3166-1 two letter country code |
||||||
99 | */ |
||||||
100 | public function __construct($phone_number, $country_code) |
||||||
101 | { |
||||||
102 | $this->phoneUtil = PhoneNumberUtil::getInstance(); |
||||||
103 | |||||||
104 | $this->phoneNumberProto = phone_parse($phone_number, $country_code); |
||||||
105 | |||||||
106 | $this->raw_input_country = $country_code; |
||||||
107 | |||||||
108 | $this->parse(); |
||||||
109 | } |
||||||
110 | |||||||
111 | private function parse() |
||||||
112 | { |
||||||
113 | if ($this->phoneNumberProto) { |
||||||
114 | // from phoneNumberProto |
||||||
115 | $this->country_code = $this->phoneNumberProto->getCountryCode(); |
||||||
116 | $this->country_code_plus_sign = '+'.$this->country_code; |
||||||
117 | $this->national_number = $this->phoneNumberProto->getNationalNumber(); |
||||||
118 | $this->extension = $this->phoneNumberProto->getExtension(); |
||||||
119 | $this->country_code_source = $this->phoneNumberProto->getCountryCodeSource(); |
||||||
120 | if (isset($this->countryCodeSourcesText[$this->country_code_source])) { |
||||||
121 | $this->country_code_source_text = $this->countryCodeSourcesText[$this->country_code_source]; |
||||||
122 | } |
||||||
123 | $this->raw_input = $this->phoneNumberProto->getRawInput(); |
||||||
124 | $this->preferred_domestic_carrier_code = $this->phoneNumberProto->getPreferredDomesticCarrierCode(); |
||||||
125 | |||||||
126 | // from validation |
||||||
127 | $this->is_possible_number = $this->phoneUtil->isPossibleNumber($this->phoneNumberProto); |
||||||
128 | $this->is_valid_number = $this->phoneUtil->isValidNumber($this->phoneNumberProto); |
||||||
129 | $this->region_code_for_number = $this->phoneUtil->getRegionCodeForNumber($this->phoneNumberProto); |
||||||
130 | $this->number_type = $this->phoneUtil->getNumberType($this->phoneNumberProto); |
||||||
131 | $this->number_type_text = $this->phoneUtil->getNumberType($this->phoneNumberProto); |
||||||
132 | if (isset($this->phoneNumberTypesText[$this->number_type])) { |
||||||
133 | $this->number_type_text = $this->phoneNumberTypesText[$this->number_type]; |
||||||
134 | } else { |
||||||
135 | $this->number_type_text = 'OTHER'; |
||||||
136 | } |
||||||
137 | $this->is_mobile_number = in_array($this->number_type, [ |
||||||
138 | PhoneNumberType::FIXED_LINE_OR_MOBILE, |
||||||
139 | PhoneNumberType::MOBILE, |
||||||
140 | ]); |
||||||
141 | |||||||
142 | // from formatting |
||||||
143 | $this->format_e164 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::E164); |
||||||
144 | $this->format_international = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::INTERNATIONAL); |
||||||
145 | $this->format_national = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::NATIONAL); |
||||||
146 | $this->format_rfc3966 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::RFC3966); |
||||||
147 | |||||||
148 | // from additional |
||||||
149 | $phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder::getInstance(); |
||||||
150 | $this->description = $phoneNumberOfflineGeocoder->getDescriptionForNumber($this->phoneNumberProto, 'en'); |
||||||
151 | |||||||
152 | $phoneNumberToCarrierMapper = PhoneNumberToCarrierMapper::getInstance(); |
||||||
153 | $this->carrier_name = $phoneNumberToCarrierMapper->getNameForNumber($this->phoneNumberProto, 'en'); |
||||||
154 | |||||||
155 | $phoneNumberToTimeZonesMapper = PhoneNumberToTimeZonesMapper::getInstance(); |
||||||
156 | $this->timezones = $phoneNumberToTimeZonesMapper->getTimeZonesForNumber($this->phoneNumberProto); |
||||||
157 | } |
||||||
158 | } |
||||||
159 | |||||||
160 | public function isValid() |
||||||
161 | { |
||||||
162 | return $this->phoneNumberProto != null; |
||||||
163 | } |
||||||
164 | |||||||
165 | public function isValidNumberForRegion($country_code) |
||||||
166 | { |
||||||
167 | return $this->phoneUtil->isValidNumberForRegion($this->phoneNumber, $country_code); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
168 | } |
||||||
169 | |||||||
170 | public function isValidNumber() |
||||||
171 | { |
||||||
172 | return $this->is_valid_number; |
||||||
173 | } |
||||||
174 | |||||||
175 | public function isMobileNumber() |
||||||
176 | { |
||||||
177 | return $this->is_mobile_number; |
||||||
178 | } |
||||||
179 | |||||||
180 | public function __toString() |
||||||
181 | { |
||||||
182 | return (string) $this->phoneNumberProto; |
||||||
183 | } |
||||||
184 | |||||||
185 | public function equals(self $other) |
||||||
186 | { |
||||||
187 | if (empty($this->isValid()) || empty($other->isValid())) { |
||||||
188 | return false; |
||||||
189 | } |
||||||
190 | |||||||
191 | return $this->phoneNumberProto->equals($other->phoneNumberProto); |
||||||
0 ignored issues
–
show
The method
equals() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() It seems like
$other->phoneNumberProto can also be of type null ; however, parameter $other of libphonenumber\PhoneNumber::equals() does only seem to accept libphonenumber\PhoneNumber , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
192 | } |
||||||
193 | |||||||
194 | public function toArray() |
||||||
195 | { |
||||||
196 | return [ |
||||||
197 | 'country_code' => $this->country_code, |
||||||
198 | 'country_code_plus_sign' => $this->country_code_plus_sign, |
||||||
199 | 'national_number' => $this->national_number, |
||||||
200 | 'extension' => $this->extension, |
||||||
201 | 'country_code_source' => $this->country_code_source, |
||||||
202 | 'country_code_source_text' => $this->country_code_source_text, |
||||||
203 | 'preferred_domestic_carrier_code' => $this->preferred_domestic_carrier_code, |
||||||
204 | 'raw_input' => $this->raw_input, |
||||||
205 | 'raw_input_country' => $this->raw_input_country, |
||||||
206 | |||||||
207 | 'is_possible_number' => $this->is_possible_number, |
||||||
208 | 'is_valid_number' => $this->is_valid_number, |
||||||
209 | 'is_mobile_number' => $this->is_mobile_number, |
||||||
210 | 'region_code_for_number' => $this->region_code_for_number, |
||||||
211 | 'number_type' => $this->number_type, |
||||||
212 | 'number_type_text' => $this->number_type_text, |
||||||
213 | |||||||
214 | 'format_e164' => $this->format_e164, |
||||||
215 | 'format_national' => $this->format_national, |
||||||
216 | 'format_international' => $this->format_international, |
||||||
217 | 'format_rfc3966' => $this->format_rfc3966, |
||||||
218 | |||||||
219 | 'description' => $this->description, |
||||||
220 | 'carrier_name' => $this->carrier_name, |
||||||
221 | 'timezones' => $this->timezones, |
||||||
222 | ]; |
||||||
223 | } |
||||||
224 | } |
||||||
225 |