Country.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Helper;
  3. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. use ModulesGarden\ProxmoxAddon\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. {
  39. if (is_numeric($string[$i]))
  40. {
  41. $return .= $string[$i];
  42. continue;
  43. }
  44. break;
  45. }
  46. return $return;
  47. }
  48. protected function initCountry()
  49. {
  50. if ($this->type === 'json')
  51. {
  52. foreach (Reader::read($this->path)->get() as $code => $data)
  53. {
  54. $this->country[$code] = $data['name'];
  55. }
  56. }
  57. else
  58. {
  59. ModuleConstants::requireFile($this->path);
  60. $this->country = $countries;
  61. }
  62. }
  63. public function getFullName($code)
  64. {
  65. if (isset($this->country[$code]))
  66. {
  67. return $this->country[$code];
  68. }
  69. return null;
  70. }
  71. public function getCountry($withKey = true)
  72. {
  73. if ($withKey)
  74. {
  75. return $this->country;
  76. }
  77. $country = [];
  78. foreach ($this->country as $code => $name)
  79. {
  80. $country[] = [
  81. 'code' => $code,
  82. 'name' => $name
  83. ];
  84. }
  85. return $country;
  86. }
  87. public function getCode($fullName)
  88. {
  89. if (in_array($fullName, $this->country, true))
  90. {
  91. return array_search($fullName, $this->country, true);
  92. }
  93. return null;
  94. }
  95. /**
  96. * @return Country
  97. */
  98. public static function getInstance()
  99. {
  100. if (self::$instance === null)
  101. {
  102. self::$instance = new self();
  103. }
  104. return self::$instance;
  105. }
  106. }