|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* |
|
5
|
|
|
* @author joshuag |
|
6
|
|
|
* @created: 04/08/2015 09:03 |
|
7
|
|
|
* @project libphonenumber-for-php |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace libphonenumber; |
|
11
|
|
|
|
|
12
|
|
|
class MultiFileMetadataSourceImpl implements MetadataSourceInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected static $metaDataFilePrefix = PhoneNumberUtil::META_DATA_FILE_PREFIX; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* A mapping from a region code to the PhoneMetadata for that region. |
|
18
|
|
|
* @var PhoneMetadata[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $regionToMetadataMap = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A mapping from a country calling code for a non-geographical entity to the PhoneMetadata for |
|
24
|
|
|
* that country calling code. Examples of the country calling codes include 800 (International |
|
25
|
|
|
* Toll Free Service) and 808 (International Shared Cost Service). |
|
26
|
|
|
* @var PhoneMetadata[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $countryCodeToNonGeographicalMetadataMap = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The prefix of the metadata files from which region data is loaded. |
|
32
|
|
|
* @var String |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $currentFilePrefix; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The metadata loader used to inject alternative metadata sources. |
|
39
|
|
|
* @var MetadataLoaderInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $metadataLoader; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param MetadataLoaderInterface $metadataLoader |
|
45
|
|
|
* @param string|null $currentFilePrefix |
|
46
|
|
|
*/ |
|
47
|
673 |
|
public function __construct(MetadataLoaderInterface $metadataLoader, $currentFilePrefix = null) |
|
48
|
|
|
{ |
|
49
|
673 |
|
if ($currentFilePrefix === null) { |
|
50
|
|
|
$currentFilePrefix = static::$metaDataFilePrefix; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
673 |
|
$this->currentFilePrefix = $currentFilePrefix; |
|
54
|
673 |
|
$this->metadataLoader = $metadataLoader; |
|
55
|
673 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
5219 |
|
public function getMetadataForRegion($regionCode) |
|
61
|
|
|
{ |
|
62
|
5219 |
|
if (!array_key_exists($regionCode, $this->regionToMetadataMap)) { |
|
63
|
|
|
// The regionCode here will be valid and won't be '001', so we don't need to worry about |
|
64
|
|
|
// what to pass in for the country calling code. |
|
65
|
829 |
|
$this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0, $this->metadataLoader); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5219 |
|
return $this->regionToMetadataMap[$regionCode]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
34 |
|
public function getMetadataForNonGeographicalRegion($countryCallingCode) |
|
75
|
|
|
{ |
|
76
|
34 |
|
if (!array_key_exists($countryCallingCode, $this->countryCodeToNonGeographicalMetadataMap)) { |
|
77
|
34 |
|
$this->loadMetadataFromFile($this->currentFilePrefix, PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY, $countryCallingCode, $this->metadataLoader); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
34 |
|
return $this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $filePrefix |
|
85
|
|
|
* @param string $regionCode |
|
86
|
|
|
* @param int $countryCallingCode |
|
87
|
|
|
* @param MetadataLoaderInterface $metadataLoader |
|
88
|
|
|
* @throws \RuntimeException |
|
89
|
|
|
*/ |
|
90
|
843 |
|
public function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode, MetadataLoaderInterface $metadataLoader) |
|
91
|
|
|
{ |
|
92
|
843 |
|
$isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode; |
|
93
|
843 |
|
$fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php'; |
|
94
|
843 |
|
if (!is_readable($fileName)) { |
|
95
|
1 |
|
throw new \RuntimeException('missing metadata: ' . $fileName); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
842 |
|
$data = $metadataLoader->loadMetadata($fileName); |
|
99
|
842 |
|
$metadata = new PhoneMetadata(); |
|
100
|
842 |
|
$metadata->fromArray($data); |
|
101
|
842 |
|
if ($isNonGeoRegion) { |
|
102
|
34 |
|
$this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata; |
|
103
|
|
|
} else { |
|
104
|
829 |
|
$this->regionToMetadataMap[$regionCode] = $metadata; |
|
105
|
|
|
} |
|
106
|
842 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|