PrefixTimeZonesMap.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace libphonenumber\prefixmapper;
  3. use libphonenumber\PhoneNumber;
  4. use libphonenumber\PhoneNumberUtil;
  5. class PrefixTimeZonesMap
  6. {
  7. /*
  8. protected final PhonePrefixMap phonePrefixMap = new PhonePrefixMap();
  9. protected static final String RAW_STRING_TIMEZONES_SEPARATOR = "&";
  10. */
  11. const RAW_STRING_TIMEZONES_SEPARATOR = '&';
  12. protected $phonePrefixMap;
  13. public function __construct($map)
  14. {
  15. $this->phonePrefixMap = new PhonePrefixMap($map);
  16. }
  17. /**
  18. * As per {@link #lookupTimeZonesForNumber(long)}, but receives the number as a PhoneNumber
  19. * instead of a long.
  20. *
  21. * @param $number PhoneNumber the phone number to look up
  22. * @return array the list of corresponding time zones
  23. */
  24. public function lookupTimeZonesForNumber(PhoneNumber $number)
  25. {
  26. $phonePrefix = $number->getCountryCode() . PhoneNumberUtil::getInstance()->getNationalSignificantNumber(
  27. $number
  28. );
  29. return $this->lookupTimeZonesForNumberKey($phonePrefix);
  30. }
  31. /**
  32. * Returns the list of time zones {@code key} corresponds to.
  33. *
  34. * <p>{@code key} could be the calling country code and the full significant number of a
  35. * certain number, or it could be just a phone-number prefix.
  36. * For example, the full number 16502530000 (from the phone-number +1 650 253 0000) is a valid
  37. * input. Also, any of its prefixes, such as 16502, is also valid.
  38. *
  39. * @param $key int the key to look up
  40. * @return array the list of corresponding time zones
  41. */
  42. protected function lookupTimeZonesForNumberKey($key)
  43. {
  44. // Lookup in the map data. The returned String may consist of several time zones, so it must be
  45. // split.
  46. $timezonesString = $this->phonePrefixMap->lookupKey($key);
  47. if ($timezonesString === null) {
  48. return array();
  49. }
  50. return $this->tokenizeRawOutputString($timezonesString);
  51. }
  52. /**
  53. * Split {@code timezonesString} into all the time zones that are part of it.
  54. *
  55. * @param $timezonesString String
  56. * @return array
  57. */
  58. protected function tokenizeRawOutputString($timezonesString)
  59. {
  60. return explode(static::RAW_STRING_TIMEZONES_SEPARATOR, $timezonesString);
  61. }
  62. /**
  63. * Returns the list of time zones {@code number}'s calling country code corresponds to.
  64. *
  65. * @param $number PhoneNumber the phone number to look up
  66. * @return array the list of corresponding time zones
  67. */
  68. public function lookupCountryLevelTimeZonesForNumber(PhoneNumber $number)
  69. {
  70. return $this->lookupTimeZonesForNumberKey($number->getCountryCode());
  71. }
  72. }