1
|
|
|
<?php |
2
|
|
|
namespace SimpleCMS\Region\Packages; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 地理模块 |
9
|
|
|
*/ |
10
|
|
|
class Region |
11
|
|
|
{ |
12
|
|
|
protected Collection $regions; |
13
|
|
|
|
14
|
|
|
protected FlattenRegion $flattenRegions; |
15
|
|
|
|
16
|
|
|
public function __construct(protected string $region_path) |
17
|
|
|
{ |
18
|
|
|
$this->regions = new Collection(); |
19
|
|
|
$this->loadRegions(); |
20
|
|
|
$this->flattenRegions = new FlattenRegion($this->regions); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function loadRegions(): void |
24
|
|
|
{ |
25
|
|
|
$content = file_get_contents(env('REGION_PATH', $this->region_path)); |
26
|
|
|
try { |
27
|
|
|
$data = json_decode($content, true); |
28
|
|
|
foreach ($data as $region) { |
29
|
|
|
$_region = new RegionModel(); |
30
|
|
|
$_region->setDeep(0); |
31
|
|
|
$_region->setData($region); |
32
|
|
|
$this->regions->push($_region); |
33
|
|
|
} |
34
|
|
|
} catch (\Exception $e) { |
35
|
|
|
error_log($e->getMessage()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 获取所有城市列表 |
41
|
|
|
* |
42
|
|
|
* @author Dennis Lui <[email protected]> |
43
|
|
|
* @return Collection |
44
|
|
|
*/ |
45
|
|
|
public function getAll(): Collection |
46
|
|
|
{ |
47
|
|
|
return $this->regions; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 获取所有下级 |
52
|
|
|
* |
53
|
|
|
* @author Dennis Lui <[email protected]> |
54
|
|
|
* @param string $code |
55
|
|
|
* @return Collection |
56
|
|
|
*/ |
57
|
|
|
public function getAllChildren(string $code): Collection |
58
|
|
|
{ |
59
|
|
|
$children = new Collection(); |
60
|
|
|
|
61
|
|
|
foreach ($this->regions as $region) { |
62
|
|
|
$children = (new ChildrenRecursively($region, $code))->getRegions(); |
63
|
|
|
if ($children->isNotEmpty()) { |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $children; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* 检查名称有效性 |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function checkName(string $name): bool |
78
|
|
|
{ |
79
|
|
|
return $this->flattenRegions->contains(fn(RegionModel $region) => $region->checkName($name)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* 检查行政代码有效性 |
84
|
|
|
* |
85
|
|
|
* @param string $code |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function checkCode(string $code): bool |
89
|
|
|
{ |
90
|
|
|
return $this->flattenRegions->contains(fn(RegionModel $region) => $region->checkCode($code)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 检查区号有效性 |
95
|
|
|
* |
96
|
|
|
* @param string $area |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function checkArea(string $area): bool |
100
|
|
|
{ |
101
|
|
|
return $this->flattenRegions->contains(fn(RegionModel $region) => $region->checkArea($area)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 检查电话号码有效性 |
106
|
|
|
* |
107
|
|
|
* @param string $number |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function checkNumber(string $number): bool |
111
|
|
|
{ |
112
|
|
|
return $this->flattenRegions->contains(fn(RegionModel $region) => $region->checkNumber($number)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 检查邮编有效性 |
117
|
|
|
* |
118
|
|
|
* @param string $zip |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function checkZip(string $zip): bool |
122
|
|
|
{ |
123
|
|
|
return $this->flattenRegions->contains(fn(RegionModel $region) => $region->checkZip($zip)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* 查找地区 |
128
|
|
|
* |
129
|
|
|
* @author Dennis Lui <[email protected]> |
130
|
|
|
* @param string $code |
131
|
|
|
* @return RegionModel|null |
132
|
|
|
*/ |
133
|
|
|
public function findRegion(string $code): ?RegionModel |
134
|
|
|
{ |
135
|
|
|
return $this->flattenRegions->first(fn(RegionModel $region) => $region->code == $code); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 获取下级城市 |
140
|
|
|
* |
141
|
|
|
* @author Dennis Lui <[email protected]> |
142
|
|
|
* @param string $code 地理标识 |
143
|
|
|
* @param int $deep 返回携带子级深度,0 标识无children |
144
|
|
|
* @return Collection |
145
|
|
|
*/ |
146
|
|
|
public function getChildren(string $code, int $deep = 0): Collection |
147
|
|
|
{ |
148
|
|
|
$children = new Collection(); |
149
|
|
|
|
150
|
|
|
foreach ($this->regions as $region) { |
151
|
|
|
$children = (new ChildrenRecursively($region, $code, $deep))->getRegions(); |
152
|
|
|
if ($children->isNotEmpty()) { |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $children; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |