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 | 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->canBeGeocoded($numberType)) { |
||
79 | return $this->getCountryNameForNumber($number, $locale); |
||
80 | } |
||
81 | |||
82 | return $this->getDescriptionForValidNumber($number, $locale, $userRegion); |
||
83 | 9 | } |
|
84 | |||
85 | /** |
||
86 | * A similar method is implemented as PhoneNumberUtil.isNumberGeographical, which performs a |
||
87 | * stricter check, as it determines if a number has a geographical association. Also, if new |
||
88 | * phone number types were added, we should check if this other method should be updated too. |
||
89 | * |
||
90 | * @param int $numberType |
||
91 | * @return boolean |
||
92 | */ |
||
93 | 9 | protected function canBeGeocoded($numberType) |
|
97 | |||
98 | /** |
||
99 | * Returns the customary display name in the given language for the given territory the phone |
||
100 | * number is from. If it could be from many territories, nothing is returned. |
||
101 | * |
||
102 | * @param PhoneNumber $number |
||
103 | * @param $locale |
||
104 | * @return string |
||
105 | */ |
||
106 | 7 | protected function getCountryNameForNumber(PhoneNumber $number, $locale) |
|
128 | |||
129 | /** |
||
130 | * Returns the customary display name in the given language for the given region. |
||
131 | * |
||
132 | * @param $regionCode |
||
133 | * @param $locale |
||
134 | * @return string |
||
135 | */ |
||
136 | 239 | protected function getRegionDisplayName($regionCode, $locale) |
|
147 | |||
148 | /** |
||
149 | * Returns a text description for the given phone number, in the language provided. The |
||
150 | * description might consist of the name of the country where the phone number is from, or the |
||
151 | * name of the geographical area the phone number is from if more detailed information is |
||
152 | * available. |
||
153 | * |
||
154 | * <p>This method assumes the validity of the number passed in has already been checked, and that |
||
155 | * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible |
||
156 | * candidates for geocoding. |
||
157 | * |
||
158 | * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from |
||
159 | * the same region as the user, only a lower-level description will be returned, if one exists. |
||
160 | * Otherwise, the phone number's region will be returned, with optionally some more detailed |
||
161 | * information. |
||
162 | * |
||
163 | * <p>For example, for a user from the region "US" (United States), we would show "Mountain View, |
||
164 | * CA" for a particular number, omitting the United States from the description. For a user from |
||
165 | * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United |
||
166 | * States" or even just "United States". |
||
167 | * |
||
168 | * @param PhoneNumber $number a valid phone number for which we want to get a text description |
||
169 | * @param string $locale the language code for which the description should be written |
||
170 | * @param string $userRegion the region code for a given user. This region will be omitted from the |
||
171 | * description if the phone number comes from this region. It is a two-letter uppercase ISO |
||
172 | * country code as defined by ISO 3166-1. |
||
173 | * @return string a text description for the given language code for the given phone number |
||
174 | */ |
||
175 | 241 | public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null) |
|
211 | } |
||
212 |