Passed
Push — upstream-8.11.0 ( 4431ea )
by Joshua
03:25
created

PhoneNumberToTimeZonesMapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 129
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getUnknownTimeZone() 0 3 1
A loadPrefixTimeZonesMapFromFile() 0 11 2
A getInstance() 0 7 2
A __construct() 0 8 1
A getTimeZonesForNumber() 0 13 3
A getTimeZonesForGeocodableNumber() 0 4 2
A getTimeZonesForGeographicalNumber() 0 3 1
A getCountryLevelTimeZonesforNumber() 0 4 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: giggsey
5
 * Date: 14/10/13
6
 * Time: 16:00
7
 */
8
9
namespace libphonenumber;
10
11
use libphonenumber\prefixmapper\PrefixTimeZonesMap;
12
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;
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)
40
    {
41 1
        if (!\is_readable($path)) {
42
            throw new \InvalidArgumentException('Mapping file can not be found');
43
        }
44
45 1
        $data = require $path;
46
47 1
        $map = new PrefixTimeZonesMap($data);
48
49 1
        return $map;
50
    }
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 3
    public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY)
62
    {
63 3
        if (static::$instance === null) {
64 1
            static::$instance = new static($mappingDir);
65 1
        }
66
67 3
        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()
75
    {
76
        return static::UNKNOWN_TIMEZONE;
77
    }
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 3
    public function getTimeZonesForNumber(PhoneNumber $number)
88
    {
89 3
        $numberType = $this->phoneUtil->getNumberType($number);
90
91 3
        if ($numberType === PhoneNumberType::UNKNOWN) {
92
            return $this->unknownTimeZoneList;
93
        }
94
95 3
        if (!PhoneNumberUtil::getInstance()->isNumberGeographical($numberType, $number->getCountryCode())) {
96 1
            return $this->getCountryLevelTimeZonesforNumber($number);
97
        }
98
99 2
        return $this->getTimeZonesForGeographicalNumber($number);
100
    }
101
102
    /**
103
     * Returns the list of time zones corresponding to the country calling code of {@code number}.
104
     *
105
     * @param $number PhoneNumber the phone number to look up
106
     * @return array the list of corresponding time zones or a single element list with the default
107
     *     unknown time zone if no other time zone was found
108
     */
109 1
    protected function getCountryLevelTimeZonesforNumber(PhoneNumber $number)
110
    {
111 1
        $timezones = $this->prefixTimeZonesMap->lookupCountryLevelTimeZonesForNumber($number);
112 1
        return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
113
    }
114
115
    /**
116
     * Returns a list of time zones to which a phone number belongs.
117
     *
118
     * <p>This method assumes the validity of the number passed in has already been checked, and that
119
     * the number is geo-localizable. We consider fixed-line and mobile numbers possible candidates
120
     * for geo-localization.
121
     *
122
     * @param $number PhoneNumber a valid phone number for which we want to get the time zones to which it belongs
123
     * @return array a list of the corresponding time zones or a single element list with the default
124
     *     unknown time zone if no other time zone was found or if the number was invalid
125
     */
126 2
    public function getTimeZonesForGeographicalNumber(PhoneNumber $number)
127
    {
128 2
        return $this->getTimeZonesForGeocodableNumber($number);
129
    }
130
131
    /**
132
     * Returns a list of time zones to which a geocodable phone number belongs.
133
     *
134
     * @param PhoneNumber $number The phone number for which we want to get the time zones to which it belongs
135
     * @return array the list of correspondiing time zones or a single element list with the default
136
     * unknown timezone if no other time zone was found or if the number was invalid
137
     */
138 2
    protected function getTimeZonesForGeocodableNumber(PhoneNumber $number)
139
    {
140 2
        $timezones = $this->prefixTimeZonesMap->lookupTimeZonesForNumber($number);
141 2
        return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
142
    }
143
}
144