|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jxlwqq\IdValidator; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait Helper. |
|
7
|
|
|
*/ |
|
|
|
|
|
|
8
|
|
|
trait Helper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* 获取地址码信息. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $addressCode 地址码 |
|
14
|
|
|
* @param string $birthdayCode 出生日期码 |
|
15
|
|
|
* |
|
16
|
|
|
* @return bool|mixed|string |
|
17
|
|
|
*/ |
|
18
|
|
|
private function _getAddressInfo($addressCode, $birthdayCode) |
|
19
|
|
|
{ |
|
20
|
|
|
$addressInfo = [ |
|
21
|
|
|
'province' => '', |
|
22
|
|
|
'city' => '', |
|
23
|
|
|
'district' => '', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
// 省级信息 |
|
27
|
|
|
$provinceAddressCode = substr($addressCode, 0, 2).'0000'; |
|
28
|
|
|
$addressInfo['province'] = $this->_getAddress($provinceAddressCode, $birthdayCode); |
|
29
|
|
|
|
|
30
|
|
|
$firstCharacter = substr($addressCode, 0, 1); // 用于判断是否是港澳台居民居住证(8字开头) |
|
31
|
|
|
|
|
32
|
|
|
// 港澳台居民居住证无市级、县级信息 |
|
33
|
|
|
if ($firstCharacter == '8') { |
|
34
|
|
|
return $addressInfo; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// 市级信息 |
|
38
|
|
|
$cityAddressCode = substr($addressCode, 0, 4).'00'; |
|
39
|
|
|
$addressInfo['city'] = $this->_getAddress($cityAddressCode, $birthdayCode); |
|
40
|
|
|
|
|
41
|
|
|
// 县级信息 |
|
42
|
|
|
$addressInfo['district'] = $this->_getAddress($addressCode, $birthdayCode); |
|
43
|
|
|
|
|
44
|
|
|
return empty($addressInfo) ? false : $addressInfo; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 获取省市区地址码. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $addressCode 地址码 |
|
51
|
|
|
* @param string $birthdayCode 出生日期码 |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
private function _getAddress($addressCode, $birthdayCode) |
|
56
|
|
|
{ |
|
57
|
|
|
$address = ''; |
|
58
|
|
|
if (isset($this->_addressCodeTimeline[$addressCode])) { |
|
59
|
|
|
$timeline = $this->_addressCodeTimeline[$addressCode]; |
|
60
|
|
|
$firstStartYear = $timeline[0]['start_year']; |
|
61
|
|
|
$year = substr($birthdayCode, 0, 4); |
|
62
|
|
|
if ($year < $firstStartYear) { |
|
63
|
|
|
$address = $timeline[0]['address']; |
|
64
|
|
|
} else { |
|
65
|
|
|
foreach ($timeline as $val) { |
|
66
|
|
|
if ($year >= $val['start_year']) { |
|
67
|
|
|
$address = $val['address']; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $address; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* 获取星座信息. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $birthdayCode 出生日期码 |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
private function _getConstellation($birthdayCode) |
|
84
|
|
|
{ |
|
85
|
|
|
$constellationList = include __DIR__.'/../data/constellation.php'; |
|
86
|
|
|
$month = (int) substr($birthdayCode, 4, 2); |
|
87
|
|
|
$day = (int) substr($birthdayCode, 6, 2); |
|
88
|
|
|
|
|
89
|
|
|
$start_date = $constellationList[$month]['start_date']; |
|
90
|
|
|
$start_day = (int) explode('-', $start_date)[1]; |
|
91
|
|
|
|
|
92
|
|
|
if ($day < $start_day) { |
|
93
|
|
|
$tmp_month = $month == 1 ? 12 : $month - 1; |
|
94
|
|
|
|
|
95
|
|
|
return $constellationList[$tmp_month]['name']; |
|
96
|
|
|
} else { |
|
97
|
|
|
return $constellationList[$month]['name']; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* 获取生肖信息. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $birthdayCode 出生日期码 |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
private function _getChineseZodiac($birthdayCode) |
|
109
|
|
|
{ |
|
110
|
|
|
$chineseZodiacList = include __DIR__.'/../data/chineseZodiac.php'; |
|
111
|
|
|
$start = 1900; // 子鼠 |
|
112
|
|
|
$end = substr($birthdayCode, 0, 4); |
|
113
|
|
|
$key = ($end - $start) % 12; |
|
114
|
|
|
|
|
115
|
|
|
return $chineseZodiacList[$key]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* 获取数字补位. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string|int $str 字符串 |
|
123
|
|
|
* @param int $len 长度 |
|
124
|
|
|
* @param string $chr 补位值 |
|
125
|
|
|
* @param bool $right 左右 |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
private function _getStrPad($str, $len = 2, $chr = '0', $right = false) |
|
130
|
|
|
{ |
|
131
|
|
|
return str_pad((string) $str, $len, $chr, $right === true ? STR_PAD_RIGHT : STR_PAD_LEFT); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|