|
1
|
|
|
<?php |
|
2
|
|
|
namespace SimpleCMS\Region\Packages; |
|
3
|
|
|
|
|
4
|
|
|
use function is_array; |
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* 地理模块 |
|
9
|
|
|
* |
|
10
|
|
|
* @author Dennis Lui <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class RegionModel extends RegionStatic implements \JsonSerializable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* 下级 |
|
16
|
|
|
* |
|
17
|
|
|
* @var Collection<RegionModel> |
|
18
|
|
|
*/ |
|
19
|
|
|
public Collection $children; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 上级 |
|
23
|
|
|
* @var RegionModel|null |
|
24
|
|
|
*/ |
|
25
|
|
|
public ?RegionModel $parent = null; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->children = new Collection(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* 设置模型 |
|
34
|
|
|
* @param array $data |
|
35
|
|
|
* @param ?RegionStatic $parent |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setData(array $data, ?RegionStatic $parent = null): void |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->deep < 5) { |
|
41
|
|
|
$this->initData($data, $parent); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 初始化数据 |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
* @param mixed $parent |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function initData(array $data, ?RegionStatic $parent = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$imp = [ |
|
54
|
|
|
'name' => null, |
|
55
|
|
|
'short' => null, |
|
56
|
|
|
'code' => null, |
|
57
|
|
|
'area' => null, |
|
58
|
|
|
'zip' => null, |
|
59
|
|
|
'lng' => 0, |
|
60
|
|
|
'lat' => 0, |
|
61
|
|
|
'children' => [] |
|
62
|
|
|
]; |
|
63
|
|
|
$data = array_merge($imp, $data); |
|
64
|
|
|
foreach (array_keys($imp) as $keyName) { |
|
65
|
|
|
$this->setValue($keyName, $data[$keyName]); |
|
66
|
|
|
} |
|
67
|
|
|
$this->parent = $parent; |
|
68
|
|
|
if ($this->deep < 4 && is_array($data['children'])) { |
|
69
|
|
|
foreach ($data['children'] as $child) { |
|
70
|
|
|
$_child = new static(); |
|
71
|
|
|
$_child->setDeep($this->deep + 1); |
|
72
|
|
|
$_child->setData($child, $this->cloneRegion()); |
|
73
|
|
|
$this->children->push($_child); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* 复制 |
|
80
|
|
|
* @return RegionStatic |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function cloneRegion(): RegionStatic |
|
83
|
|
|
{ |
|
84
|
|
|
$self = new static(); |
|
85
|
|
|
foreach ($this->getAttributes() as $keyName) { |
|
86
|
|
|
$self->$keyName = $this->$keyName; |
|
87
|
|
|
} |
|
88
|
|
|
return $self; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 参数赋值 |
|
93
|
|
|
* @param string $keyName |
|
94
|
|
|
* @param mixed $value |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function setValue(string $keyName, $value): void |
|
98
|
|
|
{ |
|
99
|
|
|
if (in_array($keyName, $this->getAttributes())) { |
|
100
|
|
|
if ($keyName == 'lng' || $keyName == 'lat') { |
|
101
|
|
|
$this->$keyName = (float) $value; |
|
102
|
|
|
} else { |
|
103
|
|
|
$this->$keyName = (string) $value; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* toArray |
|
110
|
|
|
* @return array<string,null|string|float|int|array<string,null|string|float|int|mixed>> |
|
111
|
|
|
*/ |
|
112
|
|
|
public function toArray(): array |
|
113
|
|
|
{ |
|
114
|
|
|
$data = [ |
|
115
|
|
|
'name' => $this->name ?? null, |
|
116
|
|
|
'short' => $this->short ?? null, |
|
117
|
|
|
'code' => $this->code ?? 0, |
|
118
|
|
|
'area' => $this->area ?? 0, |
|
119
|
|
|
'zip' => $this->zip ?? 0, |
|
120
|
|
|
'lng' => $this->lng ?? 0, |
|
121
|
|
|
'lat' => $this->lat ?? 0, |
|
122
|
|
|
'children' => $this->children->toArray() |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
return $data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* 检查名称有效性 |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $name |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function checkName(string $name): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->name == $name || $this->short == $name; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* 检查代码有效性 |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $code |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function checkCode(string $code): bool |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->code == $code; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* 检查区号有效性 |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $area |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
public function checkArea(string $area): bool |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->area == $area; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* 检查电话号码有效性 |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $number |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function checkNumber(string $number): bool |
|
168
|
|
|
{ |
|
169
|
|
|
if (strlen($number) < 10) |
|
170
|
|
|
return false; |
|
171
|
|
|
$area = substr($number, 0, strlen($this->area)); |
|
172
|
|
|
return $this->area == $area; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* 检查邮编有效性 |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $zip |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
public function checkZip(string $zip): bool |
|
182
|
|
|
{ |
|
183
|
|
|
return substr($this->zip, 0, 5) == substr($zip, 0, 5); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* jsonSerialize |
|
188
|
|
|
* @return mixed |
|
189
|
|
|
*/ |
|
190
|
|
|
public function jsonSerialize() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->toArray(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* __toString |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
public function __toString(): string |
|
200
|
|
|
{ |
|
201
|
|
|
return json_encode($this->toArray()); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* 获取上级 |
|
206
|
|
|
* @return RegionStatic|null |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getParent(): ?RegionStatic |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->parent; |
|
211
|
|
|
} |
|
212
|
|
|
} |