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