From 49b45aa267e88b024019db7c70ca9cee64e2feb0 Mon Sep 17 00:00:00 2001 From: Leonis Date: Tue, 3 Sep 2019 06:25:39 +0000 Subject: [PATCH] Apply fixes from StyleCI --- data/codes.php | 5 ++-- src/BadIDCardException.php | 3 +- src/IDCard.php | 58 +++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/data/codes.php b/data/codes.php index 109664a..2ba5d4c 100644 --- a/data/codes.php +++ b/data/codes.php @@ -1,5 +1,6 @@ '北京市', 110101 => '东城区', 110102 => '西城区', @@ -6693,4 +6694,4 @@ 710000 => '台湾省', 810000 => '香港特别行政区', 820000 => '澳门特别行政区', -); \ No newline at end of file +]; diff --git a/src/BadIDCardException.php b/src/BadIDCardException.php index f1b5f58..cf28429 100644 --- a/src/BadIDCardException.php +++ b/src/BadIDCardException.php @@ -4,5 +4,4 @@ class BadIDCardException extends \Exception { - -} \ No newline at end of file +} diff --git a/src/IDCard.php b/src/IDCard.php index e0c5181..aa5ddfb 100644 --- a/src/IDCard.php +++ b/src/IDCard.php @@ -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'; } /** @@ -36,6 +36,7 @@ public function check(): bool if ($this->isTrue === null) { $this->isTrue = $this->checkAreaCode() && $this->checkBirthday() && $this->checkCode(); } + return $this->isTrue; } @@ -88,8 +89,9 @@ public function checkCode(): bool * * @param string $separator * - * @return string * @throws BadIDCardException + * + * @return string */ public function address(string $separator = ''): string { @@ -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']; } @@ -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']; } @@ -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)]; } @@ -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 { @@ -158,50 +167,57 @@ 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 { @@ -209,9 +225,9 @@ public function age(): int $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)) { @@ -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 { @@ -259,8 +278,9 @@ public function constellation(): string /** * 获取属相. * - * @return string * @throws BadIDCardException + * + * @return string */ public function zodiac(): string {