|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jxlwqq\IdValidator; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class IdValidator. |
|
7
|
|
|
*/ |
|
|
|
|
|
|
8
|
|
|
class IdValidator |
|
9
|
|
|
{ |
|
10
|
|
|
use Helper; |
|
11
|
|
|
use Generator; |
|
12
|
|
|
use Checker; |
|
13
|
|
|
private $_addressCodeList = []; // 现行地址码数据 |
|
14
|
|
|
private $_addressCodeTimeline = []; // 地址码变更时间线 |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* IdValidator constructor. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->_addressCodeList = include __DIR__.'/../data/addressCode.php'; |
|
22
|
|
|
$this->_addressCodeTimeline = include __DIR__.'/../data/addressCodeTimeline.php'; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* 验证身份证号合法性. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $id 身份证号 |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public function isValid($id) |
|
33
|
|
|
{ |
|
34
|
|
|
// 基础验证 |
|
35
|
|
|
$code = $this->_checkIdArgument($id); |
|
36
|
|
|
if (empty($code)) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// 分别验证:*地址码*、*出生日期码*和*顺序码* |
|
41
|
|
|
if (!$this->_checkAddressCode($code['addressCode'], $code['birthdayCode']) || !$this->_checkBirthdayCode($code['birthdayCode']) || !$this->_checkOrderCode($code['order'])) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// 15位身份证不含校验码 |
|
46
|
|
|
if ($code['type'] === 15) { |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// 验证:校验码 |
|
51
|
|
|
$checkBit = $this->_generatorCheckBit($code['body']); |
|
52
|
|
|
|
|
53
|
|
|
// 检查校验码 |
|
54
|
|
|
return $checkBit == $code['checkBit']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* 获取身份证信息. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $id 身份证号 |
|
61
|
|
|
* |
|
62
|
|
|
* @return array|bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getInfo($id) |
|
65
|
|
|
{ |
|
66
|
|
|
// 验证有效性 |
|
67
|
|
|
if ($this->isValid($id) === false) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
$code = $this->_checkIdArgument($id); |
|
71
|
|
|
$addressInfo = $this->_getAddressInfo($code['addressCode'], $code['birthdayCode']); |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
'addressCode' => $code['addressCode'], |
|
75
|
|
|
'abandoned' => isset($this->_addressCodeList[$code['addressCode']]) ? 0 : 1, |
|
76
|
|
|
'address' => is_array($addressInfo) ? implode($addressInfo) : '', |
|
77
|
|
|
'addressTree' => array_values($addressInfo), |
|
|
|
|
|
|
78
|
|
|
'birthdayCode' => date('Y-m-d', strtotime($code['birthdayCode'])), |
|
79
|
|
|
'constellation' => $this->_getConstellation($code['birthdayCode']), |
|
80
|
|
|
'chineseZodiac' => $this->_getChineseZodiac($code['birthdayCode']), |
|
81
|
|
|
'sex' => ($code['order'] % 2 === 0 ? 0 : 1), |
|
82
|
|
|
'length' => $code['type'], |
|
83
|
|
|
'checkBit' => $code['checkBit'], |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* * 生成假数据. |
|
89
|
|
|
* |
|
90
|
|
|
* @param bool $isEighteen 是否为 18 位 |
|
91
|
|
|
* @param null|string $address 地址 |
|
92
|
|
|
* @param null|string|int $birthday 出生日期 |
|
93
|
|
|
* @param null|int $sex 性别(1为男性,0位女性) |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function fakeId($isEighteen = true, $address = null, $birthday = null, $sex = null) |
|
98
|
|
|
{ |
|
99
|
|
|
// 生成地址码 |
|
100
|
|
|
$addressCode = $this->_generatorAddressCode($address); |
|
101
|
|
|
|
|
102
|
|
|
// 出生日期码 |
|
103
|
|
|
$birthdayCode = $this->_generatorBirthdayCode($birthday); |
|
104
|
|
|
|
|
105
|
|
|
// 顺序码 |
|
106
|
|
|
$orderCode = $this->_generatorOrderCode($sex); |
|
107
|
|
|
|
|
108
|
|
|
if (!$isEighteen) { |
|
109
|
|
|
return $addressCode.substr($birthdayCode, 2).$orderCode; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$body = $addressCode.$birthdayCode.$orderCode; |
|
113
|
|
|
|
|
114
|
|
|
$checkBit = $this->_generatorCheckBit($body); |
|
115
|
|
|
|
|
116
|
|
|
return $body.$checkBit; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* 15位升级18位号码. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $id 身份证号 |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool|string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function upgradeId($id) |
|
127
|
|
|
{ |
|
128
|
|
|
if (!$this->isValid($id)) { |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
$code = $this->_generateShortType($id); |
|
132
|
|
|
$body = $code['addressCode'].$code['birthdayCode'].$code['order']; |
|
133
|
|
|
|
|
134
|
|
|
return $body.$this->_generatorCheckBit($body); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|