1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace libphonenumber\prefixmapper; |
4
|
|
|
|
5
|
|
|
use libphonenumber\PhoneNumber; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A helper class doing file handling and lookup of phone number prefix mappings. |
9
|
|
|
* |
10
|
|
|
* @package libphonenumber\prefixmapper |
11
|
|
|
*/ |
12
|
|
|
class PrefixFileReader |
13
|
|
|
{ |
14
|
|
|
protected $phonePrefixDataDirectory; |
15
|
|
|
/** |
16
|
|
|
* The mappingFileProvider knows for which combination of countryCallingCode and language a phone |
17
|
|
|
* prefix mapping file is available in the file system, so that a file can be loaded when needed. |
18
|
|
|
* @var MappingFileProvider |
19
|
|
|
*/ |
20
|
|
|
protected $mappingFileProvider; |
21
|
|
|
/** |
22
|
|
|
* A mapping from countryCallingCode_lang to the corresponding phone prefix map that has been |
23
|
|
|
* loaded. |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $availablePhonePrefixMaps = array(); |
27
|
|
|
|
28
|
252 |
|
public function __construct($phonePrefixDataDirectory) |
29
|
|
|
{ |
30
|
252 |
|
$this->phonePrefixDataDirectory = $phonePrefixDataDirectory; |
31
|
|
|
$this->loadMappingFileProvider(); |
32
|
252 |
|
} |
33
|
|
|
|
34
|
252 |
|
protected function loadMappingFileProvider() |
35
|
|
|
{ |
36
|
252 |
|
$mapPath = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . "Map.php"; |
37
|
|
|
if (!file_exists($mapPath)) { |
38
|
|
|
throw new \InvalidArgumentException("Invalid data directory"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$map = require $mapPath; |
42
|
|
|
|
43
|
|
|
$this->mappingFileProvider = new MappingFileProvider($map); |
44
|
252 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $prefixMapKey |
49
|
|
|
* @param $language |
50
|
|
|
* @param $script |
51
|
|
|
* @param $region |
52
|
|
|
* @return PhonePrefixMap|null |
53
|
|
|
*/ |
54
|
6 |
|
public function getPhonePrefixDescriptions($prefixMapKey, $language, $script, $region) |
55
|
|
|
{ |
56
|
|
|
$fileName = $this->mappingFileProvider->getFileName($prefixMapKey, $language, $script, $region); |
57
|
|
|
if (strlen($fileName) == 0) { |
58
|
3 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
if (!isset($this->availablePhonePrefixMaps[$fileName])) { |
62
|
|
|
$this->loadPhonePrefixMapFromFile($fileName); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
return $this->availablePhonePrefixMaps[$fileName]; |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
protected function loadPhonePrefixMapFromFile($fileName) |
69
|
|
|
{ |
70
|
12 |
|
$path = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . $fileName; |
71
|
|
|
if (!file_exists($path)) { |
72
|
|
|
throw new \InvalidArgumentException("Data does not exist"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$map = require $path; |
76
|
|
|
$areaCodeMap = new PhonePrefixMap($map); |
77
|
|
|
|
78
|
12 |
|
$this->availablePhonePrefixMaps[$fileName] = $areaCodeMap; |
79
|
12 |
|
} |
80
|
|
|
|
81
|
10 |
|
public function mayFallBackToEnglish($language) |
82
|
|
|
{ |
83
|
|
|
// Don't fall back to English if the requested language is among the following: |
84
|
|
|
// - Chinese |
85
|
|
|
// - Japanese |
86
|
|
|
// - Korean |
87
|
10 |
|
return ($language != 'zh' && $language != 'ja' && $language != 'ko'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a text description in the given language for the given phone number. |
92
|
|
|
* |
93
|
|
|
* @param PhoneNumber $number the phone number for which we want to get a text description |
94
|
|
|
* @param string $language two-letter lowercase ISO language codes as defined by ISO 639-1 |
95
|
|
|
* @param string $script four-letter titlecase (the first letter is uppercase and the rest of the letters |
96
|
|
|
* are lowercase) ISO script codes as defined in ISO 15924 |
97
|
|
|
* @param string $region two-letter uppercase ISO country codes as defined by ISO 3166-1 |
98
|
|
|
* @return string a text description in the given language for the given phone number, or an empty |
99
|
|
|
* string if a description is not available |
100
|
|
|
*/ |
101
|
13 |
|
public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region) |
102
|
|
|
{ |
103
|
|
|
$countryCallingCode = $number->getCountryCode(); |
104
|
|
|
// As the NANPA data is split into multiple files covering 3-digit areas, use a phone number |
105
|
|
|
// prefix of 4 digits for NANPA instead, e.g. 1650. |
106
|
13 |
|
if ($countryCallingCode === 1) { |
107
|
|
|
$phonePrefix = (1000 + intval($number->getNationalNumber() / 10000000)); |
108
|
11 |
|
} elseif ($countryCallingCode === 86) { |
109
|
|
|
// Split China into multiple files to reduce PHP memory usage |
110
|
|
|
// @see https://github.com/giggsey/libphonenumber-for-php/issues/44 |
111
|
|
|
$phonePrefix = '86' . substr($number->getNationalNumber(), 0, 3); |
112
|
|
|
} else { |
113
|
7 |
|
$phonePrefix = $countryCallingCode; |
114
|
8 |
|
} |
115
|
|
|
|
116
|
|
|
$phonePrefixDescriptions = $this->getPhonePrefixDescriptions($phonePrefix, $language, $script, $region); |
117
|
|
|
|
118
|
2 |
|
$description = ($phonePrefixDescriptions !== null) ? $phonePrefixDescriptions->lookup($number) : null; |
119
|
|
|
// When a location is not available in the requested language, fall back to English. |
120
|
|
|
if (($description === null || strlen($description) === 0) && $this->mayFallBackToEnglish($language)) { |
121
|
|
|
$defaultMap = $this->getPhonePrefixDescriptions($phonePrefix, "en", "", ""); |
122
|
9 |
|
if ($defaultMap === null) { |
123
|
1 |
|
return ""; |
124
|
|
|
} |
125
|
|
|
$description = $defaultMap->lookup($number); |
126
|
|
|
} |
127
|
|
|
|
128
|
13 |
|
return ($description !== null) ? $description : ""; |
129
|
13 |
|
} |
130
|
|
|
} |
131
|
|
|
|