1 | <?php |
||
11 | class PhoneNumberOfflineGeocoder |
||
12 | { |
||
13 | const MAPPING_DATA_DIRECTORY = '/data'; |
||
14 | /** |
||
15 | * @var PhoneNumberOfflineGeocoder |
||
16 | */ |
||
17 | protected static $instance; |
||
18 | /** |
||
19 | * @var PhoneNumberUtil |
||
20 | */ |
||
21 | protected $phoneUtil; |
||
22 | /** |
||
23 | * @var PrefixFileReader |
||
24 | */ |
||
25 | protected $prefixFileReader = null; |
||
26 | |||
27 | 245 | protected function __construct($phonePrefixDataDirectory) |
|
37 | |||
38 | /** |
||
39 | * Gets a PhoneNumberOfflineGeocoder instance to carry out international phone number geocoding. |
||
40 | * |
||
41 | * <p>The PhoneNumberOfflineGeocoder is implemented as a singleton. Therefore, calling this method |
||
42 | * multiple times will only result in one instance being created. |
||
43 | * |
||
44 | * @param string $mappingDir (Optional) Mapping Data Directory |
||
45 | * @return PhoneNumberOfflineGeocoder |
||
46 | */ |
||
47 | 250 | public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY) |
|
55 | |||
56 | 245 | public static function resetInstance() |
|
60 | |||
61 | /** |
||
62 | * As per getDescriptionForValidNumber, but explicitly checks the validity of the number |
||
63 | * passed in. |
||
64 | * |
||
65 | * |
||
66 | * @see getDescriptionForValidNumber |
||
67 | * @param PhoneNumber $number a valid phone number for which we want to get a text description |
||
68 | * @param string $locale the language code for which the description should be written |
||
69 | * @param string $userRegion the region code for a given user. This region will be omitted from the |
||
70 | * description if the phone number comes from this region. It is a two-letter uppercase ISO |
||
71 | * country code as defined by ISO 3166-1. |
||
72 | * @return string a text description for the given language code for the given phone number, or empty |
||
73 | * string if the number passed in is invalid |
||
74 | */ |
||
75 | 16 | public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
87 | |||
88 | /** |
||
89 | * A similar method is implemented as PhoneNumberUtil.isNumberGeographical, which performs a |
||
90 | * stricter check, as it determines if a number has a geographical association. Also, if new |
||
91 | * phone number types were added, we should check if this other method should be updated too. |
||
92 | * |
||
93 | * @param int $numberType |
||
94 | * @return boolean |
||
95 | */ |
||
96 | 15 | protected function canBeGeocoded($numberType) |
|
100 | |||
101 | /** |
||
102 | * Returns the customary display name in the given language for the given territory the phone |
||
103 | * number is from. If it could be from many territories, nothing is returned. |
||
104 | * |
||
105 | * @param PhoneNumber $number |
||
106 | * @param $locale |
||
107 | * @return string |
||
108 | */ |
||
109 | 8 | protected function getCountryNameForNumber(PhoneNumber $number, $locale) |
|
131 | |||
132 | /** |
||
133 | * Returns the customary display name in the given language for the given region. |
||
134 | * |
||
135 | * @param $regionCode |
||
136 | * @param $locale |
||
137 | * @return string |
||
138 | */ |
||
139 | 241 | protected function getRegionDisplayName($regionCode, $locale) |
|
150 | |||
151 | /** |
||
152 | * Returns a text description for the given phone number, in the language provided. The |
||
153 | * description might consist of the name of the country where the phone number is from, or the |
||
154 | * name of the geographical area the phone number is from if more detailed information is |
||
155 | * available. |
||
156 | * |
||
157 | * <p>This method assumes the validity of the number passed in has already been checked, and that |
||
158 | * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible |
||
159 | * candidates for geocoding. |
||
160 | * |
||
161 | * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from |
||
162 | * the same region as the user, only a lower-level description will be returned, if one exists. |
||
163 | * Otherwise, the phone number's region will be returned, with optionally some more detailed |
||
164 | * information. |
||
165 | * |
||
166 | * <p>For example, for a user from the region "US" (United States), we would show "Mountain View, |
||
167 | * CA" for a particular number, omitting the United States from the description. For a user from |
||
168 | * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United |
||
169 | * States" or even just "United States". |
||
170 | * |
||
171 | * @param PhoneNumber $number a valid phone number for which we want to get a text description |
||
172 | * @param string $locale the language code for which the description should be written |
||
173 | * @param string $userRegion the region code for a given user. This region will be omitted from the |
||
174 | * description if the phone number comes from this region. It is a two-letter uppercase ISO |
||
175 | * country code as defined by ISO 3166-1. |
||
176 | * @return string a text description for the given language code for the given phone number |
||
177 | */ |
||
178 | 247 | public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
214 | } |
||
215 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.