forked from jxlwqq/id-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdValidator.php
More file actions
126 lines (107 loc) · 3.2 KB
/
IdValidator.php
File metadata and controls
126 lines (107 loc) · 3.2 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
<?php
namespace Jxlwqq\IdValidator;
/**
* Class IdValidator.
*/
class IdValidator
{
use Helper;
private $_addressCodeList = [];
private $_constellationList = [];
private $_chineseZodiacList = [];
/**
* IdValidator constructor.
*/
public function __construct()
{
$this->_addressCodeList = include __DIR__.'/../data/addressCode.php';
$this->_constellationList = include __DIR__.'/../data/constellation.php';
$this->_chineseZodiacList = include __DIR__.'/../data/chineseZodiac.php';
}
/**
* 验证身份证号合法性.
*
* @param string $id 身份证号
*
* @return bool
*/
public function isValid($id)
{
// 基础验证
$code = $this->_checkIdArgument($id);
if (empty($code)) {
return false;
}
// 验证:地址码
if (!$this->_checkAddressCode($code['addressCode'])) {
return false;
}
// 验证:出生日期码
if (!$this->_checkBirthdayCode($code['birthdayCode'])) {
return false;
}
// 验证:顺序码
if (!$this->_checkOrderCode($code['order'])) {
return false;
}
// 15位身份证不含校验码
if ($code['type'] === 15) {
return true;
}
// 验证:校验码
$checkBit = $this->_generatorCheckBit($code['body']);
// 检查校验码
if ($checkBit != $code['checkBit']) {
return false;
} else {
return true;
}
}
/**
* 获取身份证信息.
*
* @param string $id 身份证号
*
* @return array|bool
*/
public function getInfo($id)
{
// 验证有效性
if ($this->isValid($id) === false) {
return false;
}
$code = $this->_checkIdArgument($id);
$addressInfo = $this->_getAddressInfo($code['addressCode']);
$info = [];
$info['addressCode'] = $code['addressCode'];
$info['address'] = is_array($addressInfo) ? implode($addressInfo) : '';
$info['birthdayCode'] = date('Y-m-d', strtotime($code['birthdayCode']));
$info['constellation'] = $this->_getConstellation($code['birthdayCode']);
$info['chineseZodiac'] = $this->_getChineseZodiac($code['birthdayCode']);
$info['sex'] = ($code['order'] % 2 === 0 ? 0 : 1);
$info['length'] = $code['type'];
$info['checkBit'] = $code['checkBit'];
return $info;
}
/**
* 生成假数据.
*
* @param bool $eighteen 是否为 18 位
*
* @return string
*/
public function fakeId($eighteen = true)
{
// 生成地址码
$addressCode = $this->_generatorAddressCode();
// 出生日期码
$birthdayCode = $this->_generatorBirthdayCode();
if (!$eighteen) {
return $addressCode.substr($birthdayCode, 2)
.$this->_getStrPad($this->_generatorRandInt(999, 1), 3, '1');
}
$body = $addressCode.$birthdayCode.$this->_getStrPad($this->_generatorRandInt(999, 1), 3, '1');
$checkBit = $this->_generatorCheckBit($body);
return $body.$checkBit;
}
}