|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace libphonenumber\geocoding; |
|
4
|
|
|
|
|
5
|
|
|
use Giggsey\Locale\Locale; |
|
6
|
|
|
use libphonenumber\NumberParseException; |
|
7
|
|
|
use libphonenumber\PhoneNumber; |
|
8
|
|
|
use libphonenumber\PhoneNumberType; |
|
9
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
10
|
|
|
use libphonenumber\prefixmapper\PrefixFileReader; |
|
11
|
|
|
|
|
12
|
|
|
class PhoneNumberOfflineGeocoder |
|
13
|
|
|
{ |
|
14
|
|
|
const MAPPING_DATA_DIRECTORY = '/data'; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var PhoneNumberOfflineGeocoder |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $instance; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var PhoneNumberUtil |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $phoneUtil; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PrefixFileReader |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $prefixFileReader = null; |
|
27
|
|
|
|
|
28
|
245 |
|
protected function __construct($phonePrefixDataDirectory) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->phoneUtil = PhoneNumberUtil::getInstance(); |
|
31
|
|
|
|
|
32
|
|
|
$this->prefixFileReader = new PrefixFileReader(dirname(__FILE__) . $phonePrefixDataDirectory); |
|
33
|
245 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Gets a PhoneNumberOfflineGeocoder instance to carry out international phone number geocoding. |
|
37
|
|
|
* |
|
38
|
|
|
* <p>The PhoneNumberOfflineGeocoder is implemented as a singleton. Therefore, calling this method |
|
39
|
|
|
* multiple times will only result in one instance being created. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $mappingDir (Optional) Mapping Data Directory |
|
42
|
|
|
* @return PhoneNumberOfflineGeocoder |
|
43
|
|
|
*/ |
|
44
|
250 |
|
public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY) |
|
45
|
|
|
{ |
|
46
|
250 |
|
if (static::$instance === null) { |
|
47
|
|
|
static::$instance = new static($mappingDir); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
250 |
|
return static::$instance; |
|
51
|
241 |
|
} |
|
52
|
|
|
|
|
53
|
245 |
|
public static function resetInstance() |
|
54
|
|
|
{ |
|
55
|
245 |
|
static::$instance = null; |
|
56
|
245 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* As per getDescriptionForValidNumber, but explicitly checks the validity of the number |
|
60
|
|
|
* passed in. |
|
61
|
|
|
* |
|
62
|
|
|
* |
|
63
|
|
|
* @see getDescriptionForValidNumber |
|
64
|
|
|
* @param PhoneNumber $number a valid phone number for which we want to get a text description |
|
65
|
|
|
* @param string $locale the language code for which the description should be written |
|
66
|
|
|
* @param string $userRegion the region code for a given user. This region will be omitted from the |
|
67
|
|
|
* description if the phone number comes from this region. It is a two-letter uppercase ISO |
|
68
|
|
|
* country code as defined by ISO 3166-1. |
|
69
|
|
|
* @return string a text description for the given language code for the given phone number, or empty |
|
70
|
|
|
* string if the number passed in is invalid |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
73
|
|
|
{ |
|
74
|
|
|
$numberType = $this->phoneUtil->getNumberType($number); |
|
75
|
|
|
|
|
76
|
9 |
|
if ($numberType === PhoneNumberType::UNKNOWN) { |
|
77
|
2 |
|
return ""; |
|
78
|
|
|
} elseif (!$this->phoneUtil->isNumberGeographical($numberType, $number->getCountryCode())) { |
|
|
|
|
|
|
79
|
|
|
return $this->getCountryNameForNumber($number, $locale); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->getDescriptionForValidNumber($number, $locale, $userRegion); |
|
83
|
9 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the customary display name in the given language for the given territory the phone |
|
87
|
|
|
* number is from. If it could be from many territories, nothing is returned. |
|
88
|
|
|
* |
|
89
|
|
|
* @param PhoneNumber $number |
|
90
|
|
|
* @param $locale |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
9 |
|
protected function getCountryNameForNumber(PhoneNumber $number, $locale) |
|
94
|
|
|
{ |
|
95
|
9 |
|
$regionCodes = $this->phoneUtil->getRegionCodesForCountryCode($number->getCountryCode()); |
|
96
|
|
|
|
|
97
|
|
|
if (count($regionCodes) === 1) { |
|
98
|
|
|
return $this->getRegionDisplayName($regionCodes[0], $locale); |
|
99
|
|
|
} else { |
|
100
|
|
|
$regionWhereNumberIsValid = 'ZZ'; |
|
101
|
|
|
foreach ($regionCodes as $regionCode) { |
|
102
|
|
|
if ($this->phoneUtil->isValidNumberForRegion($number, $regionCode)) { |
|
103
|
|
|
if ($regionWhereNumberIsValid !== 'ZZ') { |
|
104
|
|
|
// If we can't assign the phone number as definitely belonging to only one territory, |
|
105
|
|
|
// then we return nothing. |
|
106
|
7 |
|
return ""; |
|
107
|
|
|
} |
|
108
|
|
|
$regionWhereNumberIsValid = $regionCode; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->getRegionDisplayName($regionWhereNumberIsValid, $locale); |
|
113
|
4 |
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
/** |
|
117
|
|
|
* Returns the customary display name in the given language for the given region. |
|
118
|
|
|
* |
|
119
|
1 |
|
* @param $regionCode |
|
120
|
|
|
* @param $locale |
|
121
|
4 |
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getRegionDisplayName($regionCode, $locale) |
|
124
|
|
|
{ |
|
125
|
|
|
if ($regionCode === null || $regionCode == 'ZZ' || $regionCode === PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY) { |
|
126
|
|
|
return ""; |
|
127
|
7 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
return Locale::getDisplayRegion( |
|
130
|
|
|
'-' . $regionCode, |
|
131
|
|
|
$locale |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
239 |
|
* Returns a text description for the given phone number, in the language provided. The |
|
137
|
|
|
* description might consist of the name of the country where the phone number is from, or the |
|
138
|
239 |
|
* name of the geographical area the phone number is from if more detailed information is |
|
139
|
1 |
|
* available. |
|
140
|
|
|
* |
|
141
|
|
|
* <p>This method assumes the validity of the number passed in has already been checked, and that |
|
142
|
|
|
* the number is suitable for geocoding. We consider fixed-line and mobile numbers possible |
|
143
|
239 |
|
* candidates for geocoding. |
|
144
|
|
|
* |
|
145
|
|
|
* <p>If $userRegion is set, we also consider the region of the user. If the phone number is from |
|
146
|
|
|
* the same region as the user, only a lower-level description will be returned, if one exists. |
|
147
|
|
|
* Otherwise, the phone number's region will be returned, with optionally some more detailed |
|
148
|
|
|
* information. |
|
149
|
|
|
* |
|
150
|
|
|
* <p>For example, for a user from the region "US" (United States), we would show "Mountain View, |
|
151
|
|
|
* CA" for a particular number, omitting the United States from the description. For a user from |
|
152
|
|
|
* the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United |
|
153
|
|
|
* States" or even just "United States". |
|
154
|
|
|
* |
|
155
|
|
|
* @param PhoneNumber $number a valid phone number for which we want to get a text description |
|
156
|
|
|
* @param string $locale the language code for which the description should be written |
|
157
|
|
|
* @param string $userRegion the region code for a given user. This region will be omitted from the |
|
158
|
|
|
* description if the phone number comes from this region. It is a two-letter uppercase ISO |
|
159
|
|
|
* country code as defined by ISO 3166-1. |
|
160
|
|
|
* @return string a text description for the given language code for the given phone number |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
163
|
|
|
{ |
|
164
|
|
|
// If the user region matches the number's region, then we just show the lower-level |
|
165
|
|
|
// description, if one exists - if no description exists, we will show the region(country) name |
|
166
|
|
|
// for the number. |
|
167
|
|
|
$regionCode = $this->phoneUtil->getRegionCodeForNumber($number); |
|
168
|
|
|
if ($userRegion == null || $userRegion == $regionCode) { |
|
|
|
|
|
|
169
|
|
|
$languageStr = Locale::getPrimaryLanguage($locale); |
|
170
|
|
|
$scriptStr = ""; |
|
171
|
|
|
$regionStr = Locale::getRegion($locale); |
|
172
|
|
|
|
|
173
|
|
|
$mobileToken = $this->phoneUtil->getCountryMobileToken($number->getCountryCode()); |
|
174
|
|
|
$nationalNumber = $this->phoneUtil->getNationalSignificantNumber($number); |
|
175
|
241 |
|
if ($mobileToken !== "" && (!strncmp($nationalNumber, $mobileToken, strlen($mobileToken)))) { |
|
176
|
|
|
// In some countries, eg. Argentina, mobile numbers have a mobile token before the national |
|
177
|
|
|
// destination code, this should be removed before geocoding. |
|
178
|
|
|
$nationalNumber = substr($nationalNumber, strlen($mobileToken)); |
|
179
|
|
|
$region = $this->phoneUtil->getRegionCodeForCountryCode($number->getCountryCode()); |
|
180
|
|
|
try { |
|
181
|
241 |
|
$copiedNumber = $this->phoneUtil->parse($nationalNumber, $region); |
|
182
|
|
|
} catch (NumberParseException $e) { |
|
183
|
8 |
|
// If this happens, just reuse what we had. |
|
184
|
|
|
$copiedNumber = $number; |
|
185
|
|
|
} |
|
186
|
|
|
$areaDescription = $this->prefixFileReader->getDescriptionForNumber($copiedNumber, $languageStr, $scriptStr, $regionStr); |
|
187
|
|
|
} else { |
|
188
|
7 |
|
$areaDescription = $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return (strlen($areaDescription) > 0) ? $areaDescription : $this->getCountryNameForNumber($number, $locale); |
|
192
|
|
|
} |
|
193
|
|
|
// Otherwise, we just show the region(country) name for now. |
|
194
|
|
|
return $this->getRegionDisplayName($regionCode, $locale); |
|
195
|
|
|
// TODO: Concatenate the lower-level and country-name information in an appropriate |
|
196
|
|
|
// way for each language. |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: