Skip to content

Commit 40875f2

Browse files
committed
Refactor
1 parent 01f0aa7 commit 40875f2

File tree

4 files changed

+380
-368
lines changed

4 files changed

+380
-368
lines changed

data/chineseZodiac.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* 生肖信息
4+
*/
5+
return ['子鼠', '丑牛', '寅虎', '卯兔', '辰龙', '巳蛇', '午马', '未羊', '申猴', '酉鸡', '戌狗', '亥猪'];

data/constellation.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* 星座信息
4+
*/
5+
6+
return [
7+
'01' => [
8+
'name' => '水瓶座',
9+
'start_date' => '01-20',
10+
'end_date' => '02-18',
11+
],
12+
'02' => [
13+
'name' => '双鱼座',
14+
'start_date' => '02-19',
15+
'end_date' => '03-20',
16+
],
17+
'03' => [
18+
'name' => '白羊座',
19+
'start_date' => '03-21',
20+
'end_date' => '04-19',
21+
],
22+
'04' => [
23+
'name' => '金牛座',
24+
'start_date' => '04-20',
25+
'end_date' => '05-20',
26+
], '05' => [
27+
'name' => '双子座',
28+
'start_date' => '05-21',
29+
'end_date' => '06-21',
30+
], '06' => [
31+
'name' => '巨蟹座',
32+
'start_date' => '06-22',
33+
'end_date' => '07-22',
34+
], '07' => [
35+
'name' => '狮子座',
36+
'start_date' => '07-23',
37+
'end_date' => '08-22',
38+
], '08' => [
39+
'name' => '处女座',
40+
'start_date' => '08-23',
41+
'end_date' => '09-22',
42+
], '09' => [
43+
'name' => '天秤座',
44+
'start_date' => '09-23',
45+
'end_date' => '10-23',
46+
], '10' => [
47+
'name' => '天蝎座',
48+
'start_date' => '10-24',
49+
'end_date' => '11-22',
50+
], '11' => [
51+
'name' => '射手座',
52+
'start_date' => '11-23',
53+
'end_date' => '12-21',
54+
], '12' => [
55+
'name' => '水瓶座',
56+
'start_date' => '12-22',
57+
'end_date' => '01-19',
58+
],
59+
];

src/Helper.php

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jxlwqq
5+
* Date: 2018/9/7
6+
* Time: 14:07
7+
*/
8+
9+
namespace Jxlwqq\IdValidator;
10+
11+
12+
trait Helper
13+
{
14+
/**
15+
* 获取位置加权.
16+
*
17+
* @return array
18+
*/
19+
private function _getPosWeight()
20+
{
21+
$posWeight = [];
22+
for ($i = 18; $i > 1; $i--) {
23+
$weight = pow(2, $i - 1) % 11;
24+
$posWeight[$i] = $weight;
25+
}
26+
27+
return $posWeight;
28+
}
29+
30+
/**
31+
* 获取数字补位.
32+
*
33+
* @param $str
34+
* @param int $len 长度
35+
* @param string $chr 补位值
36+
* @param bool $right 左右
37+
*
38+
* @return string
39+
*/
40+
private function _getStrPad($str, $len = 2, $chr = '0', $right = false)
41+
{
42+
$str = strval($str);
43+
if (strlen($str) >= $len) {
44+
return $str;
45+
} else {
46+
for ($i = 0, $j = $len - strlen($str); $i < $j; $i++) {
47+
if ($right) {
48+
$str = $str.$chr;
49+
} else {
50+
$str = $chr.$str;
51+
}
52+
}
53+
54+
return $str;
55+
}
56+
}
57+
58+
/**
59+
* 获取地址码信息.
60+
*
61+
* @param string $addressCode 地址码
62+
*
63+
* @return bool|mixed|string
64+
*/
65+
private function _getAddressInfo($addressCode)
66+
{
67+
$addressInfo = [];
68+
// 省级信息
69+
$provinceAddressCode = substr($addressCode, 0, 2).'0000';
70+
$addressInfo['province'] = isset($this->_addressCodeList[$provinceAddressCode]) ? $this->_addressCodeList[$provinceAddressCode] : '';
71+
72+
// 市级信息
73+
$cityAddressCode = substr($addressCode, 0, 4).'00';
74+
$addressInfo['city'] = isset($this->_addressCodeList[$cityAddressCode]) ? $this->_addressCodeList[$cityAddressCode] : '';
75+
76+
// 县级信息
77+
$addressInfo['district'] = isset($this->_addressCodeList[$addressCode]) ? $this->_addressCodeList[$addressCode] : '';
78+
79+
if (empty($addressInfo)) {
80+
return false;
81+
} else {
82+
return $addressInfo;
83+
}
84+
}
85+
86+
/**
87+
* 获取星座信息
88+
* @param string $birthdayCode 出生日期码
89+
* @return string
90+
*/
91+
public function _getConstellation($birthdayCode)
92+
{
93+
$time = strtotime($birthdayCode);
94+
$year = substr($birthdayCode, 0, 4);
95+
$month = substr($birthdayCode, 4, 2);
96+
$day = substr($birthdayCode, 6, 2);
97+
98+
// 1月份与12月份特殊处理
99+
if (($month == 1 && $day < 20) || ($month == 12 && $day > 21)) {
100+
return $this->_constellationList['12']['name'];
101+
} elseif ($month == 1) {
102+
return $this->_constellationList['01']['name'];
103+
} elseif ($month == 12) {
104+
return $this->_constellationList['12']['name'];
105+
}
106+
107+
$startDate = $year.'-'.$this->_constellationList[$month]['start_date'];
108+
$endDate = $year.'-'.$this->_constellationList[$month]['end_date'];
109+
if (strtotime($startDate) <= $time && strtotime($endDate) >= $time) {
110+
return $this->_constellationList[$month]['name'];
111+
}
112+
$startDate = $year.'-'.$this->_constellationList[$month - 1]['start_date'];
113+
$endDate = $year.'-'.$this->_constellationList[$month - 1]['end_date'];
114+
if (strtotime($startDate) <= $time && strtotime($endDate) >= $time) {
115+
return $this->_constellationList[$month - 1]['name'];
116+
}
117+
118+
return '';
119+
}
120+
121+
/**
122+
* 获取生肖信息
123+
* @param string $birthdayCode 出生日期码
124+
* @return mixed
125+
*/
126+
public function _getChineseZodiac($birthdayCode)
127+
{
128+
$start = 1900; // 子鼠
129+
$end = substr($birthdayCode, 0, 4);
130+
$key = ($end - $start) % 12;
131+
$key = $key >= 0 ? $key : ($key + 12);
132+
133+
return $this->_chineseZodiacList[$key];
134+
}
135+
136+
/**
137+
* 检查并拆分身份证号.
138+
*
139+
* @param string $id 身份证号
140+
*
141+
* @return array|bool
142+
*/
143+
private function _checkIdArgument($id)
144+
{
145+
$id = strtoupper($id);
146+
$length = strlen($id);
147+
$code = false;
148+
switch ($length) {
149+
case 18:
150+
$code = [
151+
'body' => substr($id, 0, 17),
152+
'addressCode' => substr($id, 0, 6),
153+
'birthdayCode' => substr($id, 6, 8),
154+
'order' => substr($id, 14, 3),
155+
'checkBit' => substr($id, -1),
156+
'type' => 18,
157+
];
158+
break;
159+
case 15:
160+
$code = [
161+
'body' => $id,
162+
'addressCode' => substr($id, 0, 6),
163+
'birthdayCode' => '19'.substr($id, 6, 6),
164+
'order' => substr($id, 12, 3),
165+
'checkBit' => '',
166+
'type' => 15,
167+
];
168+
break;
169+
}
170+
171+
return $code;
172+
}
173+
174+
/**
175+
* 检查地址码
176+
*
177+
* @param string $addressCode 地址码
178+
*
179+
* @return bool
180+
*/
181+
private function _checkAddressCode($addressCode)
182+
{
183+
$addressInfo = $this->_getAddressInfo($addressCode);
184+
185+
return $addressInfo ? true : false;
186+
}
187+
188+
/**
189+
* 检查出生日期码
190+
*
191+
* @param string $birthdayCode 出生日期码
192+
*
193+
* @return bool
194+
*/
195+
private function _checkBirthdayCode($birthdayCode)
196+
{
197+
$year = intval(substr($birthdayCode, 0, 4));
198+
$month = intval(substr($birthdayCode, 4, 2));
199+
$day = intval(substr($birthdayCode, -2));
200+
201+
if ($year < 1800) {
202+
return false;
203+
}
204+
if ($month > 12 || $month === 0 || $day > 31 || $day === 0) {
205+
return false;
206+
}
207+
208+
return true;
209+
}
210+
211+
/**
212+
* 检查顺序码
213+
*
214+
* @param string $orderCode 顺序码
215+
*
216+
* @return bool
217+
*/
218+
private function _checkOrderCode($orderCode)
219+
{
220+
if (strlen($orderCode) == 3) {
221+
return true;
222+
} else {
223+
return false;
224+
}
225+
}
226+
227+
/**
228+
* 生成随机数.
229+
*
230+
* @param int $max 最大值
231+
* @param int $min 最小值
232+
*
233+
* @return int
234+
*/
235+
private function _generatorRandInt($max, $min = 1)
236+
{
237+
return rand($min, $max);
238+
}
239+
240+
/**
241+
* 生成校验码
242+
* 详细计算方法 @lint https://zh.wikipedia.org/wiki/中华人民共和国公民身份号码
243+
*
244+
* @param string $body 身份证号 body 部分
245+
*
246+
* @return int|string
247+
*/
248+
private function _generatorCheckBit($body)
249+
{
250+
// 位置加权
251+
$posWeight = $this->_getPosWeight();
252+
253+
// 累身份证号 body 部分与位置加权的积
254+
$bodySum = 0;
255+
$bodyArray = str_split($body);
256+
$count = count($bodyArray);
257+
for ($j = 0; $j < $count; $j++) {
258+
$bodySum += (intval($bodyArray[$j], 10) * $posWeight[18 - $j]);
259+
}
260+
261+
// 生成校验码
262+
$checkBit = 12 - ($bodySum % 11);
263+
if ($checkBit == 10) {
264+
$checkBit = 'X';
265+
} elseif ($checkBit > 10) {
266+
$checkBit = $checkBit % 11;
267+
}
268+
269+
return $checkBit;
270+
}
271+
272+
/**
273+
* 生成地址码
274+
*
275+
* @return string
276+
*/
277+
private function _generatorAddressCode()
278+
{
279+
$addressCode = '110100';
280+
for ($i = 0; $i < 100; $i++) {
281+
$province = $this->_getStrPad($this->_generatorRandInt(66), 2, '0');
282+
$city = $this->_getStrPad($this->_generatorRandInt(20), 2, '0');
283+
$district = $this->_getStrPad($this->_generatorRandInt(20), 2, '0');
284+
$fakeAddressCode = $province.$city.$district;
285+
if (isset($this->_addressCodeList[$fakeAddressCode])) {
286+
$addressCode = $fakeAddressCode;
287+
break;
288+
}
289+
}
290+
291+
return $addressCode;
292+
}
293+
294+
/**
295+
* 生成出生日期码
296+
*
297+
* @return string
298+
*/
299+
private function _generatorBirthdayCode()
300+
{
301+
$year = $this->_getStrPad($this->_generatorRandInt(99, 50), 2, '0');
302+
$month = $this->_getStrPad($this->_generatorRandInt(12, 1), 2, '0');
303+
$day = $this->_getStrPad($this->_generatorRandInt(28, 1), 2, '0');
304+
$year = '19'.$year;
305+
$birthdayCode = $year.$month.$day;
306+
307+
return $birthdayCode;
308+
}
309+
310+
}

0 commit comments

Comments
 (0)