Completed
Push — reducePrefixMapperFileCount ( 09d7d0...7b6d76 )
by Joshua
20:39 queued 08:53
created

PhoneNumberOfflineGeocoder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 98.25%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 23
c 4
b 0
f 0
lcom 2
cbo 4
dl 0
loc 187
ccs 56
cts 57
cp 0.9825
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resetInstance() 0 4 1
A getInstance() 0 8 2
A getDescriptionForNumber() 0 12 3
B getCountryNameForNumber() 0 22 5
A getRegionDisplayName() 0 11 4
C getDescriptionForValidNumber() 0 36 7
1
<?php
2
3
namespace libphonenumber\geocoding;
4
5
use Giggsey\Locale\Locale;
6
use libphonenumber\NumberParseException;
7
use libphonenumber\PhoneNumber;
8
use libphonenumber\PhoneNumberType;
9
use libphonenumber\PhoneNumberUtil;
10
use libphonenumber\prefixmapper\PrefixFileReader;
11
12
class PhoneNumberOfflineGeocoder
13
{
14
    const MAPPING_DATA_DIRECTORY = '/data';
15
    /**
16
     * @var PhoneNumberOfflineGeocoder
17
     */
18
    protected static $instance;
19
    /**
20
     * @var PhoneNumberUtil
21
     */
22
    protected $phoneUtil;
23
    /**
24
     * @var PrefixFileReader
25
     */
26
    protected $prefixFileReader = null;
27
28 246
    protected function __construct($phonePrefixDataDirectory)
29
    {
30 246
        $this->phoneUtil = PhoneNumberUtil::getInstance();
31
32 246
        $this->prefixFileReader = new PrefixFileReader(dirname(__FILE__) . $phonePrefixDataDirectory);
33 246
    }
34
35
    /**
36
     * Gets a PhoneNumberOfflineGeocoder instance to carry out international phone number geocoding.
37
     *
38
     * <p>The PhoneNumberOfflineGeocoder is implemented as a singleton. Therefore, calling this method
39
     * multiple times will only result in one instance being created.
40
     *
41
     * @param string $mappingDir (Optional) Mapping Data Directory
42
     * @return PhoneNumberOfflineGeocoder
43
     */
44 251
    public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY)
45
    {
46 251
        if (static::$instance === null) {
47 246
            static::$instance = new static($mappingDir);
48 246
        }
49
50 251
        return static::$instance;
51
    }
52
53 246
    public static function resetInstance()
54
    {
55 246
        static::$instance = null;
56 246
    }
57
58
    /**
59
     * As per getDescriptionForValidNumber, but explicitly checks the validity of the number
60
     * passed in.
61
     *
62
     *
63
     * @see getDescriptionForValidNumber
64
     * @param PhoneNumber $number a valid phone number for which we want to get a text description
65
     * @param string $locale the language code for which the description should be written
66
     * @param string $userRegion the region code for a given user. This region will be omitted from the
67
     *     description if the phone number comes from this region. It is a two-letter uppercase ISO
68
     *     country code as defined by ISO 3166-1.
69
     * @return string a text description for the given language code for the given phone number, or empty
70
     *     string if the number passed in is invalid
71
     */
72 17
    public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null)
73
    {
74 17
        $numberType = $this->phoneUtil->getNumberType($number);
75
76 17
        if ($numberType === PhoneNumberType::UNKNOWN) {
77 3
            return "";
78 16
        } elseif (!$this->phoneUtil->isNumberGeographical($numberType, $number->getCountryCode())) {
79 5
            return $this->getCountryNameForNumber($number, $locale);
80
        }
81
82 12
        return $this->getDescriptionForValidNumber($number, $locale, $userRegion);
83
    }
84
85
    /**
86
     * Returns the customary display name in the given language for the given territory the phone
87
     * number is from. If it could be from many territories, nothing is returned.
88
     *
89
     * @param PhoneNumber $number
90
     * @param $locale
91
     * @return string
92
     */
93 9
    protected function getCountryNameForNumber(PhoneNumber $number, $locale)
94
    {
95 9
        $regionCodes = $this->phoneUtil->getRegionCodesForCountryCode($number->getCountryCode());
96
97 9
        if (count($regionCodes) === 1) {
98 5
            return $this->getRegionDisplayName($regionCodes[0], $locale);
99
        } else {
100 5
            $regionWhereNumberIsValid = 'ZZ';
101 5
            foreach ($regionCodes as $regionCode) {
102 5
                if ($this->phoneUtil->isValidNumberForRegion($number, $regionCode)) {
103 5
                    if ($regionWhereNumberIsValid !== 'ZZ') {
104
                        // If we can't assign the phone number as definitely belonging to only one territory,
105
                        // then we return nothing.
106 1
                        return "";
107
                    }
108 5
                    $regionWhereNumberIsValid = $regionCode;
109 5
                }
110 5
            }
111
112 4
            return $this->getRegionDisplayName($regionWhereNumberIsValid, $locale);
113
        }
114
    }
115
116
    /**
117
     * Returns the customary display name in the given language for the given region.
118
     *
119
     * @param $regionCode
120
     * @param $locale
121
     * @return string
122
     */
123 242
    protected function getRegionDisplayName($regionCode, $locale)
124
    {
125 242
        if ($regionCode === null || $regionCode == 'ZZ' || $regionCode === PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY) {
126 1
            return "";
127
        }
128
129 242
        return Locale::getDisplayRegion(
130 242
            '-' . $regionCode,
131
            $locale
132 242
        );
133
    }
134
135
    /**
136
     * Returns a text description for the given phone number, in the language provided. The
137
     * description might consist of the name of the country where the phone number is from, or the
138
     * name of the geographical area the phone number is from if more detailed information is
139
     * available.
140
     *
141
     * <p>This method assumes the validity of the number passed in has already been checked, and that
142
     * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible
143
     * candidates for geocoding.
144
     *
145
     * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from
146
     * the same region as the user, only a lower-level description will be returned, if one exists.
147
     * Otherwise, the phone number's region will be returned, with optionally some more detailed
148
     * information.
149
     *
150
     * <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
151
     * CA" for a particular number, omitting the United States from the description. For a user from
152
     * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
153
     * States" or even just "United States".
154
     *
155
     * @param PhoneNumber $number a valid phone number for which we want to get a text description
156
     * @param string $locale the language code for which the description should be written
157
     * @param string $userRegion the region code for a given user. This region will be omitted from the
158
     *     description if the phone number comes from this region. It is a two-letter uppercase ISO
159
     *     country code as defined by ISO 3166-1.
160
     * @return string a text description for the given language code for the given phone number
161
     */
162 245
    public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null)
163
    {
164
        // If the user region matches the number's region, then we just show the lower-level
165
        // description, if one exists - if no description exists, we will show the region(country) name
166
        // for the number.
167 245
        $regionCode = $this->phoneUtil->getRegionCodeForNumber($number);
168 245
        if ($userRegion == null || $userRegion == $regionCode) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $userRegion of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
169 12
            $languageStr = Locale::getPrimaryLanguage($locale);
170 12
            $scriptStr = "";
171 12
            $regionStr = Locale::getRegion($locale);
172
173 12
            $mobileToken = PhoneNumberUtil::getCountryMobileToken($number->getCountryCode());
174 12
            $nationalNumber = $this->phoneUtil->getNationalSignificantNumber($number);
175 12
            if ($mobileToken !== "" && (!strncmp($nationalNumber, $mobileToken, strlen($mobileToken)))) {
176
                // In some countries, eg. Argentina, mobile numbers have a mobile token before the national
177
                // destination code, this should be removed before geocoding.
178 1
                $nationalNumber = substr($nationalNumber, strlen($mobileToken));
179 1
                $region = $this->phoneUtil->getRegionCodeForCountryCode($number->getCountryCode());
180
                try {
181 1
                    $copiedNumber = $this->phoneUtil->parse($nationalNumber, $region);
182 1
                } catch (NumberParseException $e) {
183
                    // If this happens, just reuse what we had.
184
                    $copiedNumber = $number;
185
                }
186 1
                $areaDescription = $this->prefixFileReader->getDescriptionForNumber($copiedNumber, $languageStr, $scriptStr, $regionStr);
187 1
            } else {
188 11
                $areaDescription = $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr);
189
            }
190
191 12
            return (strlen($areaDescription) > 0) ? $areaDescription : $this->getCountryNameForNumber($number, $locale);
192
        }
193
        // Otherwise, we just show the region(country) name for now.
194 234
        return $this->getRegionDisplayName($regionCode, $locale);
195
        // TODO: Concatenate the lower-level and country-name information in an appropriate
196
        // way for each language.
197
    }
198
}
199