Country.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Helper;
  3. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  4. use ThurData\Servers\KerioEmail\Core\FileReader\Reader;
  5. /**
  6. * Description of Country
  7. *
  8. * @author inbs
  9. */
  10. class Country
  11. {
  12. protected static $instance = null;
  13. protected $path = '';
  14. protected $type = '';
  15. protected $country = [];
  16. public function __construct ()
  17. {
  18. global $GLOBALS;
  19. $varsionArray = explode('.', $GLOBALS['CONFIG']['Version']);
  20. $varsion = $varsionArray[0] . "." . $varsionArray[1] . "." . $this->getOnlyNumber($varsionArray[2]);
  21. if(version_compare($varsion, '7.0.0', '>='))
  22. {
  23. $this->path = ModuleConstants::getFullPathWhmcs('resources', 'country', 'dist.countries.json');
  24. $this->type = 'json';
  25. }
  26. else
  27. {
  28. $this->path = ModuleConstants::getFullPathWhmcs('includes', 'countries.php');
  29. $this->type = 'php';
  30. }
  31. $this->initCountry();
  32. }
  33. protected function getOnlyNumber($string)
  34. {
  35. $length = strlen($string);
  36. $return = '';
  37. for ($i=0; $i<$length; $i++) {
  38. if (is_numeric($string[$i]))
  39. {
  40. $return .= $string[$i];
  41. continue;
  42. }
  43. break;
  44. }
  45. return $return;
  46. }
  47. protected function initCountry()
  48. {
  49. if ($this->type === 'json') {
  50. foreach(Reader::read($this->path)->get() as $code => $data)
  51. {
  52. $this->country[$code] = $data['name'];
  53. }
  54. } else {
  55. ModuleConstants::requireFile($this->path);
  56. $this->country = $countries;
  57. }
  58. }
  59. public function getFullName($code)
  60. {
  61. if (isset($this->country[$code]))
  62. {
  63. return $this->country[$code];
  64. }
  65. return null;
  66. }
  67. public function getCountry($withKey = true)
  68. {
  69. if ($withKey)
  70. {
  71. return $this->country;
  72. }
  73. $country = [];
  74. foreach($this->country as $code => $name)
  75. {
  76. $country[] = [
  77. 'code' => $code,
  78. 'name' => $name
  79. ];
  80. }
  81. return $country;
  82. }
  83. public function getCode($fullName)
  84. {
  85. if (in_array($fullName,$this->country,true))
  86. {
  87. return array_search($fullName, $this->country, true);
  88. }
  89. return null;
  90. }
  91. /**
  92. * @return Country
  93. */
  94. public static function getInstance()
  95. {
  96. if (self::$instance === null)
  97. {
  98. self::$instance = new self();
  99. }
  100. return self::$instance;
  101. }
  102. }