jxlwqq /
id-validator
| 1 | <?php |
||||
| 2 | |||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 3 | namespace Jxlwqq\IdValidator; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Class IdValidator. |
||||
| 7 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 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 | $additionalAddressCodeTimeline = include __DIR__.'/../data/additionalAddressCodeTimeline.php'; |
||||
| 24 | $this->_addressCodeTimeline = $additionalAddressCodeTimeline + $this->_addressCodeTimeline; |
||||
| 25 | } |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * 验证身份证号合法性. |
||||
| 29 | * |
||||
| 30 | * @param string $id 身份证号 |
||||
| 31 | * @param bool $strictMode 是否启动严格模式检查 |
||||
| 32 | * |
||||
| 33 | * @return bool |
||||
| 34 | */ |
||||
| 35 | public function isValid($id, $strictMode = false) |
||||
| 36 | { |
||||
| 37 | // 基础验证 |
||||
| 38 | $code = $this->_checkIdArgument($id); |
||||
| 39 | if (empty($code)) { |
||||
| 40 | return false; |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | // 分别验证:*地址码*、*出生日期码*和*顺序码* |
||||
| 44 | if (!$this->_checkAddressCode($code['addressCode'], $code['birthdayCode'], $strictMode) || !$this->_checkBirthdayCode($code['birthdayCode']) || !$this->_checkOrderCode($code['order'])) { |
||||
| 45 | return false; |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | // 15位身份证不含校验码 |
||||
| 49 | if ($code['type'] === 15) { |
||||
| 50 | return true; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | // 验证:校验码 |
||||
| 54 | $checkBit = $this->_generatorCheckBit($code['body']); |
||||
| 55 | |||||
| 56 | // 检查校验码 |
||||
| 57 | return $checkBit == $code['checkBit']; |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * 获取身份证信息. |
||||
| 62 | * |
||||
| 63 | * @param string $id 身份证号 |
||||
| 64 | * @param bool $strictMode 是否启动严格模式检查 |
||||
| 65 | * |
||||
| 66 | * @return array|bool |
||||
| 67 | */ |
||||
| 68 | public function getInfo($id, $strictMode = false) |
||||
| 69 | { |
||||
| 70 | // 验证有效性 |
||||
| 71 | if ($this->isValid($id, $strictMode) === false) { |
||||
| 72 | return false; |
||||
| 73 | } |
||||
| 74 | $code = $this->_checkIdArgument($id); |
||||
| 75 | $addressInfo = $this->_getAddressInfo($code['addressCode'], $code['birthdayCode'], $strictMode); |
||||
| 76 | |||||
| 77 | return [ |
||||
| 78 | 'addressCode' => $code['addressCode'], |
||||
| 79 | 'abandoned' => isset($this->_addressCodeList[$code['addressCode']]) ? 0 : 1, |
||||
| 80 | 'address' => is_array($addressInfo) ? implode($addressInfo) : '', |
||||
| 81 | 'addressTree' => array_values($addressInfo), |
||||
|
0 ignored issues
–
show
It seems like
$addressInfo can also be of type false; however, parameter $array of array_values() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 82 | 'birthdayCode' => date('Y-m-d', strtotime($code['birthdayCode'])), |
||||
| 83 | 'constellation' => $this->_getConstellation($code['birthdayCode']), |
||||
| 84 | 'chineseZodiac' => $this->_getChineseZodiac($code['birthdayCode']), |
||||
| 85 | 'sex' => ($code['order'] % 2 === 0 ? 0 : 1), |
||||
| 86 | 'length' => $code['type'], |
||||
| 87 | 'checkBit' => $code['checkBit'], |
||||
| 88 | ]; |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * * 生成假数据. |
||||
| 93 | * |
||||
| 94 | * @param bool $isEighteen 是否为 18 位 |
||||
| 95 | * @param null|string $address 地址 |
||||
| 96 | * @param null|string|int $birthday 出生日期 |
||||
| 97 | * @param null|int $sex 性别(1为男性,0位女性) |
||||
| 98 | * |
||||
| 99 | * @return string |
||||
| 100 | */ |
||||
| 101 | public function fakeId($isEighteen = true, $address = null, $birthday = null, $sex = null) |
||||
| 102 | { |
||||
| 103 | // 生成地址码 |
||||
| 104 | if (empty($address)) { |
||||
| 105 | $addressCode = array_rand($this->_addressCodeList); |
||||
| 106 | $address = $this->_addressCodeList[$addressCode]; |
||||
| 107 | } else { |
||||
| 108 | $addressCode = $this->_generatorAddressCode($address); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | // 出生日期码 |
||||
| 112 | $birthdayCode = $this->_generatorBirthdayCode($addressCode, $address, $birthday); |
||||
| 113 | |||||
| 114 | // 顺序码 |
||||
| 115 | $orderCode = $this->_generatorOrderCode($sex); |
||||
| 116 | |||||
| 117 | if (!$isEighteen) { |
||||
| 118 | return $addressCode.substr($birthdayCode, 2).$orderCode; |
||||
|
0 ignored issues
–
show
Are you sure
$addressCode of type array|integer|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 119 | } |
||||
| 120 | |||||
| 121 | $body = $addressCode.$birthdayCode.$orderCode; |
||||
| 122 | |||||
| 123 | $checkBit = $this->_generatorCheckBit($body); |
||||
| 124 | |||||
| 125 | return $body.$checkBit; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * 15位升级18位号码. |
||||
| 130 | * |
||||
| 131 | * @param string $id 身份证号 |
||||
| 132 | * |
||||
| 133 | * @return bool|string |
||||
| 134 | */ |
||||
| 135 | public function upgradeId($id) |
||||
| 136 | { |
||||
| 137 | if (!$this->isValid($id)) { |
||||
| 138 | return false; |
||||
| 139 | } |
||||
| 140 | $code = $this->_generateShortType($id); |
||||
| 141 | $body = $code['addressCode'].$code['birthdayCode'].$code['order']; |
||||
| 142 | |||||
| 143 | return $body.$this->_generatorCheckBit($body); |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 |