|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Methods for getting information about short phone numbers, such as short codes and emergency |
|
4
|
|
|
* numbers. Note that most commercial short numbers are not handled here, but by the |
|
5
|
|
|
* {@link PhoneNumberUtil}. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Shaopeng Jia |
|
8
|
|
|
* @author David Yonge-Mallo |
|
9
|
|
|
* @since 5.8 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace libphonenumber; |
|
13
|
|
|
|
|
14
|
|
|
class ShortNumberInfo |
|
15
|
|
|
{ |
|
16
|
|
|
const META_DATA_FILE_PREFIX = 'ShortNumberMetadata'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ShortNumberInfo |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $instance = null; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var MatcherAPIInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $matcherAPI; |
|
25
|
|
|
protected $currentFilePrefix; |
|
26
|
|
|
protected $regionToMetadataMap = array(); |
|
27
|
|
|
protected $countryCallingCodeToRegionCodeMap = array(); |
|
28
|
|
|
protected $countryCodeToNonGeographicalMetadataMap = array(); |
|
29
|
|
|
protected static $regionsWhereEmergencyNumbersMustBeExact = array( |
|
30
|
|
|
'BR', |
|
31
|
|
|
'CL', |
|
32
|
|
|
'NI', |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
28 |
|
protected function __construct(MatcherAPIInterface $matcherAPI) |
|
36
|
|
|
{ |
|
37
|
28 |
|
$this->matcherAPI = $matcherAPI; |
|
38
|
|
|
|
|
39
|
|
|
// TODO: Create ShortNumberInfo for a given map |
|
40
|
28 |
|
$this->countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap; |
|
41
|
|
|
|
|
42
|
28 |
|
$this->currentFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX; |
|
43
|
|
|
|
|
44
|
|
|
// Initialise PhoneNumberUtil to make sure regex's are setup correctly |
|
45
|
28 |
|
PhoneNumberUtil::getInstance(); |
|
46
|
28 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the singleton instance of ShortNumberInfo |
|
50
|
|
|
* |
|
51
|
|
|
* @return \libphonenumber\ShortNumberInfo |
|
52
|
|
|
*/ |
|
53
|
5154 |
|
public static function getInstance() |
|
54
|
|
|
{ |
|
55
|
5154 |
|
if (null === static::$instance) { |
|
56
|
28 |
|
static::$instance = new self(RegexBasedMatcher::create()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
5154 |
|
return static::$instance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
27 |
|
public static function resetInstance() |
|
63
|
|
|
{ |
|
64
|
27 |
|
static::$instance = null; |
|
65
|
27 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns a list with teh region codes that match the specific country calling code. For |
|
69
|
|
|
* non-geographical country calling codes, the region code 001 is returned. Also, in the case |
|
70
|
|
|
* of no region code being found, an empty list is returned. |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $countryCallingCode |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
583 |
|
protected function getRegionCodesForCountryCode($countryCallingCode) |
|
76
|
|
|
{ |
|
77
|
583 |
|
if (!array_key_exists($countryCallingCode, $this->countryCallingCodeToRegionCodeMap)) { |
|
78
|
1 |
|
$regionCodes = null; |
|
79
|
|
|
} else { |
|
80
|
583 |
|
$regionCodes = $this->countryCallingCodeToRegionCodeMap[$countryCallingCode]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
583 |
|
return ($regionCodes === null) ? array() : $regionCodes; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Helper method to check that the country calling code of the number matches the region it's |
|
88
|
|
|
* being dialed from. |
|
89
|
|
|
* @param PhoneNumber $number |
|
90
|
|
|
* @param string $regionDialingFrom |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
582 |
|
protected function regionDialingFromMatchesNumber(PhoneNumber $number, $regionDialingFrom) |
|
94
|
|
|
{ |
|
95
|
582 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
96
|
|
|
|
|
97
|
582 |
|
return in_array($regionDialingFrom, $regionCodes); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getSupportedRegions() |
|
101
|
|
|
{ |
|
102
|
|
|
return ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Gets a valid short number for the specified region. |
|
107
|
|
|
* |
|
108
|
|
|
* @param $regionCode String the region for which an example short number is needed |
|
109
|
|
|
* @return string a valid short number for the specified region. Returns an empty string when the |
|
110
|
|
|
* metadata does not contain such information. |
|
111
|
|
|
*/ |
|
112
|
239 |
|
public function getExampleShortNumber($regionCode) |
|
113
|
|
|
{ |
|
114
|
239 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
|
115
|
239 |
|
if ($phoneMetadata === null) { |
|
116
|
1 |
|
return ""; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** @var PhoneNumberDesc $desc */ |
|
120
|
239 |
|
$desc = $phoneMetadata->getShortCode(); |
|
121
|
239 |
|
if ($desc !== null && $desc->hasExampleNumber()) { |
|
122
|
239 |
|
return $desc->getExampleNumber(); |
|
123
|
|
|
} |
|
124
|
|
|
return ""; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $regionCode |
|
129
|
|
|
* @return PhoneMetadata|null |
|
130
|
|
|
*/ |
|
131
|
1709 |
|
public function getMetadataForRegion($regionCode) |
|
132
|
|
|
{ |
|
133
|
1709 |
|
if (!in_array($regionCode, ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet)) { |
|
134
|
1 |
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1709 |
|
if (!isset($this->regionToMetadataMap[$regionCode])) { |
|
138
|
|
|
// The regionCode here will be valid and won't be '001', so we don't need to worry about |
|
139
|
|
|
// what to pass in for the country calling code. |
|
140
|
262 |
|
$this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1709 |
|
return isset($this->regionToMetadataMap[$regionCode]) ? $this->regionToMetadataMap[$regionCode] : null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
262 |
|
protected function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode) |
|
147
|
|
|
{ |
|
148
|
262 |
|
$isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode; |
|
149
|
262 |
|
$fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php'; |
|
150
|
262 |
|
if (!is_readable($fileName)) { |
|
151
|
|
|
throw new \Exception('missing metadata: ' . $fileName); |
|
152
|
|
|
} else { |
|
153
|
262 |
|
$data = include $fileName; |
|
154
|
262 |
|
$metadata = new PhoneMetadata(); |
|
155
|
262 |
|
$metadata->fromArray($data); |
|
156
|
262 |
|
if ($isNonGeoRegion) { |
|
157
|
|
|
$this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata; |
|
158
|
|
|
} else { |
|
159
|
262 |
|
$this->regionToMetadataMap[$regionCode] = $metadata; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
262 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Gets a valid short number for the specified cost category. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $regionCode the region for which an example short number is needed |
|
168
|
|
|
* @param int $cost the cost category of number that is needed |
|
169
|
|
|
* @return string a valid short number for the specified region and cost category. Returns an empty string |
|
170
|
|
|
* when the metadata does not contain such information, or the cost is UNKNOWN_COST. |
|
171
|
|
|
*/ |
|
172
|
954 |
|
public function getExampleShortNumberForCost($regionCode, $cost) |
|
173
|
|
|
{ |
|
174
|
954 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
|
175
|
954 |
|
if ($phoneMetadata === null) { |
|
176
|
|
|
return ""; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** @var PhoneNumberDesc $desc */ |
|
180
|
954 |
|
$desc = null; |
|
181
|
|
|
switch ($cost) { |
|
182
|
954 |
|
case ShortNumberCost::TOLL_FREE: |
|
183
|
240 |
|
$desc = $phoneMetadata->getTollFree(); |
|
184
|
240 |
|
break; |
|
185
|
716 |
|
case ShortNumberCost::STANDARD_RATE: |
|
186
|
240 |
|
$desc = $phoneMetadata->getStandardRate(); |
|
187
|
240 |
|
break; |
|
188
|
478 |
|
case ShortNumberCost::PREMIUM_RATE: |
|
189
|
240 |
|
$desc = $phoneMetadata->getPremiumRate(); |
|
190
|
240 |
|
break; |
|
191
|
|
|
default: |
|
192
|
|
|
// UNKNOWN_COST numbers are computed by the process of elimination from the other cost categories |
|
193
|
239 |
|
break; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
954 |
|
if ($desc !== null && $desc->hasExampleNumber()) { |
|
197
|
74 |
|
return $desc->getExampleNumber(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
881 |
|
return ""; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Returns true if the given number, exactly as dialed, might be used to connect to an emergency |
|
205
|
|
|
* service in the given region. |
|
206
|
|
|
* <p> |
|
207
|
|
|
* This method accepts a string, rather than a PhoneNumber, because it needs to distinguish |
|
208
|
|
|
* cases such as "+1 911" and "911", where the former may not connect to an emergency service in |
|
209
|
|
|
* all cases but the latter would. This method takes into account cases where the number might |
|
210
|
|
|
* contain formatting, or might have additional digits appended (when it is okay to do that in |
|
211
|
|
|
* the specified region). |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $number the phone number to test |
|
214
|
|
|
* @param string $regionCode the region where the phone number if being dialled |
|
215
|
|
|
* @return boolean whether the number might be used to connect to an emergency service in the given region |
|
216
|
|
|
*/ |
|
217
|
18 |
|
public function connectsToEmergencyNumber($number, $regionCode) |
|
218
|
|
|
{ |
|
219
|
18 |
|
return $this->matchesEmergencyNumberHelper($number, $regionCode, true /* allows prefix match */); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param string $number |
|
224
|
|
|
* @param string $regionCode |
|
225
|
|
|
* @param bool $allowPrefixMatch |
|
226
|
|
|
* @return bool |
|
227
|
|
|
*/ |
|
228
|
278 |
|
protected function matchesEmergencyNumberHelper($number, $regionCode, $allowPrefixMatch) |
|
229
|
|
|
{ |
|
230
|
278 |
|
$number = PhoneNumberUtil::extractPossibleNumber($number); |
|
231
|
278 |
|
$matcher = new Matcher(PhoneNumberUtil::$PLUS_CHARS_PATTERN, $number); |
|
232
|
278 |
|
if ($matcher->lookingAt()) { |
|
233
|
|
|
// Returns false if the number starts with a plus sign. We don't believe dialing the country |
|
234
|
|
|
// code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can |
|
235
|
|
|
// add additional logic here to handle it. |
|
236
|
4 |
|
return false; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
274 |
|
$metadata = $this->getMetadataForRegion($regionCode); |
|
240
|
274 |
|
if ($metadata === null || !$metadata->hasEmergency()) { |
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
274 |
|
$normalizedNumber = PhoneNumberUtil::normalizeDigitsOnly($number); |
|
245
|
274 |
|
$emergencyDesc = $metadata->getEmergency(); |
|
246
|
|
|
|
|
247
|
274 |
|
$allowPrefixMatchForRegion = ($allowPrefixMatch |
|
248
|
274 |
|
&& !in_array($regionCode, static::$regionsWhereEmergencyNumbersMustBeExact) |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
274 |
|
return $this->matcherAPI->matchesNationalNumber($normalizedNumber, $emergencyDesc, $allowPrefixMatchForRegion); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Given a valid short number, determines whether it is carrier-specific (however, nothing is |
|
256
|
|
|
* implied about its validity). If it is important that the number is valid, then its validity |
|
257
|
|
|
* must first be checked using {@link isValidShortNumber} or |
|
258
|
|
|
* {@link #isValidShortNumberForRegion}. |
|
259
|
|
|
* |
|
260
|
|
|
* @param PhoneNumber $number the valid short number to check |
|
261
|
|
|
* @return boolean whether the short number is carrier-specific (assuming the input was a valid short |
|
262
|
|
|
* number). |
|
263
|
|
|
*/ |
|
264
|
27 |
|
public function isCarrierSpecific(PhoneNumber $number) |
|
265
|
|
|
{ |
|
266
|
27 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
267
|
27 |
|
$regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes); |
|
268
|
27 |
|
$nationalNumber = $this->getNationalSignificantNumber($number); |
|
269
|
27 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
|
270
|
|
|
|
|
271
|
27 |
|
return ($phoneMetadata !== null) && ($this->matchesPossibleNumberAndNationalNumber( |
|
272
|
|
|
$nationalNumber, |
|
273
|
27 |
|
$phoneMetadata->getCarrierSpecific() |
|
274
|
|
|
)); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Helper method to get the region code for a given phone number, from a list of possible region |
|
279
|
|
|
* codes. If the list contains more than one region, the first region for which the number is |
|
280
|
|
|
* valid is returned. |
|
281
|
|
|
* |
|
282
|
|
|
* @param PhoneNumber $number |
|
283
|
|
|
* @param $regionCodes |
|
284
|
|
|
* @return String|null Region Code (or null if none are found) |
|
285
|
|
|
*/ |
|
286
|
269 |
|
protected function getRegionCodeForShortNumberFromRegionList(PhoneNumber $number, $regionCodes) |
|
287
|
|
|
{ |
|
288
|
269 |
|
if (count($regionCodes) == 0) { |
|
289
|
|
|
return null; |
|
290
|
269 |
|
} elseif (count($regionCodes) == 1) { |
|
291
|
213 |
|
return $regionCodes[0]; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
57 |
|
$nationalNumber = $this->getNationalSignificantNumber($number); |
|
295
|
|
|
|
|
296
|
57 |
|
foreach ($regionCodes as $regionCode) { |
|
297
|
57 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
|
298
|
57 |
|
if ($phoneMetadata !== null |
|
299
|
57 |
|
&& $this->matchesPossibleNumberAndNationalNumber($nationalNumber, $phoneMetadata->getShortCode()) |
|
300
|
|
|
) { |
|
301
|
|
|
// The number is valid for this region. |
|
302
|
57 |
|
return $regionCode; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
return null; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Check whether a short number is a possible number. If a country calling code is shared by |
|
310
|
|
|
* multiple regions, this returns true if it's possible in any of them. This provides a more |
|
311
|
|
|
* lenient check than {@link #isValidShortNumber}. See {@link |
|
312
|
|
|
* #IsPossibleShortNumberForRegion(PhoneNumber, String)} for details. |
|
313
|
|
|
* |
|
314
|
|
|
* @param $number PhoneNumber the short number to check |
|
315
|
|
|
* @return boolean whether the number is a possible short number |
|
316
|
|
|
*/ |
|
317
|
2 |
|
public function isPossibleShortNumber(PhoneNumber $number) |
|
318
|
|
|
{ |
|
319
|
2 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
320
|
2 |
|
$shortNumber = $this->getNationalSignificantNumber($number); |
|
321
|
|
|
|
|
322
|
2 |
|
foreach ($regionCodes as $region) { |
|
323
|
2 |
|
$phoneMetadata = $this->getMetadataForRegion($region); |
|
324
|
|
|
|
|
325
|
2 |
|
if ($phoneMetadata === null) { |
|
326
|
|
|
continue; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
2 |
|
if ($this->matcherAPI->matchesPossibleNumber($shortNumber, $phoneMetadata->getGeneralDesc())) { |
|
330
|
2 |
|
return true; |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
2 |
|
return false; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Check whether a short number is a possible number when dialled from a region, given the number |
|
339
|
|
|
* in the form of a string, and the region where the number is dialled from. This provides a more |
|
340
|
|
|
* lenient check than {@link #isValidShortNumber}. |
|
341
|
|
|
* |
|
342
|
|
|
* @param PhoneNumber|string $shortNumber The short number to check |
|
343
|
|
|
* @param string $regionDialingFrom Region dialing From |
|
344
|
|
|
* @return boolean whether the number is a possible short number |
|
345
|
|
|
*/ |
|
346
|
267 |
|
public function isPossibleShortNumberForRegion($shortNumber, $regionDialingFrom) |
|
347
|
|
|
{ |
|
348
|
267 |
|
if ($shortNumber instanceof PhoneNumber) { |
|
349
|
267 |
|
if (!$this->regionDialingFromMatchesNumber($shortNumber, $regionDialingFrom)) { |
|
350
|
1 |
|
return false; |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
266 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
|
354
|
|
|
|
|
355
|
266 |
|
if ($phoneMetadata === null) { |
|
356
|
|
|
return false; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
266 |
|
if ($shortNumber instanceof PhoneNumber) { |
|
360
|
266 |
|
return $this->matcherAPI->matchesPossibleNumber( |
|
361
|
266 |
|
$this->getNationalSignificantNumber($shortNumber), |
|
362
|
266 |
|
$phoneMetadata->getGeneralDesc() |
|
363
|
|
|
); |
|
364
|
|
|
} else { |
|
365
|
|
|
/** |
|
366
|
|
|
* @deprecated Anyone who was using it and passing in a string with whitespace (or other |
|
367
|
|
|
* formatting characters) would have been getting the wrong result. You should parse |
|
368
|
|
|
* the string to PhoneNumber and use the method |
|
369
|
|
|
* {@code #isPossibleShortNumberForRegion(PhoneNumber, String)}. This method will be |
|
370
|
|
|
* removed in the next release. |
|
371
|
|
|
*/ |
|
372
|
|
|
|
|
373
|
|
|
return $this->matcherAPI->matchesPossibleNumber($shortNumber, $phoneMetadata->getGeneralDesc()); |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Tests whether a short number matches a valid pattern. If a country calling code is shared by |
|
379
|
|
|
* multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify |
|
380
|
|
|
* the number is actually in use, which is impossible to tell by just looking at the number |
|
381
|
|
|
* itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details. |
|
382
|
|
|
* |
|
383
|
|
|
* @param $number PhoneNumber the short number for which we want to test the validity |
|
384
|
|
|
* @return boolean whether the short number matches a valid pattern |
|
385
|
|
|
*/ |
|
386
|
242 |
|
public function isValidShortNumber(PhoneNumber $number) |
|
387
|
|
|
{ |
|
388
|
242 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
389
|
242 |
|
$regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes); |
|
390
|
242 |
|
if (count($regionCodes) > 1 && $regionCode !== null) { |
|
391
|
|
|
// If a matching region had been found for the phone number from among two or more regions, |
|
392
|
|
|
// then we have already implicitly verified its validity for that region. |
|
393
|
53 |
|
return true; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
190 |
|
return $this->isValidShortNumberForRegion($number, $regionCode); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify |
|
401
|
|
|
* the number is actually in use, which is impossible to tell by just looking at the number |
|
402
|
|
|
* itself. |
|
403
|
|
|
* |
|
404
|
|
|
* @param PhoneNumber|string $number The Short number for which we want to test the validity |
|
405
|
|
|
* @param string $regionDialingFrom the region from which the number is dialed |
|
406
|
|
|
* @return boolean whether the short number matches a valid pattern |
|
407
|
|
|
*/ |
|
408
|
243 |
|
public function isValidShortNumberForRegion($number, $regionDialingFrom) |
|
409
|
|
|
{ |
|
410
|
243 |
|
if ($number instanceof PhoneNumber) { |
|
411
|
243 |
|
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) { |
|
412
|
1 |
|
return false; |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
242 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
|
416
|
|
|
|
|
417
|
242 |
|
if ($phoneMetadata === null) { |
|
418
|
|
|
return false; |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
242 |
|
if ($number instanceof PhoneNumber) { |
|
422
|
242 |
|
$shortNumber = $this->getNationalSignificantNumber($number); |
|
423
|
|
|
} else { |
|
424
|
|
|
/** |
|
425
|
|
|
* @deprecated Anyone who was using it and passing in a string with whitespace (or other |
|
426
|
|
|
* formatting characters) would have been getting the wrong result. You should parse |
|
427
|
|
|
* the string to PhoneNumber and use the method |
|
428
|
|
|
* {@code #isValidShortNumberForRegion(PhoneNumber, String)}. This method will be |
|
429
|
|
|
* removed in the next release. |
|
430
|
|
|
*/ |
|
431
|
|
|
$shortNumber = $number; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
242 |
|
$generalDesc = $phoneMetadata->getGeneralDesc(); |
|
435
|
|
|
|
|
436
|
242 |
|
if (!$this->matchesPossibleNumberAndNationalNumber($shortNumber, $generalDesc)) { |
|
437
|
1 |
|
return false; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
242 |
|
$shortNumberDesc = $phoneMetadata->getShortCode(); |
|
441
|
|
|
|
|
442
|
242 |
|
return $this->matchesPossibleNumberAndNationalNumber($shortNumber, $shortNumberDesc); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* Gets the expected cost category of a short number when dialled from a region (however, nothing is |
|
447
|
|
|
* implied about its validity). If it is important that the number is valid, then its validity |
|
448
|
|
|
* must first be checked using {@link isValidShortNumberForRegion}. Note that emergency numbers |
|
449
|
|
|
* are always considered toll-free. |
|
450
|
|
|
* Example usage: |
|
451
|
|
|
* <pre>{@code |
|
452
|
|
|
* $shortInfo = ShortNumberInfo::getInstance(); |
|
453
|
|
|
* $shortNumber = "110"; |
|
454
|
|
|
* $regionCode = "FR"; |
|
455
|
|
|
* if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) { |
|
456
|
|
|
* $cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode); |
|
457
|
|
|
* // Do something with the cost information here. |
|
458
|
|
|
* }}</pre> |
|
459
|
|
|
* |
|
460
|
|
|
* @param PhoneNumber|string $number the short number for which we want to know the expected cost category, |
|
461
|
|
|
* as a string |
|
462
|
|
|
* @param string $regionDialingFrom the region from which the number is dialed |
|
463
|
|
|
* @return int the expected cost category for that region of the short number. Returns UNKNOWN_COST if |
|
464
|
|
|
* the number does not match a cost category. Note that an invalid number may match any cost |
|
465
|
|
|
* category. |
|
466
|
|
|
*/ |
|
467
|
314 |
|
public function getExpectedCostForRegion($number, $regionDialingFrom) |
|
468
|
|
|
{ |
|
469
|
314 |
|
if ($number instanceof PhoneNumber) { |
|
470
|
314 |
|
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) { |
|
471
|
2 |
|
return ShortNumberCost::UNKNOWN_COST; |
|
472
|
|
|
} |
|
473
|
|
|
} |
|
474
|
|
|
// Note that regionDialingFrom may be null, in which case phoneMetadata will also be null. |
|
475
|
313 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
|
476
|
313 |
|
if ($phoneMetadata === null) { |
|
477
|
|
|
return ShortNumberCost::UNKNOWN_COST; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
313 |
|
if ($number instanceof PhoneNumber) { |
|
481
|
313 |
|
$shortNumber = $this->getNationalSignificantNumber($number); |
|
482
|
|
|
} else { |
|
483
|
|
|
/** |
|
484
|
|
|
* @deprecated Anyone who was using it and passing in a string with whitespace (or other |
|
485
|
|
|
* formatting characters) would have been getting the wrong result. You should parse |
|
486
|
|
|
* the string to PhoneNumber and use the method |
|
487
|
|
|
* {@code #getExpectedCostForRegion(PhoneNumber, String)}. This method will be |
|
488
|
|
|
* removed in the next release. |
|
489
|
|
|
*/ |
|
490
|
|
|
$shortNumber = $number; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
// The cost categories are tested in order of decreasing expense, since if for some reason the |
|
494
|
|
|
// patterns overlap the most expensive matching cost category should be returned. |
|
495
|
313 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getPremiumRate())) { |
|
496
|
14 |
|
return ShortNumberCost::PREMIUM_RATE; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
301 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getStandardRate())) { |
|
500
|
16 |
|
return ShortNumberCost::STANDARD_RATE; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
287 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getTollFree())) { |
|
504
|
50 |
|
return ShortNumberCost::TOLL_FREE; |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
240 |
|
if ($this->isEmergencyNumber($shortNumber, $regionDialingFrom)) { |
|
508
|
|
|
// Emergency numbers are implicitly toll-free. |
|
509
|
238 |
|
return ShortNumberCost::TOLL_FREE; |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
3 |
|
return ShortNumberCost::UNKNOWN_COST; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Gets the expected cost category of a short number (however, nothing is implied about its |
|
517
|
|
|
* validity). If the country calling code is unique to a region, this method behaves exactly the |
|
518
|
|
|
* same as {@link #getExpectedCostForRegion(PhoneNumber, String)}. However, if the country calling |
|
519
|
|
|
* code is shared by multiple regions, then it returns the highest cost in the sequence |
|
520
|
|
|
* PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of |
|
521
|
|
|
* UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE |
|
522
|
|
|
* or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it |
|
523
|
|
|
* might be a PREMIUM_RATE number. |
|
524
|
|
|
* |
|
525
|
|
|
* <p> |
|
526
|
|
|
* For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected |
|
527
|
|
|
* cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same |
|
528
|
|
|
* country calling code. |
|
529
|
|
|
* </p> |
|
530
|
|
|
* |
|
531
|
|
|
* Note: If the region from which the number is dialed is known, it is highly preferable to call |
|
532
|
|
|
* {@link #getExpectedCostForRegion(PhoneNumber, String)} instead. |
|
533
|
|
|
* |
|
534
|
|
|
* @param PhoneNumber $number the short number for which we want to know the expected cost category |
|
535
|
|
|
* @return int the highest expected cost category of the short number in the region(s) with the given |
|
536
|
|
|
* country calling code |
|
537
|
|
|
*/ |
|
538
|
3 |
|
public function getExpectedCost(PhoneNumber $number) |
|
539
|
|
|
{ |
|
540
|
3 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
541
|
3 |
|
if (count($regionCodes) == 0) { |
|
542
|
1 |
|
return ShortNumberCost::UNKNOWN_COST; |
|
543
|
|
|
} |
|
544
|
3 |
|
if (count($regionCodes) == 1) { |
|
545
|
1 |
|
return $this->getExpectedCostForRegion($number, $regionCodes[0]); |
|
546
|
|
|
} |
|
547
|
2 |
|
$cost = ShortNumberCost::TOLL_FREE; |
|
548
|
2 |
|
foreach ($regionCodes as $regionCode) { |
|
549
|
2 |
|
$costForRegion = $this->getExpectedCostForRegion($number, $regionCode); |
|
550
|
|
|
switch ($costForRegion) { |
|
551
|
2 |
|
case ShortNumberCost::PREMIUM_RATE: |
|
552
|
1 |
|
return ShortNumberCost::PREMIUM_RATE; |
|
553
|
|
|
|
|
554
|
2 |
|
case ShortNumberCost::UNKNOWN_COST: |
|
555
|
1 |
|
$cost = ShortNumberCost::UNKNOWN_COST; |
|
556
|
1 |
|
break; |
|
557
|
|
|
|
|
558
|
2 |
|
case ShortNumberCost::STANDARD_RATE: |
|
559
|
1 |
|
if ($cost != ShortNumberCost::UNKNOWN_COST) { |
|
560
|
1 |
|
$cost = ShortNumberCost::STANDARD_RATE; |
|
561
|
|
|
} |
|
562
|
1 |
|
break; |
|
563
|
2 |
|
case ShortNumberCost::TOLL_FREE: |
|
564
|
|
|
// Do nothing |
|
565
|
2 |
|
break; |
|
566
|
|
|
} |
|
567
|
|
|
} |
|
568
|
2 |
|
return $cost; |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
/** |
|
572
|
|
|
* Returns true if the given number exactly matches an emergency service number in the given |
|
573
|
|
|
* region. |
|
574
|
|
|
* <p> |
|
575
|
|
|
* This method takes into account cases where the number might contain formatting, but doesn't |
|
576
|
|
|
* allow additional digits to be appended. Note that {@code isEmergencyNumber(number, region)} |
|
577
|
|
|
* implies {@code connectsToEmergencyNumber(number, region)}. |
|
578
|
|
|
* |
|
579
|
|
|
* @param string $number the phone number to test |
|
580
|
|
|
* @param string $regionCode the region where the phone number is being dialled |
|
581
|
|
|
* @return boolean whether the number exactly matches an emergency services number in the given region |
|
582
|
|
|
*/ |
|
583
|
260 |
|
public function isEmergencyNumber($number, $regionCode) |
|
584
|
|
|
{ |
|
585
|
260 |
|
return $this->matchesEmergencyNumberHelper($number, $regionCode, false /* doesn't allow prefix match */); |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* Gets the national significant number of the a phone number. Note a national significant number |
|
590
|
|
|
* doesn't contain a national prefix or any formatting. |
|
591
|
|
|
* <p> |
|
592
|
|
|
* This is a temporary duplicate of the {@code getNationalSignificantNumber} method from |
|
593
|
|
|
* {@code PhoneNumberUtil}. Ultimately a canonical static version should exist in a separate |
|
594
|
|
|
* utility class (to prevent {@code ShortNumberInfo} needing to depend on PhoneNumberUtil). |
|
595
|
|
|
* |
|
596
|
|
|
* @param PhoneNumber $number the phone number for which the national significant number is needed |
|
597
|
|
|
* @return string the national significant number of the PhoneNumber object passed in |
|
598
|
|
|
*/ |
|
599
|
582 |
|
protected function getNationalSignificantNumber(PhoneNumber $number) |
|
600
|
|
|
{ |
|
601
|
|
|
// If leading zero(s) have been set, we prefix this now. Note this is not a national prefix. |
|
602
|
582 |
|
$nationalNumber = ''; |
|
603
|
582 |
|
if ($number->isItalianLeadingZero()) { |
|
604
|
10 |
|
$zeros = str_repeat('0', $number->getNumberOfLeadingZeros()); |
|
605
|
10 |
|
$nationalNumber .= $zeros; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
582 |
|
$nationalNumber .= $number->getNationalNumber(); |
|
609
|
|
|
|
|
610
|
582 |
|
return $nationalNumber; |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
/** |
|
614
|
|
|
* // TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping |
|
615
|
|
|
* this performance optimization, and if so move this into the matcher implementation |
|
616
|
|
|
* @param string $number |
|
617
|
|
|
* @param PhoneNumberDesc $numberDesc |
|
618
|
|
|
* @return bool |
|
619
|
|
|
*/ |
|
620
|
579 |
|
protected function matchesPossibleNumberAndNationalNumber($number, PhoneNumberDesc $numberDesc) |
|
621
|
|
|
{ |
|
622
|
579 |
|
return ($this->matcherAPI->matchesPossibleNumber($number, $numberDesc) |
|
623
|
579 |
|
&& $this->matcherAPI->matchesNationalNumber($number, $numberDesc, false)); |
|
624
|
|
|
} |
|
625
|
|
|
} |
|
626
|
|
|
|