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