Skip to content

Commit

Permalink
Merge pull request #7 from yangliulnn/analysis-zRpEmn
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
yl authored Sep 3, 2019
2 parents 297851d + 49b45aa commit 466d42f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
5 changes: 3 additions & 2 deletions data/codes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
return array (

return [
110000 => '北京市',
110101 => '东城区',
110102 => '西城区',
Expand Down Expand Up @@ -6693,4 +6694,4 @@
710000 => '台湾省',
810000 => '香港特别行政区',
820000 => '澳门特别行政区',
);
];
3 changes: 1 addition & 2 deletions src/BadIDCardException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class BadIDCardException extends \Exception
{

}
}
58 changes: 39 additions & 19 deletions src/IDCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class IDCard
public function __construct(string $id)
{
$this->id = strtoupper($id);
$this->areaCodes = (array)require __DIR__.'/../data/codes.php';
$this->areaCodes = (array) require __DIR__.'/../data/codes.php';
}

/**
Expand All @@ -36,6 +36,7 @@ public function check(): bool
if ($this->isTrue === null) {
$this->isTrue = $this->checkAreaCode() && $this->checkBirthday() && $this->checkCode();
}

return $this->isTrue;
}

Expand Down Expand Up @@ -88,8 +89,9 @@ public function checkCode(): bool
*
* @param string $separator
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function address(string $separator = ''): string
{
Expand All @@ -99,14 +101,16 @@ public function address(string $separator = ''): string
/**
* 获取省
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function province(): string
{
if ($this->check() === false) {
throw new BadIDCardException();
}

return $this->areaCodes[substr($this->id, 0, 2).'0000'];
}

Expand All @@ -115,14 +119,16 @@ public function province(): string
*
* @param string $separator
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function city(string $separator = ''): string
{
if ($this->check() === false) {
throw new BadIDCardException();
}

return $separator.$this->areaCodes[substr($this->id, 0, 4).'00'];
}

Expand All @@ -131,14 +137,16 @@ public function city(string $separator = ''): string
*
* @param string $separator
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function zone(string $separator = ''): string
{
if ($this->check() === false) {
throw new BadIDCardException();
}

return $separator.$this->areaCodes[substr($this->id, 0, 6)];
}

Expand All @@ -147,8 +155,9 @@ public function zone(string $separator = ''): string
*
* @param string $format
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function birthday(string $format = 'Y-m-d'): string
{
Expand All @@ -158,60 +167,67 @@ public function birthday(string $format = 'Y-m-d'): string
/**
* 获取年.
*
* @return int
* @throws BadIDCardException
*
* @return int
*/
public function year(): int
{
if ($this->check() === false) {
throw new BadIDCardException();
}
return (int)substr($this->id, 6, 4);

return (int) substr($this->id, 6, 4);
}

/**
* 获取月.
*
* @return int
* @throws BadIDCardException
*
* @return int
*/
public function month(): int
{
if ($this->check() === false) {
throw new BadIDCardException();
}
return (int)substr($this->id, 10, 2);

return (int) substr($this->id, 10, 2);
}

/**
* 获取日.
*
* @return int
* @throws BadIDCardException
*
* @return int
*/
public function day(): int
{
if ($this->check() === false) {
throw new BadIDCardException();
}
return (int)substr($this->id, 12, 2);

return (int) substr($this->id, 12, 2);
}

/**
* 获取年龄.
*
* @return int
* @throws BadIDCardException
*
* @return int
*/
public function age(): int
{
$year = $this->year();
$month = $this->month();
$day = $this->day();

$nowYear = (int)date('Y');
$nowMonth = (int)date('n');
$nowDay = (int)date('j');
$nowYear = (int) date('Y');
$nowMonth = (int) date('n');
$nowDay = (int) date('j');

$age = $nowYear > $year ? $nowYear - $year - 1 : 0;
if ($nowMonth > $month || ($nowMonth === $month && $nowDay >= $day)) {
Expand All @@ -224,22 +240,25 @@ public function age(): int
/**
* 获取性别.
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function sex(): string
{
if ($this->check() === false) {
throw new BadIDCardException();
}

return substr($this->id, 16, 1) % 2 ? '' : '';
}

/**
* 获取星座.
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function constellation(): string
{
Expand All @@ -259,8 +278,9 @@ public function constellation(): string
/**
* 获取属相.
*
* @return string
* @throws BadIDCardException
*
* @return string
*/
public function zodiac(): string
{
Expand Down

0 comments on commit 466d42f

Please sign in to comment.