Skip to content

Commit 56aa99f

Browse files
committed
基本信息增加星座和生肖
1 parent 3af927e commit 56aa99f

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ $idValidator = new IdValidator();
3131
$idValidator->isValid('440308199901101512'); // 18 位
3232
$idValidator->isValid('610104620927690'); // 15 位
3333
```
34+
返回信息格式:
35+
36+
```php
37+
[
38+
'addressCode' => '440308',
39+
'address' => '广东省深圳市盐田区',
40+
'birthdayCode' => '1999-01-10',
41+
'constellation' => '水瓶座',
42+
'chineseZodiac' => '卯兔',
43+
'sex' => 1,
44+
'length' => 18,
45+
'checkBit' => '2',
46+
]
47+
```
3448

3549
### 获取身份证号信息
3650

src/IdValidator.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function getInfo($id)
8282
$info['addressCode'] = $code['addressCode'];
8383
$info['address'] = is_array($addressInfo) ? implode($addressInfo) : '';
8484
$info['birthdayCode'] = date('Y-m-d', strtotime($code['birthdayCode']));
85+
$info['constellation'] = $this->_getConstellation($code['birthdayCode']);
86+
$info['chineseZodiac'] = $this->_getChineseZodiac($code['birthdayCode']);
8587
$info['sex'] = ($code['order'] % 2 === 0 ? 0 : 1);
8688
$info['length'] = $code['type'];
8789
$info['checkBit'] = $code['checkBit'];
@@ -188,6 +190,51 @@ private function _getAddressInfo($addressCode)
188190
}
189191
}
190192

193+
public function _getConstellation($birthdayCode)
194+
{
195+
196+
$constellationList = $this->_getConstellationList();
197+
198+
$time = strtotime($birthdayCode);
199+
$year = substr($birthdayCode, 0,4);
200+
$month = substr($birthdayCode, 4, 2);
201+
$day = substr($birthdayCode, 6,2);
202+
203+
204+
// 1月份与12月份特殊处理
205+
if( ( $month == 1 && $day < 20 ) || ( $month == 12 && $day > 21 ) ){
206+
return $constellationList[12]['name'];
207+
}else if( $month == 1 ){
208+
return $constellationList[1]['name'];
209+
}else if( $month == 12 ){
210+
return $constellationList[12]['name'];
211+
}
212+
213+
$startDate = $year.'-'.$constellationList[$month]['start_date'];
214+
$endDate = $year.'-'.$constellationList[$month]['end_date'];
215+
if( strtotime( $startDate ) <= $time && strtotime( $endDate ) >= $time ){
216+
return $constellationList[$month]['name'];
217+
}
218+
$startDate = $year.'-'.$constellationList[$month-1]['start_date'];
219+
$endDate = $year.'-'.$constellationList[$month-1]['end_date'];
220+
if( strtotime( $startDate ) <= $time && strtotime( $endDate ) >= $time ){
221+
return $constellationList[$month-1]['name'];
222+
}
223+
224+
return '';
225+
}
226+
227+
public function _getChineseZodiac($birthdayCode)
228+
{
229+
$chineseZodiacList = $this->_getChineseZodiacList();
230+
$start = 1900; // 子鼠
231+
$end = substr($birthdayCode, 0, 4);
232+
$key = ($end - $start) % 12;
233+
$key = $key >= 0 ? $key : ($key + 12);
234+
return $chineseZodiacList[$key];
235+
}
236+
237+
191238
/**
192239
* 检查并拆分身份证号.
193240
*
@@ -361,4 +408,73 @@ private function _generatorBirthdayCode()
361408

362409
return $birthdayCode;
363410
}
411+
412+
/**
413+
* @return array
414+
*/
415+
private function _getChineseZodiacList()
416+
{
417+
return ['子鼠', '丑牛', '寅虎', '卯兔', '辰龙', '巳蛇', '午马', '未羊', '申猴', '酉鸡', '戌狗', '亥猪'];
418+
}
419+
420+
/**
421+
* @return array
422+
*/
423+
private function _getConstellationList()
424+
{
425+
return [
426+
'01' => [
427+
'name' => '水瓶座',
428+
'start_date' => '01-20',
429+
'end_date' => '02-18'
430+
],
431+
'02' => [
432+
'name' => '双鱼座',
433+
'start_date' => '02-19',
434+
'end_date' => '03-20'
435+
],
436+
'03' => [
437+
'name' => '白羊座',
438+
'start_date' => '03-21',
439+
'end_date' => '04-19'
440+
],
441+
'04' => [
442+
'name' => '金牛座',
443+
'start_date' => '04-20',
444+
'end_date' => '05-20'
445+
], '05' => [
446+
'name' => '双子座',
447+
'start_date' => '05-21',
448+
'end_date' => '06-21'
449+
], '06' => [
450+
'name' => '巨蟹座',
451+
'start_date' => '06-22',
452+
'end_date' => '07-22'
453+
], '07' => [
454+
'name' => '狮子座',
455+
'start_date' => '07-23',
456+
'end_date' => '08-22'
457+
], '08' => [
458+
'name' => '处女座',
459+
'start_date' => '08-23',
460+
'end_date' => '09-22'
461+
], '09' => [
462+
'name' => '天秤座',
463+
'start_date' => '09-23',
464+
'end_date' => '10-23'
465+
], '10' => [
466+
'name' => '天蝎座',
467+
'start_date' => '10-24',
468+
'end_date' => '11-22'
469+
], '11' => [
470+
'name' => '射手座',
471+
'start_date' => '11-23',
472+
'end_date' => '12-21'
473+
], '12' => [
474+
'name' => '水瓶座',
475+
'start_date' => '12-22',
476+
'end_date' => '01-19'
477+
]
478+
];
479+
}
364480
}

tests/IdValidatorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function testGetInfo()
3333
'addressCode' => '440308',
3434
'address' => '广东省深圳市盐田区',
3535
'birthdayCode' => '1999-01-10',
36+
'constellation' => '水瓶座',
37+
'chineseZodiac' => '卯兔',
3638
'sex' => 1,
3739
'length' => 18,
3840
'checkBit' => '2', ],
@@ -44,6 +46,8 @@ public function testGetInfo()
4446
'addressCode' => '610104',
4547
'address' => '陕西省西安市莲湖区',
4648
'birthdayCode' => '1962-09-27',
49+
'constellation' => '天秤座',
50+
'chineseZodiac' => '寅虎',
4751
'sex' => 0,
4852
'length' => 15,
4953
'checkBit' => '', ],

0 commit comments

Comments
 (0)