area_validator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/3/22
  6. * Time: 上午10:40
  7. */
  8. namespace area;
  9. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  10. require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
  11. use algorithm;
  12. class area_validator extends area_check
  13. {
  14. private $mProvinces;
  15. private $mCities;
  16. private $mCountries;
  17. public function __construct($table)
  18. {
  19. parent::__construct($table);
  20. $this->fill();
  21. }
  22. public function province($area_id)
  23. {
  24. $area_id = intval($area_id);
  25. if(algorithm::binary_search($this->mProvinces,$area_id)) {
  26. return array('province' => $this->mItems[$area_id]);
  27. } else {
  28. return false;
  29. }
  30. }
  31. public function city($area_id)
  32. {
  33. $area_id = intval($area_id);
  34. if(algorithm::binary_search($this->mCities,$area_id)) {
  35. $result = [];
  36. $result['city'] = $this->mItems[$area_id];
  37. $parent_id = $this->mItems[$area_id]['area_parent_id'];
  38. $result['province'] = $this->mItems[$parent_id];
  39. return $result;
  40. } else {
  41. return false;
  42. }
  43. }
  44. public function country($area_id)
  45. {
  46. $area_id = intval($area_id);
  47. if(algorithm::binary_search($this->mCountries,$area_id)) {
  48. $result = [];
  49. $result['country'] = $this->mItems[$area_id];
  50. $city_id = $result['country']['area_parent_id'];
  51. $result['city'] = $this->mItems[$city_id];
  52. $province_id = $this->mItems[$city_id]['area_parent_id'];
  53. $result['province'] = $this->mItems[$province_id];
  54. return $result;
  55. } else {
  56. return false;
  57. }
  58. }
  59. public function from_areaid($area_id)
  60. {
  61. if(array_key_exists($area_id,$this->mItems))
  62. {
  63. $areas = [];
  64. $area = $this->mItems[$area_id];
  65. $areas[] = $area;
  66. $parent_id = intval($area['area_parent_id']);
  67. while ($parent_id > 0) {
  68. $area = $this->mItems[$parent_id];
  69. $areas[] = $area;
  70. $parent_id = intval($area['area_parent_id']);
  71. }
  72. $result = [];
  73. $count = count($areas);
  74. for($index = $count - 1 ; $index >= 0; --$index)
  75. {
  76. if($index == $count - 1) {
  77. $result['province'] = $areas[$index];
  78. }
  79. elseif($index == $count - 2) {
  80. $result['city'] = $areas[$index];
  81. }
  82. elseif($index == $count - 3) {
  83. $result['country'] = $areas[$index];
  84. }
  85. }
  86. return $result;
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function from_name($prov_name, $city_name, $country_name)
  92. {
  93. if(empty($prov_name) || empty($city_name) || empty($country_name)) {
  94. return false;
  95. }
  96. if(array_key_exists($prov_name,$this->mNameToProvid))
  97. {
  98. $result = [];
  99. $prov_id = $this->mNameToProvid[$prov_name];
  100. $result['province'] = $this->mItems[$prov_id];
  101. $cities = $this->mAreaIds[$prov_id];
  102. foreach ($cities as $city_id)
  103. {
  104. $city = $this->mItems[$city_id];
  105. if($city['area_name'] == $city_name)
  106. {
  107. $result['city'] = $city;
  108. $countries = $this->mAreaIds[$city_id];
  109. foreach ($countries as $country_id)
  110. {
  111. $country = $this->mItems[$country_id];
  112. if ($country['area_name'] == $country_name) {
  113. $result['country'] = $country;
  114. return $result;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. private function fill()
  123. {
  124. $items = $this->sub_areas(0);
  125. $provins = [];
  126. foreach ($items as $item) {
  127. $area_id = intval($item['area_id']);
  128. if($area_id >= 32) continue; //海外地区不处理了。
  129. $provins[] = $area_id;
  130. }
  131. foreach ($provins as $area_id) {
  132. $this->mProvinces[] = intval($area_id);
  133. }
  134. sort($this->mProvinces);
  135. $citys = $this->next($provins);
  136. foreach ($citys as $area_id) {
  137. $this->mCities[] = intval($area_id);
  138. }
  139. sort($this->mCities);
  140. $countries = $this->next($citys);
  141. foreach ($countries as $area_id) {
  142. $this->mCountries[] = intval($area_id);
  143. }
  144. sort($this->mCountries);
  145. }
  146. public function nosub_city()
  147. {
  148. $result = [];
  149. foreach ($this->mCities as $city_id) {
  150. $subs = $this->mAreaIds[$city_id];
  151. if(empty($subs)) {
  152. $result[] = $this->mItems[$city_id];
  153. }
  154. }
  155. return $result;
  156. }
  157. }