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 | 1 | ); |
|
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 | 1 | } |
|
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 | * Returns the list of time zones corresponding to the country calling code of {@code number}. |
||
102 | * |
||
103 | * @param $number PhoneNumber the phone number to look up |
||
104 | * @return array the list of corresponding time zones or a single element list with the default |
||
105 | * unknown time zone if no other time zone was found |
||
106 | */ |
||
107 | 1 | protected function getCountryLevelTimeZonesforNumber(PhoneNumber $number) |
|
112 | |||
113 | /** |
||
114 | * Returns a list of time zones to which a phone number belongs. |
||
115 | * |
||
116 | * <p>This method assumes the validity of the number passed in has already been checked, and that |
||
117 | * the number is geo-localizable. We consider fixed-line and mobile numbers possible candidates |
||
118 | * for geo-localization. |
||
119 | * |
||
120 | * @param $number PhoneNumber a valid phone number for which we want to get the time zones to which it belongs |
||
121 | * @return array a list of the corresponding time zones or a single element list with the default |
||
122 | * unknown time zone if no other time zone was found or if the number was invalid |
||
123 | */ |
||
124 | 1 | public function getTimeZonesForGeographicalNumber(PhoneNumber $number) |
|
128 | |||
129 | /** |
||
130 | * Returns a list of time zones to which a geocodable phone number belongs. |
||
131 | * |
||
132 | * @param PhoneNumber $number The phone number for which we want to get the time zones to which it belongs |
||
133 | * @return array the list of correspondiing time zones or a single element list with the default |
||
134 | * unknown timezone if no other time zone was found or if the number was invalid |
||
135 | */ |
||
136 | 1 | protected function getTimeZonesForGeocodableNumber(PhoneNumber $number) |
|
141 | } |
||
142 |