1 | <?php |
||
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 | /** |
||
29 | * PhoneNumberOfflineGeocoder constructor. |
||
30 | * @param string $phonePrefixDataDirectory |
||
31 | */ |
||
32 | 246 | protected function __construct($phonePrefixDataDirectory) |
|
33 | { |
||
34 | 246 | $this->phoneUtil = PhoneNumberUtil::getInstance(); |
|
35 | |||
36 | 246 | $this->prefixFileReader = new PrefixFileReader(dirname(__FILE__) . $phonePrefixDataDirectory); |
|
37 | 246 | } |
|
38 | |||
39 | /** |
||
40 | * Gets a PhoneNumberOfflineGeocoder instance to carry out international phone number geocoding. |
||
41 | * |
||
42 | * <p>The PhoneNumberOfflineGeocoder is implemented as a singleton. Therefore, calling this method |
||
43 | * multiple times will only result in one instance being created. |
||
44 | * |
||
45 | * @param string $mappingDir (Optional) Mapping Data Directory |
||
46 | * @return PhoneNumberOfflineGeocoder |
||
47 | */ |
||
48 | 251 | public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY) |
|
49 | { |
||
50 | 251 | if (static::$instance === null) { |
|
51 | 246 | static::$instance = new static($mappingDir); |
|
52 | } |
||
53 | |||
54 | 251 | return static::$instance; |
|
55 | } |
||
56 | |||
57 | 246 | public static function resetInstance() |
|
61 | |||
62 | /** |
||
63 | * As per getDescriptionForValidNumber, but explicitly checks the validity of the number |
||
64 | * passed in. |
||
65 | * |
||
66 | * |
||
67 | * @see getDescriptionForValidNumber |
||
68 | * @param PhoneNumber $number a valid phone number for which we want to get a text description |
||
69 | * @param string $locale the language code for which the description should be written |
||
70 | * @param string $userRegion the region code for a given user. This region will be omitted from the |
||
71 | * description if the phone number comes from this region. It is a two-letter uppercase ISO |
||
72 | * country code as defined by ISO 3166-1. |
||
73 | * @return string a text description for the given language code for the given phone number, or empty |
||
74 | * string if the number passed in is invalid |
||
75 | */ |
||
76 | 17 | public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
88 | |||
89 | /** |
||
90 | * Returns the customary display name in the given language for the given territory the phone |
||
91 | * number is from. If it could be from many territories, nothing is returned. |
||
92 | * |
||
93 | * @param PhoneNumber $number |
||
94 | * @param string $locale |
||
95 | * @return string |
||
96 | */ |
||
97 | 9 | protected function getCountryNameForNumber(PhoneNumber $number, $locale) |
|
119 | |||
120 | /** |
||
121 | * Returns the customary display name in the given language for the given region. |
||
122 | * |
||
123 | * @param $regionCode |
||
124 | * @param $locale |
||
125 | * @return string |
||
126 | */ |
||
127 | 242 | protected function getRegionDisplayName($regionCode, $locale) |
|
138 | |||
139 | /** |
||
140 | * Returns a text description for the given phone number, in the language provided. The |
||
141 | * description might consist of the name of the country where the phone number is from, or the |
||
142 | * name of the geographical area the phone number is from if more detailed information is |
||
143 | * available. |
||
144 | * |
||
145 | * <p>This method assumes the validity of the number passed in has already been checked, and that |
||
146 | * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible |
||
147 | * candidates for geocoding. |
||
148 | * |
||
149 | * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from |
||
150 | * the same region as the user, only a lower-level description will be returned, if one exists. |
||
151 | * Otherwise, the phone number's region will be returned, with optionally some more detailed |
||
152 | * information. |
||
153 | * |
||
154 | * <p>For example, for a user from the region "US" (United States), we would show "Mountain View, |
||
155 | * CA" for a particular number, omitting the United States from the description. For a user from |
||
156 | * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United |
||
157 | * States" or even just "United States". |
||
158 | * |
||
159 | * @param PhoneNumber $number a valid phone number for which we want to get a text description |
||
160 | * @param string $locale the language code for which the description should be written |
||
161 | * @param string $userRegion the region code for a given user. This region will be omitted from the |
||
162 | * description if the phone number comes from this region. It is a two-letter uppercase ISO |
||
163 | * country code as defined by ISO 3166-1. |
||
164 | * @return string a text description for the given language code for the given phone number |
||
165 | */ |
||
166 | 245 | public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
202 | } |
||
203 |