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