Skip to content

Commit 1e6daa6

Browse files
committed
refactor(core): 放宽 PHP 版本并优化代码逻辑
箭头函数改用闭包函数,部分返回增加空合并运算符兜底 Closes #2
1 parent a432705 commit 1e6daa6

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2-
composer.lock
2+
composer.lock
3+
/test.php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=8.1",
18+
"php": ">=7.0",
19+
"ext-mbstring": "*",
1920
"hejunjie/china-division": "^1.0"
2021
}
2122
}

src/AddressParser.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AddressParser
1010

1111
/**
1212
* 解析地址
13-
* @param mixed $string 地址信息
13+
* @param string $string 地址信息
1414
* @param bool $user 信息中是否包含用户信息
1515
* @param string $unknownValue 未匹配到数据时填充内容
1616
* @param array $level1Data 一级行政区(省)数据(不传递则使用默认数据源)
@@ -39,23 +39,23 @@ public static function parse(string $string, bool $user = true, $unknownValue =
3939
];
4040
// 提取用户信息(如果需要)
4141
if ($user) {
42-
$extractUserInfo = UserInfoExtractor::extractUserInfo($string);
42+
$extractUserInfo = (array)UserInfoExtractor::extractUserInfo($string);
4343
$data = array_merge($data, [
44-
'name' => $extractUserInfo['name'],
45-
'mobile' => $extractUserInfo['mobile'],
46-
'idn' => $extractUserInfo['idn'],
47-
'postcode' => $extractUserInfo['postcode']
44+
'name' => $extractUserInfo['name'] ?? '',
45+
'mobile' => $extractUserInfo['mobile'] ?? '',
46+
'idn' => $extractUserInfo['idn'] ?? '',
47+
'postcode' => $extractUserInfo['postcode'] ?? ''
4848
]);
49-
$addr = $extractUserInfo['addr'];
49+
$addr = $extractUserInfo['addr'] ?? '';
5050
} else {
5151
$addr = $string;
5252
}
5353
// 解析地址
54-
$address = RegionMatcher::parseAddress($addr, $unknownValue, $level1Data, $level2Data, $level3Data);
54+
$address = (array)RegionMatcher::parseAddress($addr, $unknownValue, $level1Data, $level2Data, $level3Data);
5555
// 更新地址信息
56-
$data['province'] = $address['province'];
57-
$data['city'] = $address['city'];
58-
$data['region'] = $address['region'];
56+
$data['province'] = $address['province'] ?? '';
57+
$data['city'] = $address['city'] ?? '';
58+
$data['region'] = $address['region'] ?? '';
5959
$data['street'] = trim(str_replace(
6060
[$data['region'], $data['city'], $data['province']],
6161
['', '', ''],

src/Support/RegionMatcher.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ public static function parseParsedAddress(
145145
if ($regionInput === '') {
146146
return $result;
147147
}
148-
$regionMatches = array_filter($level3Data, fn($v) => mb_strpos($v['name'], $regionInput) !== false);
148+
$regionMatches = array_filter($level3Data, function ($v) use ($regionInput) {
149+
return mb_strpos($v['name'], $regionInput) !== false;
150+
});
149151
if (count($regionMatches) > 1) {
150152
$result = self::handleMultipleRegionMatches($regionMatches, $cityInput, $unknownValue, $level1Data, $level2Data, $regionInput);
151153
} elseif (count($regionMatches) === 1) {
@@ -179,7 +181,9 @@ private static function handleMultipleRegionMatches(array $regionMatches, string
179181
'city' => $unknownValue,
180182
'region' => $unknownValue
181183
];
182-
$cityMatches = array_filter($level2Data, fn($v) => mb_strpos($v['name'], $cityInput) !== false);
184+
$cityMatches = array_filter($level2Data, function ($v) use ($cityInput) {
185+
return mb_strpos($v['name'], $cityInput) !== false;
186+
});
183187
if (count($cityMatches) > 1) {
184188
foreach ($regionMatches as $regionMatch) {
185189
foreach ($cityMatches as $id => $city) {
@@ -206,7 +210,10 @@ private static function handleMultipleRegionMatches(array $regionMatches, string
206210
}
207211
}
208212
if (count($cityMatches) === 0) {
209-
$provinceMatches = array_filter($level1Data, fn($v) => mb_strpos($v['name'], $cityInput) !== false);
213+
$provinceMatches = array_filter($level1Data, function ($v) use ($cityInput) {
214+
return mb_strpos($v['name'], $cityInput) !== false;
215+
});
216+
210217
if (count($provinceMatches) > 1) {
211218
foreach ($regionMatches as $regionMatch) {
212219
$pidPrefix = substr($regionMatch['pid'], 0, 2);
@@ -295,7 +302,9 @@ private static function handleSingleRegionMatch(array $region, string $unknownVa
295302
*/
296303
private static function handleNoRegionMatch(string $cityInput, string $regionInput, string $unknownValue, array $level1Data, array $level2Data): array
297304
{
298-
$cityMatches = array_filter($level2Data, fn($v) => mb_strpos($v['name'], $cityInput) !== false);
305+
$cityMatches = array_filter($level2Data, function ($v) use ($cityInput) {
306+
return mb_strpos($v['name'], $cityInput) !== false;
307+
});
299308

300309
if (count($cityMatches) > 0) {
301310
$first = array_values($cityMatches)[0];
@@ -305,20 +314,21 @@ private static function handleNoRegionMatch(string $cityInput, string $regionInp
305314
'region' => $regionInput
306315
];
307316
}
308-
309-
$provinceMatches = array_filter($level1Data, fn($v) => mb_strpos($v['name'], $cityInput) !== false);
310-
317+
$provinceMatches = array_filter($level1Data, function ($v) use ($cityInput) {
318+
return mb_strpos($v['name'], $cityInput) !== false;
319+
});
311320
if (count($provinceMatches) > 0) {
312321
foreach ($provinceMatches as $id => $province) {
313-
$citiesUnderProvince = array_filter($level2Data, fn($v) => $v['pid'] == $id);
322+
$citiesUnderProvince = array_filter($level2Data, function ($v) use ($id) {
323+
return $v['pid'] == $id;
324+
});
314325
return [
315326
'province' => $province['name'],
316327
'city' => count($citiesUnderProvince) === 0 ? '' : $unknownValue,
317328
'region' => $regionInput
318329
];
319330
}
320331
}
321-
322332
return [
323333
'province' => $unknownValue,
324334
'city' => $cityInput,

0 commit comments

Comments
 (0)