jxlwqq /
id-validator
| 1 | <?php |
||||
| 2 | |||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 3 | namespace Jxlwqq\IdValidator; |
||||
| 4 | |||||
| 5 | use DateTime; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * Trait Checker. |
||||
| 9 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 10 | trait Checker |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * 检查并拆分身份证号. |
||||
| 14 | * |
||||
| 15 | * @param string $id 身份证号 |
||||
| 16 | * |
||||
| 17 | * @return array|bool |
||||
| 18 | */ |
||||
| 19 | private function _checkIdArgument($id) |
||||
| 20 | { |
||||
| 21 | $id = strtoupper($id); |
||||
| 22 | $length = strlen($id); |
||||
| 23 | |||||
| 24 | if ($length === 15) { |
||||
| 25 | return $this->_generateShortType($id); |
||||
| 26 | } |
||||
| 27 | |||||
| 28 | if ($length === 18) { |
||||
| 29 | return $this->_generatelongType($id); |
||||
| 30 | } |
||||
| 31 | |||||
| 32 | return false; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Generation for the short type. |
||||
| 37 | * |
||||
| 38 | * @param string $id 身份证号 |
||||
| 39 | * |
||||
| 40 | * @return array |
||||
| 41 | */ |
||||
| 42 | private function _generateShortType($id) |
||||
| 43 | { |
||||
| 44 | preg_match('/(.{6})(.{6})(.{3})/', $id, $matches); |
||||
| 45 | |||||
| 46 | return [ |
||||
| 47 | 'body' => $matches[0], |
||||
| 48 | 'addressCode' => $matches[1], |
||||
| 49 | 'birthdayCode' => '19'.$matches[2], |
||||
| 50 | 'order' => $matches[3], |
||||
| 51 | 'checkBit' => '', |
||||
| 52 | 'type' => 15, |
||||
| 53 | ]; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Generation for the long type. |
||||
| 58 | * |
||||
| 59 | * @param string $id 身份证号 |
||||
| 60 | * |
||||
| 61 | * @return array |
||||
| 62 | */ |
||||
| 63 | private function _generateLongType($id) |
||||
| 64 | { |
||||
| 65 | preg_match('/((.{6})(.{8})(.{3}))(.)/', $id, $matches); |
||||
| 66 | |||||
| 67 | return [ |
||||
| 68 | 'body' => $matches[1], |
||||
| 69 | 'addressCode' => $matches[2], |
||||
| 70 | 'birthdayCode' => $matches[3], |
||||
| 71 | 'order' => $matches[4], |
||||
| 72 | 'checkBit' => $matches[5], |
||||
| 73 | 'type' => 18, |
||||
| 74 | ]; |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * 检查地址码 |
||||
| 79 | * |
||||
| 80 | * @param string $addressCode 地址码 |
||||
| 81 | * @param string $birthdayCode 出生日期码 |
||||
| 82 | * @param bool $strictMode 是否启动严格模式检查 |
||||
| 83 | * |
||||
| 84 | * @return bool |
||||
| 85 | */ |
||||
| 86 | private function _checkAddressCode($addressCode, $birthdayCode, $strictMode = false) |
||||
| 87 | { |
||||
| 88 | return (bool) $this->_getAddressInfo($addressCode, $birthdayCode, $strictMode); |
||||
|
0 ignored issues
–
show
It seems like
_getAddressInfo() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * 检查顺序码 |
||||
| 93 | * |
||||
| 94 | * @param string $orderCode 顺序码 |
||||
| 95 | * |
||||
| 96 | * @return bool |
||||
| 97 | */ |
||||
| 98 | private function _checkOrderCode($orderCode) |
||||
| 99 | { |
||||
| 100 | return strlen($orderCode) === 3; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * 检查出生日期码 |
||||
| 105 | * |
||||
| 106 | * @param string $birthdayCode 出生日期码 |
||||
| 107 | * |
||||
| 108 | * @return bool |
||||
| 109 | */ |
||||
| 110 | private function _checkBirthdayCode($birthdayCode) |
||||
| 111 | { |
||||
| 112 | $date = DateTime::createFromFormat($format = 'Ymd', $birthdayCode); |
||||
| 113 | |||||
| 114 | return $date && $date->format($format) === $birthdayCode && (int) $date->format('Y') >= 1800; |
||||
| 115 | } |
||||
| 116 | } |
||||
| 117 |