forked from giggsey/libphonenumber-for-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhoneNumberToTimeZonesMapper.php
143 lines (123 loc) · 5 KB
/
PhoneNumberToTimeZonesMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Created by PhpStorm.
* User: giggsey
* Date: 14/10/13
* Time: 16:00
*/
namespace libphonenumber;
use libphonenumber\prefixmapper\PrefixTimeZonesMap;
class PhoneNumberToTimeZonesMapper
{
public const UNKNOWN_TIMEZONE = 'Etc/Unknown';
public const MAPPING_DATA_DIRECTORY = '/timezone/data/';
public const MAPPING_DATA_FILE_NAME = 'map_data.php';
/**
* @var PhoneNumberToTimeZonesMapper
*/
protected static $instance;
protected $unknownTimeZoneList = [];
/**
* @var PhoneNumberUtil
*/
protected $phoneUtil;
protected $prefixTimeZonesMap;
protected function __construct($phonePrefixDataDirectory)
{
$this->prefixTimeZonesMap = static::loadPrefixTimeZonesMapFromFile(
__DIR__ . $phonePrefixDataDirectory . DIRECTORY_SEPARATOR . static::MAPPING_DATA_FILE_NAME
);
$this->phoneUtil = PhoneNumberUtil::getInstance();
$this->unknownTimeZoneList[] = static::UNKNOWN_TIMEZONE;
}
protected static function loadPrefixTimeZonesMapFromFile($path)
{
if (!\is_readable($path)) {
throw new \InvalidArgumentException('Mapping file can not be found');
}
$data = require $path;
$map = new PrefixTimeZonesMap($data);
return $map;
}
/**
* Gets a {@link PhoneNumberToTimeZonesMapper} instance.
*
* <p> The {@link PhoneNumberToTimeZonesMapper} is implemented as a singleton. Therefore, calling
* this method multiple times will only result in one instance being created.
*
* @return PhoneNumberToTimeZonesMapper instance
*/
public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY)
{
if (static::$instance === null) {
static::$instance = new static($mappingDir);
}
return static::$instance;
}
/**
* Returns a String with the ICU unknown time zone.
* @return string
*/
public static function getUnknownTimeZone()
{
return static::UNKNOWN_TIMEZONE;
}
/**
* As per {@link #getTimeZonesForGeographicalNumber(PhoneNumber)} but explicitly checks
* the validity of the number passed in.
*
* @param $number PhoneNumber the phone number for which we want to get the time zones to which it belongs
* @return string[] a list of the corresponding time zones or a single element list with the default
* unknown time zone if no other time zone was found or if the number was invalid
*/
public function getTimeZonesForNumber(PhoneNumber $number)
{
$numberType = $this->phoneUtil->getNumberType($number);
if ($numberType === PhoneNumberType::UNKNOWN) {
return $this->unknownTimeZoneList;
}
if (!PhoneNumberUtil::getInstance()->isNumberGeographical($numberType, $number->getCountryCode())) {
return $this->getCountryLevelTimeZonesforNumber($number);
}
return $this->getTimeZonesForGeographicalNumber($number);
}
/**
* Returns the list of time zones corresponding to the country calling code of {@code number}.
*
* @param $number PhoneNumber the phone number to look up
* @return string[] the list of corresponding time zones or a single element list with the default
* unknown time zone if no other time zone was found
*/
protected function getCountryLevelTimeZonesforNumber(PhoneNumber $number)
{
$timezones = $this->prefixTimeZonesMap->lookupCountryLevelTimeZonesForNumber($number);
return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
}
/**
* Returns a list of time zones to which a phone number belongs.
*
* <p>This method assumes the validity of the number passed in has already been checked, and that
* the number is geo-localizable. We consider fixed-line and mobile numbers possible candidates
* for geo-localization.
*
* @param $number PhoneNumber a valid phone number for which we want to get the time zones to which it belongs
* @return string[] a list of the corresponding time zones or a single element list with the default
* unknown time zone if no other time zone was found or if the number was invalid
*/
public function getTimeZonesForGeographicalNumber(PhoneNumber $number)
{
return $this->getTimeZonesForGeocodableNumber($number);
}
/**
* Returns a list of time zones to which a geocodable phone number belongs.
*
* @param PhoneNumber $number The phone number for which we want to get the time zones to which it belongs
* @return string[] the list of corresponding time zones or a single element list with the default
* unknown timezone if no other time zone was found or if the number was invalid
*/
protected function getTimeZonesForGeocodableNumber(PhoneNumber $number)
{
$timezones = $this->prefixTimeZonesMap->lookupTimeZonesForNumber($number);
return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
}
}