forked from jxlwqq/id-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecker.php
More file actions
100 lines (91 loc) · 2.3 KB
/
Checker.php
File metadata and controls
100 lines (91 loc) · 2.3 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
<?php
namespace Jxlwqq\IdValidator;
/**
* Trait Checker.
*/
trait Checker
{
/**
* 检查并拆分身份证号.
*
* @param string $id 身份证号
*
* @return array|bool
*/
private function _checkIdArgument($id)
{
$id = strtoupper($id);
$length = strlen($id);
$code = false;
switch ($length) {
case 18:
$code = [
'body' => substr($id, 0, 17),
'addressCode' => substr($id, 0, 6),
'birthdayCode' => substr($id, 6, 8),
'order' => substr($id, 14, 3),
'checkBit' => substr($id, -1),
'type' => 18,
];
break;
case 15:
$code = [
'body' => $id,
'addressCode' => substr($id, 0, 6),
'birthdayCode' => '19'.substr($id, 6, 6),
'order' => substr($id, 12, 3),
'checkBit' => '',
'type' => 15,
];
break;
}
return $code;
}
/**
* 检查地址码
*
* @param string $addressCode 地址码
*
* @return bool
*/
private function _checkAddressCode($addressCode)
{
$addressInfo = $this->_getAddressInfo($addressCode);
return $addressInfo ? true : false;
}
/**
* 检查顺序码
*
* @param string $orderCode 顺序码
*
* @return bool
*/
private function _checkOrderCode($orderCode)
{
if (strlen($orderCode) == 3) {
return true;
} else {
return false;
}
}
/**
* 检查出生日期码
*
* @param string $birthdayCode 出生日期码
*
* @return bool
*/
private function _checkBirthdayCode($birthdayCode)
{
$year = intval(substr($birthdayCode, 0, 4));
$month = intval(substr($birthdayCode, 4, 2));
$day = intval(substr($birthdayCode, -2));
if ($year < 1800) {
return false;
}
if ($month > 12 || $month === 0 || $day > 31 || $day === 0) {
return false;
}
return true;
}
}