1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace libphonenumber\prefixmapper; |
4
|
|
|
|
5
|
|
|
use libphonenumber\PhoneNumber; |
6
|
|
|
use libphonenumber\PhoneNumberUtil; |
7
|
|
|
|
8
|
|
|
class PrefixTimeZonesMap |
9
|
|
|
{ |
10
|
|
|
/* |
11
|
|
|
|
12
|
|
|
protected final PhonePrefixMap phonePrefixMap = new PhonePrefixMap(); |
13
|
|
|
protected static final String RAW_STRING_TIMEZONES_SEPARATOR = "&"; |
14
|
|
|
*/ |
15
|
|
|
const RAW_STRING_TIMEZONES_SEPARATOR = "&"; |
16
|
|
|
protected $phonePrefixMap; |
17
|
|
|
|
18
|
1 |
|
public function __construct($map) |
19
|
|
|
{ |
20
|
|
|
$this->phonePrefixMap = new PhonePrefixMap($map); |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* As per {@link #lookupTimeZonesForNumber(long)}, but receives the number as a PhoneNumber |
25
|
|
|
* instead of a long. |
26
|
|
|
* |
27
|
|
|
* @param $number PhoneNumber the phone number to look up |
28
|
|
|
* @return array the list of corresponding time zones |
29
|
|
|
*/ |
30
|
6 |
|
public function lookupTimeZonesForNumber(PhoneNumber $number) |
31
|
|
|
{ |
32
|
|
|
$phonePrefix = $number->getCountryCode() . PhoneNumberUtil::getInstance()->getNationalSignificantNumber( |
33
|
|
|
$number |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
return $this->lookupTimeZonesForNumberKey($phonePrefix); |
37
|
6 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the list of time zones {@code key} corresponds to. |
41
|
|
|
* |
42
|
|
|
* <p>{@code key} could be the calling country code and the full significant number of a |
43
|
|
|
* certain number, or it could be just a phone-number prefix. |
44
|
|
|
* For example, the full number 16502530000 (from the phone-number +1 650 253 0000) is a valid |
45
|
|
|
* input. Also, any of its prefixes, such as 16502, is also valid. |
46
|
|
|
* |
47
|
|
|
* @param $key int the key to look up |
48
|
|
|
* @return array the list of corresponding time zones |
49
|
|
|
*/ |
50
|
7 |
|
protected function lookupTimeZonesForNumberKey($key) |
51
|
|
|
{ |
52
|
|
|
// Lookup in the map data. The returned String may consist of several time zones, so it must be |
53
|
|
|
// split. |
54
|
|
|
$timezonesString = $this->phonePrefixMap->lookupKey($key); |
55
|
|
|
|
56
|
7 |
|
if ($timezonesString === null) { |
57
|
2 |
|
return array(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->tokenizeRawOutputString($timezonesString); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Split {@code timezonesString} into all the time zones that are part of it. |
65
|
|
|
* |
66
|
|
|
* @param $timezonesString String |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function tokenizeRawOutputString($timezonesString) |
70
|
|
|
{ |
71
|
|
|
return explode(static::RAW_STRING_TIMEZONES_SEPARATOR, $timezonesString); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the list of time zones {@code number}'s calling country code corresponds to. |
76
|
|
|
* |
77
|
|
|
* @param $number PhoneNumber the phone number to look up |
78
|
|
|
* @return array the list of corresponding time zones |
79
|
|
|
*/ |
80
|
1 |
|
public function lookupCountryLevelTimeZonesForNumber(PhoneNumber $number) |
81
|
|
|
{ |
82
|
|
|
return $this->lookupTimeZonesForNumberKey($number->getCountryCode()); |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|