forked from wi1dcard/china-divisions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivision.php
More file actions
252 lines (215 loc) · 5.06 KB
/
Division.php
File metadata and controls
252 lines (215 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace ChinaDivisions;
class Division
{
/**
* 区划 ID
*
* @var string|int
*/
protected $divisionId;
/**
* 自身信息
*
* @var array
*/
protected $data;
/**
* 子区划信息
*
* @var array
*/
protected $children;
/**
* 客户端
*
* @var Client
*/
protected $client;
public function __construct($divisionId, Client $client = null)
{
$this->divisionId = $divisionId;
$this->client = $client ?? new Client();
}
/**
* 子区划信息
*
* @return static[]
*/
public function children()
{
if ($this->needToRefreshChildren()) {
$this->refreshChildren();
}
return $this->children;
}
/**
* 需要刷新子区划信息吗?
*
* @return bool
*/
protected function needToRefreshChildren()
{
return $this->children === null;
}
/**
* 刷新子区划信息
*
* @return void
*/
protected function refreshChildren()
{
$request = $this->client->build(
'CNDZK_CHINA_SUB_DIVISIONS',
'cndzk_sub_divisions_cpcode',
['divisionId' => $this->divisionId],
'cndzk_address_sub_divisions'
);
$data = $this->client->send($request);
$list = [];
foreach ($data['divisionsList'] as $item) {
$division = new self($item['divisionCode'], $this->client);
$division->data = $item;
$list[] = $division;
}
return $this->children = $list;
}
/**
* 获取自身信息
*
* @return array
*/
public function self()
{
if ($this->needToRefreshSelf()) {
$this->refreshSelf();
}
return $this->data;
}
/**
* 携带子区划的自身信息
*
* @return array
*/
public function selfWithChildren()
{
$data = $this->data;
$data['children'] = $this->children();
return $data;
}
/**
* 需要刷新自身信息吗?
*
* @return bool
*/
protected function needToRefreshSelf()
{
return $this->data === null;
}
/**
* 刷新自身信息
*
* @return void
*/
protected function refreshSelf()
{
$request = $this->client->build(
'CNDZK_CHINA_DIVISION',
'cndzk_common_division_cpcode',
['divisionId' => $this->divisionId],
'cndzk_address_common_division'
);
$data = $this->client->send($request);
$this->data = $data['chinaDivisionVO'];
}
/**
* 获取可读的省市区地址(例:山东省青岛市市北区)
*
* @return string
*/
public function breadcrumb()
{
$address = '';
foreach ($this->ancestors() as $ancestor) {
$address .= $ancestor->self()['divisionName'];
}
return $address;
}
/**
* 获取上一级父区划
*
* @return static
*/
public function parent()
{
$self = $this->self();
return new self($self['parentId'], $this->client);
}
/**
* 获取所有上级父区划
*
* @see self::breadcrumb()
*
* @return static[]
*/
public function ancestors()
{
$func = function (self $item) use (&$func) {
$self = $item->self();
if ($self['parentId'] == 0) {
return [];
}
$ancestors = $func($item->parent());
$ancestors[] = $item;
return $ancestors;
};
return $func($this);
}
/**
* 地址录入联想
*
* @param string $detailAddress
* @param string $divisionAddress
* @param int $limit
* @param bool $isDegrade
*
* @return array
*/
public function guess($detailAddress, $divisionAddress = '', $limit = 10, $isDegrade = false)
{
$request = $this->client->build(
'CNDZK_GUESS_ADDRESS',
'cndzk_guess_address_cpcode',
[
'divisionAddress' => $divisionAddress ?: $this->breadcrumb(),
'detailAddress' => $detailAddress,
'isDegrade' => $isDegrade ? 1 : 0,
'limit' => intval($limit),
],
'cndzk_guess_address'
);
$data = $this->client->send($request);
return $data['guess_address_response']['data'];
}
/**
* 地址规范查询
*
* @return array
*
* @deprecated v0.1.2
*/
public function search($fullAddress, $limit = 20)
{
$request = $this->client->build(
'CNDZK_ADDRESS_QUERY',
'cndzk_address_query_cpcode',
[
'address' => $fullAddress,
'limit' => intval($limit),
],
'cndzk_address_query'
);
$data = $this->client->send($request);
return $data['AddressBuildings'];
}
}