Completed
Push — master ( 71957b...519609 )
by Joshua
16:17
created

PrefixTimeZonesMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 56.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 77
ccs 9
cts 16
cp 0.5625
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A lookupCountryLevelTimeZonesForNumber() 0 4 1
A __construct() 0 4 1
A lookupTimeZonesForNumber() 0 8 1
A lookupTimeZonesForNumberKey() 0 12 2
A tokenizeRawOutputString() 0 4 1
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