MultiFileMetadataSourceImpl.php 3.5 KB

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