Skip to content

Commit bea3257

Browse files
committed
Refactor
1 parent 51ae2f1 commit bea3257

File tree

1 file changed

+81
-75
lines changed

1 file changed

+81
-75
lines changed

src/IdValidator.php

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: jxlwqq
5-
* Date: 2018/9/6
6-
* Time: 19:03.
7-
*/
82

93
namespace Jxlwqq\IdValidator;
104

5+
/**
6+
* Class IdValidator
7+
* @package Jxlwqq\IdValidator
8+
*/
119
class IdValidator
1210
{
13-
private $addressCodeList = [];
11+
private $_addressCodeList = [];
1412

13+
/**
14+
* IdValidator constructor.
15+
*/
1516
public function __construct()
1617
{
17-
$this->addressCodeList = require __DIR__.'/../data/addressCode.php';
18+
$this->_addressCodeList = include __DIR__ . '/../data/addressCode.php';
1819
}
1920

2021
/**
@@ -27,23 +28,23 @@ public function __construct()
2728
public function isValid($id)
2829
{
2930
// 基础验证
30-
$code = $this->checkIdArgument($id);
31+
$code = $this->_checkIdArgument($id);
3132
if (!$code) {
3233
return false;
3334
}
3435

3536
// 验证:地址码
36-
if (!$this->checkAddressCode($code['addressCode'])) {
37+
if (!$this->_checkAddressCode($code['addressCode'])) {
3738
return false;
3839
}
3940

4041
// 验证:出生日期码
41-
if (!$this->checkBirthdayCode($code['birthdayCode'])) {
42+
if (!$this->_checkBirthdayCode($code['birthdayCode'])) {
4243
return false;
4344
}
4445

4546
// 验证:顺序码
46-
if (!$this->checkOrderCode($code['order'])) {
47+
if (!$this->_checkOrderCode($code['order'])) {
4748
return false;
4849
}
4950

@@ -78,58 +79,56 @@ public function getInfo($id)
7879
if ($this->isValid($id) === false) {
7980
return false;
8081
}
81-
$code = $this->checkIdArgument($id);
82+
$code = $this->_checkIdArgument($id);
8283
$info = [];
8384
$info['addressCode'] = $code['addressCode'];
84-
$info['address'] = implode($this->getAddressInfo($code['addressCode']));
85+
$info['address'] = implode($this->_getAddressInfo($code['addressCode']));
8586
$info['birthdayCode'] = date('Y-m-d', strtotime($code['birthdayCode']));
8687
$info['sex'] = ($code['order'] % 2 === 0 ? 0 : 1);
8788
$info['length'] = $code['type'];
88-
if ($code['type'] === 18) {
89-
$info['checkBit'] = $code['checkBit'];
90-
}
89+
$info['checkBit'] = $code['checkBit'];
9190

9291
return $info;
9392
}
9493

9594
/**
9695
* 生成假数据.
9796
*
98-
* @param bool $fifteen
97+
* @param bool $eighteen
9998
*
10099
* @return string
101100
*/
102-
public function fakeId($fifteen = false)
101+
public function fakeId($eighteen = true)
103102
{
104103
// 生成地址码
105-
$addressCode = $this->generatorAddressCode();
104+
$addressCode = $this->_generatorAddressCode();
106105

107106
// 出生日期码
108-
$birthdayCode = $this->generatorBirthdayCode();
107+
$birthdayCode = $this->_generatorBirthdayCode();
109108

110-
if ($fifteen) {
111-
return $addressCode.substr($birthdayCode, 2)
112-
.$this->getStrPad($this->generatorRandInt(999, 1), 3, '1');
109+
if (!$eighteen) {
110+
return $addressCode . substr($birthdayCode, 2)
111+
. $this->_getStrPad($this->_generatorRandInt(999, 1), 3, '1');
113112
}
114113

115-
$body = $addressCode.$birthdayCode.$this->getStrPad($this->generatorRandInt(999, 1), 3, '1');
114+
$body = $addressCode . $birthdayCode . $this->_getStrPad($this->_generatorRandInt(999, 1), 3, '1');
116115

117116
$checkBit = $this->generatorCheckBit($body);
118117

119-
return $body.$checkBit;
118+
return $body . $checkBit;
120119
}
121120

122121
/**
123122
* 获取位置加权.
124123
*
125124
* @return array
126125
*/
127-
private function getPosWeight()
126+
private function _getPosWeight()
128127
{
129128
$posWeight = [];
130129
for ($i = 18; $i > 1; $i--) {
131-
$wei = pow(2, $i - 1) % 11;
132-
$posWeight[$i] = $wei;
130+
$weight = pow(2, $i - 1) % 11;
131+
$posWeight[$i] = $weight;
133132
}
134133

135134
return $posWeight;
@@ -139,23 +138,23 @@ private function getPosWeight()
139138
* 获取数字补位.
140139
*
141140
* @param $str
142-
* @param int $len
141+
* @param int $len
143142
* @param string $chr
144-
* @param bool $right
143+
* @param bool $right
145144
*
146145
* @return string
147146
*/
148-
private function getStrPad($str, $len = 2, $chr = '0', $right = false)
147+
private function _getStrPad($str, $len = 2, $chr = '0', $right = false)
149148
{
150149
$str = strval($str);
151150
if (strlen($str) >= $len) {
152151
return $str;
153152
} else {
154153
for ($i = 0, $j = $len - strlen($str); $i < $j; $i++) {
155154
if ($right) {
156-
$str = $str.$chr;
155+
$str = $str . $chr;
157156
} else {
158-
$str = $chr.$str;
157+
$str = $chr . $str;
159158
}
160159
}
161160

@@ -170,24 +169,24 @@ private function getStrPad($str, $len = 2, $chr = '0', $right = false)
170169
*
171170
* @return bool|mixed|string
172171
*/
173-
private function getAddressInfo($addressCode)
172+
private function _getAddressInfo($addressCode)
174173
{
175174
$addressInfo = [];
176175
// 省级信息
177-
$provinceAddressCode = substr($addressCode, 0, 2).'0000';
178-
$addressInfo['province'] = isset($this->addressCodeList[$provinceAddressCode]) ? $this->addressCodeList[$provinceAddressCode] : '';
176+
$provinceAddressCode = substr($addressCode, 0, 2) . '0000';
177+
$addressInfo['province'] = isset($this->_addressCodeList[$provinceAddressCode]) ? $this->_addressCodeList[$provinceAddressCode] : '';
179178

180179
// 市级信息
181-
$cityAddressCode = substr($addressCode, 0, 4).'00';
182-
$addressInfo['city'] = isset($this->addressCodeList[$cityAddressCode]) ? $this->addressCodeList[$cityAddressCode] : '';
180+
$cityAddressCode = substr($addressCode, 0, 4) . '00';
181+
$addressInfo['city'] = isset($this->_addressCodeList[$cityAddressCode]) ? $this->_addressCodeList[$cityAddressCode] : '';
183182

184183
// 县级信息
185-
$addressInfo['district'] = isset($this->addressCodeList[$addressCode]) ? $this->addressCodeList[$addressCode] : '';
184+
$addressInfo['district'] = isset($this->_addressCodeList[$addressCode]) ? $this->_addressCodeList[$addressCode] : '';
186185

187-
if ($addressInfo) {
188-
return $addressInfo;
189-
} else {
186+
if (empty($addressInfo)) {
190187
return false;
188+
} else {
189+
return $addressInfo;
191190
}
192191
}
193192

@@ -198,29 +197,30 @@ private function getAddressInfo($addressCode)
198197
*
199198
* @return array|bool
200199
*/
201-
private function checkIdArgument($id)
200+
private function _checkIdArgument($id)
202201
{
203202
$id = strtoupper($id);
204203
$length = strlen($id);
205204
$code = false;
206205
switch ($length) {
207206
case 18:
208207
$code = [
209-
'body' => substr($id, 0, 17),
210-
'addressCode' => substr($id, 0, 6),
208+
'body' => substr($id, 0, 17),
209+
'addressCode' => substr($id, 0, 6),
211210
'birthdayCode' => substr($id, 6, 8),
212-
'order' => substr($id, 14, 3),
213-
'checkBit' => substr($id, -1),
214-
'type' => 18,
211+
'order' => substr($id, 14, 3),
212+
'checkBit' => substr($id, -1),
213+
'type' => 18,
215214
];
216215
break;
217216
case 15:
218217
$code = [
219-
'body' => $id,
220-
'addressCode' => substr($id, 0, 6),
221-
'birthdayCode' => '19'.substr($id, 6, 6),
222-
'order' => substr($id, 12, 3),
223-
'type' => 15,
218+
'body' => $id,
219+
'addressCode' => substr($id, 0, 6),
220+
'birthdayCode' => '19' . substr($id, 6, 6),
221+
'order' => substr($id, 12, 3),
222+
'checkBit' => '',
223+
'type' => 15,
224224
];
225225
break;
226226
}
@@ -235,9 +235,9 @@ private function checkIdArgument($id)
235235
*
236236
* @return bool
237237
*/
238-
private function checkAddressCode($addressCode)
238+
private function _checkAddressCode($addressCode)
239239
{
240-
$addressInfo = $this->getAddressInfo($addressCode);
240+
$addressInfo = $this->_getAddressInfo($addressCode);
241241

242242
return $addressInfo ? true : false;
243243
}
@@ -249,7 +249,7 @@ private function checkAddressCode($addressCode)
249249
*
250250
* @return bool
251251
*/
252-
private function checkBirthdayCode($birthdayCode)
252+
private function _checkBirthdayCode($birthdayCode)
253253
{
254254
$year = intval(substr($birthdayCode, 0, 4));
255255
$month = intval(substr($birthdayCode, 4, 2));
@@ -272,10 +272,15 @@ private function checkBirthdayCode($birthdayCode)
272272
*
273273
* @return bool
274274
*/
275-
private function checkOrderCode($orderCode)
275+
private function _checkOrderCode($orderCode)
276276
{
277-
// 暂无需检测
278-
return true;
277+
if (strlen($orderCode) == 3) {
278+
return true;
279+
} else {
280+
return false;
281+
}
282+
283+
279284
}
280285

281286
/**
@@ -286,7 +291,7 @@ private function checkOrderCode($orderCode)
286291
*
287292
* @return int
288293
*/
289-
private function generatorRandInt($max, $min = 1)
294+
private function _generatorRandInt($max, $min = 1)
290295
{
291296
return rand($min, $max);
292297
}
@@ -301,12 +306,13 @@ private function generatorRandInt($max, $min = 1)
301306
private function generatorCheckBit($body)
302307
{
303308
// 位置加权
304-
$posWeight = $this->getPosWeight();
309+
$posWeight = $this->_getPosWeight();
305310

306311
// 累身份证号 body 部分与位置加权的积
307312
$bodySum = 0;
308313
$bodyArray = str_split($body);
309-
for ($j = 0; $j < count($bodyArray); $j++) {
314+
$count = count($bodyArray);
315+
for ($j = 0; $j < $count; $j++) {
310316
$bodySum += (intval($bodyArray[$j], 10) * $posWeight[18 - $j]);
311317
}
312318

@@ -326,15 +332,15 @@ private function generatorCheckBit($body)
326332
*
327333
* @return string
328334
*/
329-
private function generatorAddressCode()
335+
private function _generatorAddressCode()
330336
{
331337
$addressCode = '110100';
332338
for ($i = 0; $i < 100; $i++) {
333-
$province = $this->getStrPad($this->generatorRandInt(66), 2, '0');
334-
$city = $this->getStrPad($this->generatorRandInt(20), 2, '0');
335-
$district = $this->getStrPad($this->generatorRandInt(20), 2, '0');
336-
$fakeAddressCode = $province.$city.$district;
337-
if (isset($this->addressCodeList[$fakeAddressCode])) {
339+
$province = $this->_getStrPad($this->_generatorRandInt(66), 2, '0');
340+
$city = $this->_getStrPad($this->_generatorRandInt(20), 2, '0');
341+
$district = $this->_getStrPad($this->_generatorRandInt(20), 2, '0');
342+
$fakeAddressCode = $province . $city . $district;
343+
if (isset($this->_addressCodeList[$fakeAddressCode])) {
338344
$addressCode = $fakeAddressCode;
339345
break;
340346
}
@@ -348,13 +354,13 @@ private function generatorAddressCode()
348354
*
349355
* @return string
350356
*/
351-
private function generatorBirthdayCode()
357+
private function _generatorBirthdayCode()
352358
{
353-
$year = $this->getStrPad($this->generatorRandInt(99, 50), 2, '0');
354-
$month = $this->getStrPad($this->generatorRandInt(12, 1), 2, '0');
355-
$day = $this->getStrPad($this->generatorRandInt(28, 1), 2, '0');
356-
$year = '19'.$year;
357-
$birthdayCode = $year.$month.$day;
359+
$year = $this->_getStrPad($this->_generatorRandInt(99, 50), 2, '0');
360+
$month = $this->_getStrPad($this->_generatorRandInt(12, 1), 2, '0');
361+
$day = $this->_getStrPad($this->_generatorRandInt(28, 1), 2, '0');
362+
$year = '19' . $year;
363+
$birthdayCode = $year . $month . $day;
358364

359365
return $birthdayCode;
360366
}

0 commit comments

Comments
 (0)