Passed
Pull Request — master (#49)
by jxlwqq
18:40
created

IdValidator::fakeId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 4
b 0
f 0
nc 2
nop 4
dl 0
loc 20
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jxlwqq\IdValidator;
4
5
/**
6
 * Class IdValidator.
7
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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),
0 ignored issues
show
Bug introduced by
It seems like $addressInfo can also be of type false; however, parameter $input 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 ignore-type  annotation

77
            'addressTree'   => array_values(/** @scrutinizer ignore-type */ $addressInfo),
Loading history...
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;
0 ignored issues
show
Bug introduced by
Are you sure $addressCode of type false|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 ignore-type  annotation

109
            return /** @scrutinizer ignore-type */ $addressCode.substr($birthdayCode, 2).$orderCode;
Loading history...
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