Completed
Push — upstream-8.3.0 ( 6d22b3...36447c )
by Joshua
29:18 queued 18:23
created

PhoneNumberToTimeZonesMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
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 = 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)
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 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()
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 2
    public function getTimeZonesForNumber(PhoneNumber $number)
88
    {
89 2
        $numberType = $this->phoneUtil->getNumberType($number);
90
91 2
        if ($numberType === PhoneNumberType::UNKNOWN) {
92
            return $this->unknownTimeZoneList;
93 2
        } elseif (!!PhoneNumberUtil::getInstance()->isNumberGeographical($numberType, $number->getCountryCode())) {
94 1
            return $this->getCountryLevelTimeZonesforNumber($number);
95
        }
96
97 1
        return $this->getTimeZonesForGeographicalNumber($number);
98
    }
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)
108
    {
109 1
        $timezones = $this->prefixTimeZonesMap->lookupCountryLevelTimeZonesForNumber($number);
110 1
        return (count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
111
    }
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)
125
    {
126 1
        return $this->getTimeZonesForGeocodableNumber($number);
127
    }
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)
137
    {
138 1
        $timezones = $this->prefixTimeZonesMap->lookupTimeZonesForNumber($number);
139 1
        return (count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
140
    }
141
}
142