1 | <?php |
||
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 | 252 | public function __construct($phonePrefixDataDirectory) |
|
29 | { |
||
30 | 252 | $this->phonePrefixDataDirectory = $phonePrefixDataDirectory; |
|
31 | $this->loadMappingFileProvider(); |
||
32 | 252 | } |
|
33 | |||
34 | 252 | protected function loadMappingFileProvider() |
|
35 | { |
||
36 | 252 | $mapPath = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . "Map.php"; |
|
37 | if (!file_exists($mapPath)) { |
||
38 | throw new \InvalidArgumentException("Invalid data directory"); |
||
39 | } |
||
40 | |||
41 | $map = require $mapPath; |
||
42 | |||
43 | $this->mappingFileProvider = new MappingFileProvider($map); |
||
44 | 252 | } |
|
45 | |||
46 | |||
47 | /** |
||
48 | * @param $prefixMapKey |
||
49 | * @param $language |
||
50 | * @param $script |
||
51 | * @param $region |
||
52 | * @return PhonePrefixMap|null |
||
53 | */ |
||
54 | 6 | public function getPhonePrefixDescriptions($prefixMapKey, $language, $script, $region) |
|
55 | { |
||
56 | $fileName = $this->mappingFileProvider->getFileName($prefixMapKey, $language, $script, $region); |
||
57 | if (strlen($fileName) == 0) { |
||
58 | 3 | return null; |
|
59 | } |
||
60 | |||
61 | 6 | if (!isset($this->availablePhonePrefixMaps[$fileName])) { |
|
62 | $this->loadPhonePrefixMapFromFile($fileName); |
||
63 | } |
||
64 | |||
65 | 6 | return $this->availablePhonePrefixMaps[$fileName]; |
|
66 | } |
||
67 | |||
68 | 12 | protected function loadPhonePrefixMapFromFile($fileName) |
|
69 | { |
||
70 | 12 | $path = $this->phonePrefixDataDirectory . DIRECTORY_SEPARATOR . $fileName; |
|
71 | if (!file_exists($path)) { |
||
72 | throw new \InvalidArgumentException("Data does not exist"); |
||
73 | } |
||
74 | |||
75 | $map = require $path; |
||
76 | $areaCodeMap = new PhonePrefixMap($map); |
||
77 | |||
78 | 12 | $this->availablePhonePrefixMaps[$fileName] = $areaCodeMap; |
|
79 | 12 | } |
|
80 | |||
81 | 10 | public function mayFallBackToEnglish($language) |
|
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 | 13 | public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region) |
|
130 | } |
||
131 |