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
|
|
|
protected function initData(array $data, ?RegionStatic $parent = null) |
46
|
|
|
{ |
47
|
|
|
$data = array_merge([ |
48
|
|
|
'name' => null, |
49
|
|
|
'short' => null, |
50
|
|
|
'code' => null, |
51
|
|
|
'area' => null, |
52
|
|
|
'zip' => null, |
53
|
|
|
'lng' => 0, |
54
|
|
|
'lat' => 0, |
55
|
|
|
'children' => [] |
56
|
|
|
], $data); |
57
|
|
|
$this->name = trim($data['name']); |
58
|
|
|
$this->short = trim($data['short']); |
59
|
|
|
$this->code = trim($data['code']); |
60
|
|
|
$this->area = trim($data['area']); |
61
|
|
|
$this->zip = trim($data['zip']); |
62
|
|
|
$this->lng = (float) $data['lng']; |
63
|
|
|
$this->lat = (float) $data['lat']; |
64
|
|
|
$this->parent = $parent; |
65
|
|
|
if ($this->deep < 4 && is_array($data['children'])) { |
66
|
|
|
foreach ($data['children'] as $child) { |
67
|
|
|
$_child = new static(); |
68
|
|
|
$_child->setDeep($this->deep + 1); |
69
|
|
|
$_child->setData($child, $this->cloneRegion()); |
70
|
|
|
$this->children->push($_child); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 复制 |
77
|
|
|
* @return RegionStatic |
78
|
|
|
*/ |
79
|
|
|
protected function cloneRegion(): RegionStatic |
80
|
|
|
{ |
81
|
|
|
$self = new static(); |
82
|
|
|
$self->name = $this->name; |
83
|
|
|
$self->short = $this->short; |
84
|
|
|
$self->code = $this->code; |
85
|
|
|
$self->area = $this->area; |
86
|
|
|
$self->zip = $this->zip; |
87
|
|
|
$self->lng = $this->lng; |
88
|
|
|
$self->lat = $this->lat; |
89
|
|
|
return $self; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* toArray |
94
|
|
|
* @return array<string,null|string|float|int|array<string,null|string|float|int|mixed>> |
95
|
|
|
*/ |
96
|
|
|
public function toArray(): array |
97
|
|
|
{ |
98
|
|
|
$data = [ |
99
|
|
|
'name' => $this->name ?? null, |
100
|
|
|
'short' => $this->short ?? null, |
101
|
|
|
'code' => $this->code ?? 0, |
102
|
|
|
'area' => $this->area ?? 0, |
103
|
|
|
'zip' => $this->zip ?? 0, |
104
|
|
|
'lng' => $this->lng ?? 0, |
105
|
|
|
'lat' => $this->lat ?? 0, |
106
|
|
|
'children' => $this->children->toArray() |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 检查名称有效性 |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function checkName(string $name): bool |
119
|
|
|
{ |
120
|
|
|
return $this->name == $name || $this->short == $name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 检查代码有效性 |
125
|
|
|
* |
126
|
|
|
* @param string $code |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function checkCode(string $code): bool |
130
|
|
|
{ |
131
|
|
|
return $this->code == $code; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* 检查区号有效性 |
136
|
|
|
* |
137
|
|
|
* @param string $area |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function checkArea(string $area): bool |
141
|
|
|
{ |
142
|
|
|
return $this->area == $area; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* 检查电话号码有效性 |
147
|
|
|
* |
148
|
|
|
* @param string $number |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function checkNumber(string $number): bool |
152
|
|
|
{ |
153
|
|
|
if (strlen($number) < 10) |
154
|
|
|
return false; |
155
|
|
|
$area = substr($number, 0, strlen($this->area)); |
156
|
|
|
return $this->area == $area; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 检查邮编有效性 |
161
|
|
|
* |
162
|
|
|
* @param string $zip |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function checkZip(string $zip): bool |
166
|
|
|
{ |
167
|
|
|
return substr($this->zip, 0, 5) == substr($zip, 0, 5); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* jsonSerialize |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function jsonSerialize() |
175
|
|
|
{ |
176
|
|
|
return $this->toArray(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* __toString |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function __toString(): string |
184
|
|
|
{ |
185
|
|
|
return json_encode($this->toArray()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* 获取上级 |
190
|
|
|
* @return RegionStatic|null |
191
|
|
|
*/ |
192
|
|
|
public function getParent(): ?RegionStatic |
193
|
|
|
{ |
194
|
|
|
return $this->parent; |
195
|
|
|
} |
196
|
|
|
} |