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