Completed
Push — dev-7.6.1 ( 6fa1d9...762c00 )
by Joshua
49:26 queued 32:55
created

PrefixFileReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
namespace libphonenumber\prefixmapper;
4
5
use libphonenumber\PhoneNumber;
6
7
/**
8
 * A helper class doing file handling and lookup of phone number prefix mappings.
9
 *
10
 * @package libphonenumber\prefixmapper
11
 */
12
class PrefixFileReader
13
{
14
    protected $phonePrefixDataDirectory;
15
    /**
16
     * The mappingFileProvider knows for which combination of countryCallingCode and language a phone
17
     * prefix mapping file is available in the file system, so that a file can be loaded when needed.
18
     * @var MappingFileProvider
19
     */
20
    protected $mappingFileProvider;
21
    /**
22
     * A mapping from countryCallingCode_lang to the corresponding phone prefix map that has been
23
     * loaded.
24
     * @var array
25
     */
26
    protected $availablePhonePrefixMaps = array();
27
28 253
    public function __construct($phonePrefixDataDirectory)
29
    {
30 253
        $this->phonePrefixDataDirectory = $phonePrefixDataDirectory;
31 253
        $this->loadMappingFileProvider();
32 253
    }
33
34 253
    protected function loadMappingFileProvider()
35
    {
36 253
        $mapPath = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . "Map.php";
37 253
        if (!file_exists($mapPath)) {
38
            throw new \InvalidArgumentException("Invalid data directory");
39
        }
40
41 253
        $map = require $mapPath;
42
43 253
        $this->mappingFileProvider = new MappingFileProvider($map);
44 253
    }
45
46
47
    /**
48
     * @param $prefixMapKey
49
     * @param $language
50
     * @param $script
51
     * @param $region
52
     * @return PhonePrefixMap|null
53
     */
54 25
    public function getPhonePrefixDescriptions($prefixMapKey, $language, $script, $region)
55
    {
56 25
        $fileName = $this->mappingFileProvider->getFileName($prefixMapKey, $language, $script, $region);
57 25
        if (strlen($fileName) == 0) {
58 9
            return null;
59
        }
60
61 20
        if (!isset($this->availablePhonePrefixMaps[$fileName])) {
62 17
            $this->loadPhonePrefixMapFromFile($fileName);
63
        }
64
65 20
        return $this->availablePhonePrefixMaps[$fileName];
66
    }
67
68 17
    protected function loadPhonePrefixMapFromFile($fileName)
69
    {
70 17
        $path = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . $fileName;
71 17
        if (!file_exists($path)) {
72
            throw new \InvalidArgumentException("Data does not exist");
73
        }
74
75 17
        $map = require $path;
76 17
        $areaCodeMap = new PhonePrefixMap($map);
77
78 17
        $this->availablePhonePrefixMaps[$fileName] = $areaCodeMap;
79 17
    }
80
81 14
    public function mayFallBackToEnglish($language)
82
    {
83
        // Don't fall back to English if the requested language is among the following:
84
        // - Chinese
85
        // - Japanese
86
        // - Korean
87 14
        return ($language != 'zh' && $language != 'ja' && $language != 'ko');
88
    }
89
90
    /**
91
     * Returns a text description in the given language for the given phone number.
92
     *
93
     * @param PhoneNumber $number the phone number for which we want to get a text description
94
     * @param string $language two-letter lowercase ISO language codes as defined by ISO 639-1
95
     * @param string $script four-letter titlecase (the first letter is uppercase and the rest of the letters
96
     *     are lowercase) ISO script codes as defined in ISO 15924
97
     * @param string $region two-letter uppercase ISO country codes as defined by ISO 3166-1
98
     * @return string a text description in the given language for the given phone number, or an empty
99
     *     string if a description is not available
100
     */
101 25
    public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region)
102
    {
103 25
        $countryCallingCode = $number->getCountryCode();
104
        // As the NANPA data is split into multiple files covering 3-digit areas, use a phone number
105
        // prefix of 4 digits for NANPA instead, e.g. 1650.
106 25
        if ($countryCallingCode === 1) {
107 10
            $phonePrefix = (1000 + intval($number->getNationalNumber() / 10000000));
108 18
        } elseif ($countryCallingCode === 86) {
109
            // Split China into multiple files to reduce PHP memory usage
110
            // @see https://github.com/giggsey/libphonenumber-for-php/issues/44
111 4
            $phonePrefix = '86' . substr($number->getNationalNumber(), 0, 3);
112
        } else {
113 14
            $phonePrefix = $countryCallingCode;
114
        }
115
116 25
        $phonePrefixDescriptions = $this->getPhonePrefixDescriptions($phonePrefix, $language, $script, $region);
117
118 25
        $description = ($phonePrefixDescriptions !== null) ? $phonePrefixDescriptions->lookup($number) : null;
119
        // When a location is not available in the requested language, fall back to English.
120 25
        if (($description === null || strlen($description) === 0) && $this->mayFallBackToEnglish($language)) {
121 13
            $defaultMap = $this->getPhonePrefixDescriptions($phonePrefix, "en", "", "");
122 13
            if ($defaultMap === null) {
123 4
                return "";
124
            }
125 9
            $description = $defaultMap->lookup($number);
126
        }
127
128 22
        return ($description !== null) ? $description : "";
129
    }
130
}
131