1 | <?php |
||
13 | class PhoneNumberToTimeZonesMapper |
||
14 | { |
||
15 | const UNKNOWN_TIMEZONE = 'Etc/Unknown'; |
||
16 | const MAPPING_DATA_DIRECTORY = '/timezone/data/'; |
||
17 | const MAPPING_DATA_FILE_NAME = "map_data.php"; |
||
18 | /** |
||
19 | * @var PhoneNumberToTimeZonesMapper |
||
20 | */ |
||
21 | protected static $instance = null; |
||
22 | protected $unknownTimeZoneList = array(); |
||
23 | /** |
||
24 | * @var PhoneNumberUtil |
||
25 | */ |
||
26 | protected $phoneUtil; |
||
27 | protected $prefixTimeZonesMap; |
||
28 | |||
29 | 1 | protected function __construct($phonePrefixDataDirectory) |
|
30 | { |
||
31 | 1 | $this->prefixTimeZonesMap = static::loadPrefixTimeZonesMapFromFile( |
|
32 | 1 | dirname(__FILE__) . $phonePrefixDataDirectory . DIRECTORY_SEPARATOR . static::MAPPING_DATA_FILE_NAME |
|
33 | ); |
||
34 | 1 | $this->phoneUtil = PhoneNumberUtil::getInstance(); |
|
35 | |||
36 | 1 | $this->unknownTimeZoneList[] = static::UNKNOWN_TIMEZONE; |
|
37 | 1 | } |
|
38 | |||
39 | 1 | protected static function loadPrefixTimeZonesMapFromFile($path) |
|
51 | |||
52 | /** |
||
53 | * Gets a {@link PhoneNumberToTimeZonesMapper} instance. |
||
54 | * |
||
55 | * <p> The {@link PhoneNumberToTimeZonesMapper} is implemented as a singleton. Therefore, calling |
||
56 | * this method multiple times will only result in one instance being created. |
||
57 | * |
||
58 | * @param $mappingDir |
||
59 | * @return PhoneNumberToTimeZonesMapper instance |
||
60 | */ |
||
61 | 2 | public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY) |
|
62 | { |
||
63 | 2 | if (static::$instance === null) { |
|
64 | 1 | static::$instance = new static($mappingDir); |
|
65 | } |
||
66 | |||
67 | 2 | return static::$instance; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns a String with the ICU unknown time zone. |
||
72 | * @return string |
||
73 | */ |
||
74 | public static function getUnknownTimeZone() |
||
78 | |||
79 | /** |
||
80 | * As per {@link #getTimeZonesForGeographicalNumber(PhoneNumber)} but explicitly checks |
||
81 | * the validity of the number passed in. |
||
82 | * |
||
83 | * @param $number PhoneNumber the phone number for which we want to get the time zones to which it belongs |
||
84 | * @return array a list of the corresponding time zones or a single element list with the default |
||
85 | * unknown time zone if no other time zone was found or if the number was invalid |
||
86 | */ |
||
87 | 2 | public function getTimeZonesForNumber(PhoneNumber $number) |
|
99 | |||
100 | /** |
||
101 | * A similar method is implemented as PhoneNumberUtil.isNumberGeographical, which performs a |
||
102 | * stricter check, as it determines if a number has a geographical association. Also, if new |
||
103 | * phone number types were added, we should check if this other method should be updated too. |
||
104 | * TODO: Remove duplication by completing the logic in the method in PhoneNumberUtil. |
||
105 | * For more information, see the comments in that method. |
||
106 | * @param $numberType |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function canBeGeocoded($numberType) |
|
116 | |||
117 | /** |
||
118 | * Returns the list of time zones corresponding to the country calling code of {@code number}. |
||
119 | * |
||
120 | * @param $number PhoneNumber the phone number to look up |
||
121 | * @return array the list of corresponding time zones or a single element list with the default |
||
122 | * unknown time zone if no other time zone was found |
||
123 | */ |
||
124 | 1 | protected function getCountryLevelTimeZonesforNumber(PhoneNumber $number) |
|
129 | |||
130 | /** |
||
131 | * Returns a list of time zones to which a phone number belongs. |
||
132 | * |
||
133 | * <p>This method assumes the validity of the number passed in has already been checked, and that |
||
134 | * the number is geo-localizable. We consider fixed-line and mobile numbers possible candidates |
||
135 | * for geo-localization. |
||
136 | * |
||
137 | * @param $number PhoneNumber a valid phone number for which we want to get the time zones to which it belongs |
||
138 | * @return array a list of the corresponding time zones or a single element list with the default |
||
139 | * unknown time zone if no other time zone was found or if the number was invalid |
||
140 | */ |
||
141 | 1 | public function getTimeZonesForGeographicalNumber(PhoneNumber $number) |
|
145 | |||
146 | /** |
||
147 | * Returns a list of time zones to which a geocodable phone number belongs. |
||
148 | * |
||
149 | * @param PhoneNumber $number The phone number for which we want to get the time zones to which it belongs |
||
150 | * @return array the list of correspondiing time zones or a single element list with the default |
||
151 | * unknown timezone if no other time zone was found or if the number was invalid |
||
152 | */ |
||
153 | 1 | protected function getTimeZonesForGeocodableNumber(PhoneNumber $number) |
|
158 | } |
||
159 |