PhoneNumberToTimeZonesMapper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: giggsey
  5. * Date: 14/10/13
  6. * Time: 16:00
  7. */
  8. namespace libphonenumber;
  9. use libphonenumber\prefixmapper\PrefixTimeZonesMap;
  10. class PhoneNumberToTimeZonesMapper
  11. {
  12. const UNKNOWN_TIMEZONE = 'Etc/Unknown';
  13. const MAPPING_DATA_DIRECTORY = '/timezone/data/';
  14. const MAPPING_DATA_FILE_NAME = 'map_data.php';
  15. /**
  16. * @var PhoneNumberToTimeZonesMapper
  17. */
  18. protected static $instance;
  19. protected $unknownTimeZoneList = array();
  20. /**
  21. * @var PhoneNumberUtil
  22. */
  23. protected $phoneUtil;
  24. protected $prefixTimeZonesMap;
  25. protected function __construct($phonePrefixDataDirectory)
  26. {
  27. $this->prefixTimeZonesMap = static::loadPrefixTimeZonesMapFromFile(
  28. \dirname(__FILE__) . $phonePrefixDataDirectory . DIRECTORY_SEPARATOR . static::MAPPING_DATA_FILE_NAME
  29. );
  30. $this->phoneUtil = PhoneNumberUtil::getInstance();
  31. $this->unknownTimeZoneList[] = static::UNKNOWN_TIMEZONE;
  32. }
  33. protected static function loadPrefixTimeZonesMapFromFile($path)
  34. {
  35. if (!\is_readable($path)) {
  36. throw new \InvalidArgumentException('Mapping file can not be found');
  37. }
  38. $data = require $path;
  39. $map = new PrefixTimeZonesMap($data);
  40. return $map;
  41. }
  42. /**
  43. * Gets a {@link PhoneNumberToTimeZonesMapper} instance.
  44. *
  45. * <p> The {@link PhoneNumberToTimeZonesMapper} is implemented as a singleton. Therefore, calling
  46. * this method multiple times will only result in one instance being created.
  47. *
  48. * @param $mappingDir
  49. * @return PhoneNumberToTimeZonesMapper instance
  50. */
  51. public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY)
  52. {
  53. if (static::$instance === null) {
  54. static::$instance = new static($mappingDir);
  55. }
  56. return static::$instance;
  57. }
  58. /**
  59. * Returns a String with the ICU unknown time zone.
  60. * @return string
  61. */
  62. public static function getUnknownTimeZone()
  63. {
  64. return static::UNKNOWN_TIMEZONE;
  65. }
  66. /**
  67. * As per {@link #getTimeZonesForGeographicalNumber(PhoneNumber)} but explicitly checks
  68. * the validity of the number passed in.
  69. *
  70. * @param $number PhoneNumber the phone number for which we want to get the time zones to which it belongs
  71. * @return array a list of the corresponding time zones or a single element list with the default
  72. * unknown time zone if no other time zone was found or if the number was invalid
  73. */
  74. public function getTimeZonesForNumber(PhoneNumber $number)
  75. {
  76. $numberType = $this->phoneUtil->getNumberType($number);
  77. if ($numberType === PhoneNumberType::UNKNOWN) {
  78. return $this->unknownTimeZoneList;
  79. }
  80. if (!PhoneNumberUtil::getInstance()->isNumberGeographical($numberType, $number->getCountryCode())) {
  81. return $this->getCountryLevelTimeZonesforNumber($number);
  82. }
  83. return $this->getTimeZonesForGeographicalNumber($number);
  84. }
  85. /**
  86. * Returns the list of time zones corresponding to the country calling code of {@code number}.
  87. *
  88. * @param $number PhoneNumber the phone number to look up
  89. * @return array the list of corresponding time zones or a single element list with the default
  90. * unknown time zone if no other time zone was found
  91. */
  92. protected function getCountryLevelTimeZonesforNumber(PhoneNumber $number)
  93. {
  94. $timezones = $this->prefixTimeZonesMap->lookupCountryLevelTimeZonesForNumber($number);
  95. return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
  96. }
  97. /**
  98. * Returns a list of time zones to which a phone number belongs.
  99. *
  100. * <p>This method assumes the validity of the number passed in has already been checked, and that
  101. * the number is geo-localizable. We consider fixed-line and mobile numbers possible candidates
  102. * for geo-localization.
  103. *
  104. * @param $number PhoneNumber a valid phone number for which we want to get the time zones to which it belongs
  105. * @return array a list of the corresponding time zones or a single element list with the default
  106. * unknown time zone if no other time zone was found or if the number was invalid
  107. */
  108. public function getTimeZonesForGeographicalNumber(PhoneNumber $number)
  109. {
  110. return $this->getTimeZonesForGeocodableNumber($number);
  111. }
  112. /**
  113. * Returns a list of time zones to which a geocodable phone number belongs.
  114. *
  115. * @param PhoneNumber $number The phone number for which we want to get the time zones to which it belongs
  116. * @return array the list of correspondiing time zones or a single element list with the default
  117. * unknown timezone if no other time zone was found or if the number was invalid
  118. */
  119. protected function getTimeZonesForGeocodableNumber(PhoneNumber $number)
  120. {
  121. $timezones = $this->prefixTimeZonesMap->lookupTimeZonesForNumber($number);
  122. return (\count($timezones) == 0) ? $this->unknownTimeZoneList : $timezones;
  123. }
  124. }