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

PrefixFileReader   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 108
ccs 38
cts 40
cp 0.95
rs 10
c 1
b 0
f 0

6 Methods

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