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
|
29 |
|
protected function __construct(MatcherAPIInterface $matcherAPI) |
36
|
|
|
{ |
37
|
29 |
|
$this->matcherAPI = $matcherAPI; |
38
|
|
|
|
39
|
|
|
// TODO: Create ShortNumberInfo for a given map |
40
|
29 |
|
$this->countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap; |
41
|
|
|
|
42
|
29 |
|
$this->currentFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX; |
43
|
|
|
|
44
|
|
|
// Initialise PhoneNumberUtil to make sure regex's are setup correctly |
45
|
29 |
|
PhoneNumberUtil::getInstance(); |
46
|
29 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the singleton instance of ShortNumberInfo |
50
|
|
|
* |
51
|
|
|
* @return \libphonenumber\ShortNumberInfo |
52
|
|
|
*/ |
53
|
5373 |
|
public static function getInstance() |
54
|
|
|
{ |
55
|
5373 |
|
if (null === static::$instance) { |
56
|
29 |
|
static::$instance = new self(RegexBasedMatcher::create()); |
57
|
|
|
} |
58
|
|
|
|
59
|
5373 |
|
return static::$instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
28 |
|
public static function resetInstance() |
63
|
|
|
{ |
64
|
28 |
|
static::$instance = null; |
65
|
28 |
|
} |
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
|
634 |
|
protected function getRegionCodesForCountryCode($countryCallingCode) |
76
|
|
|
{ |
77
|
634 |
|
if (!array_key_exists($countryCallingCode, $this->countryCallingCodeToRegionCodeMap)) { |
78
|
1 |
|
$regionCodes = null; |
79
|
|
|
} else { |
80
|
634 |
|
$regionCodes = $this->countryCallingCodeToRegionCodeMap[$countryCallingCode]; |
81
|
|
|
} |
82
|
|
|
|
83
|
634 |
|
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
|
633 |
|
protected function regionDialingFromMatchesNumber(PhoneNumber $number, $regionDialingFrom) |
94
|
|
|
{ |
95
|
633 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
96
|
|
|
|
97
|
633 |
|
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
|
1934 |
|
public function getMetadataForRegion($regionCode) |
132
|
|
|
{ |
133
|
1934 |
|
if (!in_array($regionCode, ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet)) { |
134
|
1 |
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
1934 |
|
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
|
259 |
|
$this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0); |
141
|
|
|
} |
142
|
|
|
|
143
|
1934 |
|
return isset($this->regionToMetadataMap[$regionCode]) ? $this->regionToMetadataMap[$regionCode] : null; |
144
|
|
|
} |
145
|
|
|
|
146
|
259 |
|
protected function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode) |
147
|
|
|
{ |
148
|
259 |
|
$isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode; |
149
|
259 |
|
$fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php'; |
150
|
259 |
|
if (!is_readable($fileName)) { |
151
|
|
|
throw new \Exception('missing metadata: ' . $fileName); |
152
|
|
|
} else { |
153
|
259 |
|
$data = include $fileName; |
154
|
259 |
|
$metadata = new PhoneMetadata(); |
155
|
259 |
|
$metadata->fromArray($data); |
156
|
259 |
|
if ($isNonGeoRegion) { |
157
|
|
|
$this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata; |
158
|
|
|
} else { |
159
|
259 |
|
$this->regionToMetadataMap[$regionCode] = $metadata; |
160
|
|
|
} |
161
|
|
|
} |
162
|
259 |
|
} |
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
|
85 |
|
return $desc->getExampleNumber(); |
198
|
|
|
} |
199
|
|
|
|
200
|
870 |
|
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
|
10 |
|
public function connectsToEmergencyNumber($number, $regionCode) |
218
|
|
|
{ |
219
|
10 |
|
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
|
262 |
|
protected function matchesEmergencyNumberHelper($number, $regionCode, $allowPrefixMatch) |
229
|
|
|
{ |
230
|
262 |
|
$number = PhoneNumberUtil::extractPossibleNumber($number); |
231
|
262 |
|
$matcher = new Matcher(PhoneNumberUtil::$PLUS_CHARS_PATTERN, $number); |
232
|
262 |
|
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
|
2 |
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
260 |
|
$metadata = $this->getMetadataForRegion($regionCode); |
240
|
260 |
|
if ($metadata === null || !$metadata->hasEmergency()) { |
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
260 |
|
$normalizedNumber = PhoneNumberUtil::normalizeDigitsOnly($number); |
245
|
260 |
|
$emergencyDesc = $metadata->getEmergency(); |
246
|
|
|
|
247
|
260 |
|
$allowPrefixMatchForRegion = ($allowPrefixMatch |
248
|
260 |
|
&& !in_array($regionCode, static::$regionsWhereEmergencyNumbersMustBeExact) |
249
|
|
|
); |
250
|
|
|
|
251
|
260 |
|
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). Carrier-specific numbers may connect to a different end-point, or |
257
|
|
|
* not connect at all, depending on the user's carrier. If it is important that the number is |
258
|
|
|
* valid, then its validity must first be checked using {@link #isValidShortNumber} or |
259
|
|
|
* {@link #isValidShortNumberForRegion}. |
260
|
|
|
* |
261
|
|
|
* @param PhoneNumber $number the valid short number to check |
262
|
|
|
* @return boolean whether the short number is carrier-specific (assuming the input was a valid short |
263
|
|
|
* number). |
264
|
|
|
*/ |
265
|
1 |
|
public function isCarrierSpecific(PhoneNumber $number) |
266
|
|
|
{ |
267
|
1 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
268
|
1 |
|
$regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes); |
269
|
1 |
|
$nationalNumber = $this->getNationalSignificantNumber($number); |
270
|
1 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
271
|
|
|
|
272
|
1 |
|
return ($phoneMetadata !== null) && ($this->matchesPossibleNumberAndNationalNumber( |
273
|
|
|
$nationalNumber, |
274
|
1 |
|
$phoneMetadata->getCarrierSpecific() |
275
|
|
|
)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Given a valid short number, determines whether it is carrier-specific when dialed from the |
280
|
|
|
* given region (however, nothing is implied about its validity). Carrier-specific numbers may |
281
|
|
|
* connect to a different end-point, or not connect at all, depending on the user's carrier. If |
282
|
|
|
* it is important that the number is valid, then its validity must first be checked using |
283
|
|
|
* {@link #isValidShortNumber} or {@link #isValidShortNumberForRegion}. Returns false if the |
284
|
|
|
* number doesn't match the region provided. |
285
|
|
|
* @param PhoneNumber $number The valid short number to check |
286
|
|
|
* @param string $regionDialingFrom The region from which the number is dialed |
287
|
|
|
* @return bool Whether the short number is carrier-specific (Assuming the input was a valid short |
288
|
|
|
* number) |
289
|
|
|
*/ |
290
|
67 |
|
public function isCarrierSpecificForRegion(PhoneNumber $number, $regionDialingFrom) |
291
|
|
|
{ |
292
|
67 |
|
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) { |
293
|
|
|
return false; |
294
|
|
|
} |
295
|
|
|
|
296
|
67 |
|
$nationalNumber = $this->getNationalSignificantNumber($number); |
297
|
67 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
298
|
|
|
|
299
|
67 |
|
return ($phoneMetadata !== null) |
300
|
67 |
|
&& ($this->matchesPossibleNumberAndNationalNumber($nationalNumber, $phoneMetadata->getCarrierSpecific())); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Helper method to get the region code for a given phone number, from a list of possible region |
305
|
|
|
* codes. If the list contains more than one region, the first region for which the number is |
306
|
|
|
* valid is returned. |
307
|
|
|
* |
308
|
|
|
* @param PhoneNumber $number |
309
|
|
|
* @param $regionCodes |
310
|
|
|
* @return String|null Region Code (or null if none are found) |
311
|
|
|
*/ |
312
|
243 |
|
protected function getRegionCodeForShortNumberFromRegionList(PhoneNumber $number, $regionCodes) |
313
|
|
|
{ |
314
|
243 |
|
if (count($regionCodes) == 0) { |
315
|
|
|
return null; |
316
|
243 |
|
} elseif (count($regionCodes) == 1) { |
317
|
190 |
|
return $regionCodes[0]; |
318
|
|
|
} |
319
|
|
|
|
320
|
54 |
|
$nationalNumber = $this->getNationalSignificantNumber($number); |
321
|
|
|
|
322
|
54 |
|
foreach ($regionCodes as $regionCode) { |
323
|
54 |
|
$phoneMetadata = $this->getMetadataForRegion($regionCode); |
324
|
54 |
|
if ($phoneMetadata !== null |
325
|
54 |
|
&& $this->matchesPossibleNumberAndNationalNumber($nationalNumber, $phoneMetadata->getShortCode()) |
326
|
|
|
) { |
327
|
|
|
// The number is valid for this region. |
328
|
54 |
|
return $regionCode; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
return null; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Check whether a short number is a possible number. If a country calling code is shared by |
336
|
|
|
* multiple regions, this returns true if it's possible in any of them. This provides a more |
337
|
|
|
* lenient check than {@link #isValidShortNumber}. See {@link |
338
|
|
|
* #IsPossibleShortNumberForRegion(PhoneNumber, String)} for details. |
339
|
|
|
* |
340
|
|
|
* @param $number PhoneNumber the short number to check |
341
|
|
|
* @return boolean whether the number is a possible short number |
342
|
|
|
*/ |
343
|
2 |
|
public function isPossibleShortNumber(PhoneNumber $number) |
344
|
|
|
{ |
345
|
2 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
346
|
2 |
|
$shortNumberLength = strlen($this->getNationalSignificantNumber($number)); |
347
|
|
|
|
348
|
2 |
|
foreach ($regionCodes as $region) { |
349
|
2 |
|
$phoneMetadata = $this->getMetadataForRegion($region); |
350
|
|
|
|
351
|
2 |
|
if ($phoneMetadata === null) { |
352
|
|
|
continue; |
353
|
|
|
} |
354
|
|
|
|
355
|
2 |
|
if (in_array($shortNumberLength, $phoneMetadata->getGeneralDesc()->getPossibleLength())) { |
356
|
2 |
|
return true; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
2 |
|
return false; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Check whether a short number is a possible number when dialled from a region, given the number |
365
|
|
|
* in the form of a string, and the region where the number is dialled from. This provides a more |
366
|
|
|
* lenient check than {@link #isValidShortNumber}. |
367
|
|
|
* |
368
|
|
|
* @param PhoneNumber $shortNumber The short number to check |
369
|
|
|
* @param string $regionDialingFrom Region dialing From |
370
|
|
|
* @return boolean whether the number is a possible short number |
371
|
|
|
*/ |
372
|
306 |
|
public function isPossibleShortNumberForRegion(PhoneNumber $shortNumber, $regionDialingFrom) |
373
|
|
|
{ |
374
|
306 |
|
if (!$this->regionDialingFromMatchesNumber($shortNumber, $regionDialingFrom)) { |
375
|
1 |
|
return false; |
376
|
|
|
} |
377
|
|
|
|
378
|
305 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
379
|
|
|
|
380
|
305 |
|
if ($phoneMetadata === null) { |
381
|
|
|
return false; |
382
|
|
|
} |
383
|
|
|
|
384
|
305 |
|
$numberLength = strlen($this->getNationalSignificantNumber($shortNumber)); |
385
|
305 |
|
return in_array($numberLength, $phoneMetadata->getGeneralDesc()->getPossibleLength()); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Tests whether a short number matches a valid pattern. If a country calling code is shared by |
390
|
|
|
* multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify |
391
|
|
|
* the number is actually in use, which is impossible to tell by just looking at the number |
392
|
|
|
* itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details. |
393
|
|
|
* |
394
|
|
|
* @param $number PhoneNumber the short number for which we want to test the validity |
395
|
|
|
* @return boolean whether the short number matches a valid pattern |
396
|
|
|
*/ |
397
|
242 |
|
public function isValidShortNumber(PhoneNumber $number) |
398
|
|
|
{ |
399
|
242 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
400
|
242 |
|
$regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes); |
401
|
242 |
|
if (count($regionCodes) > 1 && $regionCode !== null) { |
402
|
|
|
// If a matching region had been found for the phone number from among two or more regions, |
403
|
|
|
// then we have already implicitly verified its validity for that region. |
404
|
53 |
|
return true; |
405
|
|
|
} |
406
|
|
|
|
407
|
190 |
|
return $this->isValidShortNumberForRegion($number, $regionCode); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify |
412
|
|
|
* the number is actually in use, which is impossible to tell by just looking at the number |
413
|
|
|
* itself. |
414
|
|
|
* |
415
|
|
|
* @param PhoneNumber $number The Short number for which we want to test the validity |
416
|
|
|
* @param string $regionDialingFrom the region from which the number is dialed |
417
|
|
|
* @return boolean whether the short number matches a valid pattern |
418
|
|
|
*/ |
419
|
243 |
|
public function isValidShortNumberForRegion(PhoneNumber $number, $regionDialingFrom) |
420
|
|
|
{ |
421
|
243 |
|
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) { |
422
|
1 |
|
return false; |
423
|
|
|
} |
424
|
242 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
425
|
|
|
|
426
|
242 |
|
if ($phoneMetadata === null) { |
427
|
|
|
return false; |
428
|
|
|
} |
429
|
|
|
|
430
|
242 |
|
$shortNumber = $this->getNationalSignificantNumber($number); |
431
|
|
|
|
432
|
242 |
|
$generalDesc = $phoneMetadata->getGeneralDesc(); |
433
|
|
|
|
434
|
242 |
|
if (!$this->matchesPossibleNumberAndNationalNumber($shortNumber, $generalDesc)) { |
435
|
1 |
|
return false; |
436
|
|
|
} |
437
|
|
|
|
438
|
242 |
|
$shortNumberDesc = $phoneMetadata->getShortCode(); |
439
|
|
|
|
440
|
242 |
|
return $this->matchesPossibleNumberAndNationalNumber($shortNumber, $shortNumberDesc); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Gets the expected cost category of a short number when dialled from a region (however, nothing is |
445
|
|
|
* implied about its validity). If it is important that the number is valid, then its validity |
446
|
|
|
* must first be checked using {@link isValidShortNumberForRegion}. Note that emergency numbers |
447
|
|
|
* are always considered toll-free. |
448
|
|
|
* Example usage: |
449
|
|
|
* <pre>{@code |
450
|
|
|
* $shortInfo = ShortNumberInfo::getInstance(); |
451
|
|
|
* $shortNumber = PhoneNumberUtil::parse("110", "US); |
452
|
|
|
* $regionCode = "FR"; |
453
|
|
|
* if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) { |
454
|
|
|
* $cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode); |
455
|
|
|
* // Do something with the cost information here. |
456
|
|
|
* }}</pre> |
457
|
|
|
* |
458
|
|
|
* @param PhoneNumber $number the short number for which we want to know the expected cost category, |
459
|
|
|
* as a string |
460
|
|
|
* @param string $regionDialingFrom the region from which the number is dialed |
461
|
|
|
* @return int the expected cost category for that region of the short number. Returns UNKNOWN_COST if |
462
|
|
|
* the number does not match a cost category. Note that an invalid number may match any cost |
463
|
|
|
* category. |
464
|
|
|
*/ |
465
|
325 |
|
public function getExpectedCostForRegion(PhoneNumber $number, $regionDialingFrom) |
466
|
|
|
{ |
467
|
325 |
|
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) { |
468
|
2 |
|
return ShortNumberCost::UNKNOWN_COST; |
469
|
|
|
} |
470
|
|
|
// Note that regionDialingFrom may be null, in which case phoneMetadata will also be null. |
471
|
324 |
|
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom); |
472
|
324 |
|
if ($phoneMetadata === null) { |
473
|
|
|
return ShortNumberCost::UNKNOWN_COST; |
474
|
|
|
} |
475
|
|
|
|
476
|
324 |
|
$shortNumber = $this->getNationalSignificantNumber($number); |
477
|
|
|
|
478
|
|
|
// The possible lengths are not present for a particular sub-type if they match the general |
479
|
|
|
// description; for this reason, we check the possible lengths against the general description |
480
|
|
|
// first to allow an early exit if possible. |
481
|
324 |
|
if (!in_array(strlen($shortNumber), $phoneMetadata->getGeneralDesc()->getPossibleLength())) { |
482
|
1 |
|
return ShortNumberCost::UNKNOWN_COST; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
// The cost categories are tested in order of decreasing expense, since if for some reason the |
486
|
|
|
// patterns overlap the most expensive matching cost category should be returned. |
487
|
324 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getPremiumRate())) { |
488
|
18 |
|
return ShortNumberCost::PREMIUM_RATE; |
489
|
|
|
} |
490
|
|
|
|
491
|
308 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getStandardRate())) { |
492
|
20 |
|
return ShortNumberCost::STANDARD_RATE; |
493
|
|
|
} |
494
|
|
|
|
495
|
290 |
|
if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getTollFree())) { |
496
|
54 |
|
return ShortNumberCost::TOLL_FREE; |
497
|
|
|
} |
498
|
|
|
|
499
|
239 |
|
if ($this->isEmergencyNumber($shortNumber, $regionDialingFrom)) { |
500
|
|
|
// Emergency numbers are implicitly toll-free. |
501
|
237 |
|
return ShortNumberCost::TOLL_FREE; |
502
|
|
|
} |
503
|
|
|
|
504
|
3 |
|
return ShortNumberCost::UNKNOWN_COST; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Gets the expected cost category of a short number (however, nothing is implied about its |
509
|
|
|
* validity). If the country calling code is unique to a region, this method behaves exactly the |
510
|
|
|
* same as {@link #getExpectedCostForRegion(PhoneNumber, String)}. However, if the country calling |
511
|
|
|
* code is shared by multiple regions, then it returns the highest cost in the sequence |
512
|
|
|
* PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of |
513
|
|
|
* UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE |
514
|
|
|
* or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it |
515
|
|
|
* might be a PREMIUM_RATE number. |
516
|
|
|
* |
517
|
|
|
* <p> |
518
|
|
|
* For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected |
519
|
|
|
* cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same |
520
|
|
|
* country calling code. |
521
|
|
|
* </p> |
522
|
|
|
* |
523
|
|
|
* Note: If the region from which the number is dialed is known, it is highly preferable to call |
524
|
|
|
* {@link #getExpectedCostForRegion(PhoneNumber, String)} instead. |
525
|
|
|
* |
526
|
|
|
* @param PhoneNumber $number the short number for which we want to know the expected cost category |
527
|
|
|
* @return int the highest expected cost category of the short number in the region(s) with the given |
528
|
|
|
* country calling code |
529
|
|
|
*/ |
530
|
3 |
|
public function getExpectedCost(PhoneNumber $number) |
531
|
|
|
{ |
532
|
3 |
|
$regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
533
|
3 |
|
if (count($regionCodes) == 0) { |
534
|
1 |
|
return ShortNumberCost::UNKNOWN_COST; |
535
|
|
|
} |
536
|
3 |
|
if (count($regionCodes) == 1) { |
537
|
1 |
|
return $this->getExpectedCostForRegion($number, $regionCodes[0]); |
538
|
|
|
} |
539
|
2 |
|
$cost = ShortNumberCost::TOLL_FREE; |
540
|
2 |
|
foreach ($regionCodes as $regionCode) { |
541
|
2 |
|
$costForRegion = $this->getExpectedCostForRegion($number, $regionCode); |
542
|
|
|
switch ($costForRegion) { |
543
|
2 |
|
case ShortNumberCost::PREMIUM_RATE: |
544
|
1 |
|
return ShortNumberCost::PREMIUM_RATE; |
545
|
|
|
|
546
|
2 |
|
case ShortNumberCost::UNKNOWN_COST: |
547
|
1 |
|
$cost = ShortNumberCost::UNKNOWN_COST; |
548
|
1 |
|
break; |
549
|
|
|
|
550
|
2 |
|
case ShortNumberCost::STANDARD_RATE: |
551
|
1 |
|
if ($cost != ShortNumberCost::UNKNOWN_COST) { |
552
|
1 |
|
$cost = ShortNumberCost::STANDARD_RATE; |
553
|
|
|
} |
554
|
1 |
|
break; |
555
|
2 |
|
case ShortNumberCost::TOLL_FREE: |
556
|
|
|
// Do nothing |
557
|
2 |
|
break; |
558
|
|
|
} |
559
|
|
|
} |
560
|
2 |
|
return $cost; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Returns true if the given number exactly matches an emergency service number in the given |
565
|
|
|
* region. |
566
|
|
|
* <p> |
567
|
|
|
* This method takes into account cases where the number might contain formatting, but doesn't |
568
|
|
|
* allow additional digits to be appended. Note that {@code isEmergencyNumber(number, region)} |
569
|
|
|
* implies {@code connectsToEmergencyNumber(number, region)}. |
570
|
|
|
* |
571
|
|
|
* @param string $number the phone number to test |
572
|
|
|
* @param string $regionCode the region where the phone number is being dialled |
573
|
|
|
* @return boolean whether the number exactly matches an emergency services number in the given region |
574
|
|
|
*/ |
575
|
252 |
|
public function isEmergencyNumber($number, $regionCode) |
576
|
|
|
{ |
577
|
252 |
|
return $this->matchesEmergencyNumberHelper($number, $regionCode, false /* doesn't allow prefix match */); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Gets the national significant number of the a phone number. Note a national significant number |
582
|
|
|
* doesn't contain a national prefix or any formatting. |
583
|
|
|
* <p> |
584
|
|
|
* This is a temporary duplicate of the {@code getNationalSignificantNumber} method from |
585
|
|
|
* {@code PhoneNumberUtil}. Ultimately a canonical static version should exist in a separate |
586
|
|
|
* utility class (to prevent {@code ShortNumberInfo} needing to depend on PhoneNumberUtil). |
587
|
|
|
* |
588
|
|
|
* @param PhoneNumber $number the phone number for which the national significant number is needed |
589
|
|
|
* @return string the national significant number of the PhoneNumber object passed in |
590
|
|
|
*/ |
591
|
633 |
|
protected function getNationalSignificantNumber(PhoneNumber $number) |
592
|
|
|
{ |
593
|
|
|
// If leading zero(s) have been set, we prefix this now. Note this is not a national prefix. |
594
|
633 |
|
$nationalNumber = ''; |
595
|
633 |
|
if ($number->isItalianLeadingZero()) { |
596
|
11 |
|
$zeros = str_repeat('0', $number->getNumberOfLeadingZeros()); |
597
|
11 |
|
$nationalNumber .= $zeros; |
598
|
|
|
} |
599
|
|
|
|
600
|
633 |
|
$nationalNumber .= $number->getNationalNumber(); |
601
|
|
|
|
602
|
633 |
|
return $nationalNumber; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping |
607
|
|
|
* this performance optimization. |
608
|
|
|
* @param string $number |
609
|
|
|
* @param PhoneNumberDesc $numberDesc |
610
|
|
|
* @return bool |
611
|
|
|
*/ |
612
|
630 |
|
protected function matchesPossibleNumberAndNationalNumber($number, PhoneNumberDesc $numberDesc) |
613
|
|
|
{ |
614
|
630 |
|
if (count($numberDesc->getPossibleLength()) > 0 && !in_array(strlen($number), $numberDesc->getPossibleLength())) { |
615
|
296 |
|
return false; |
616
|
|
|
} |
617
|
|
|
|
618
|
411 |
|
return $this->matcherAPI->matchesNationalNumber($number, $numberDesc, false); |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|