diff --git a/ArrayHelper.php b/ArrayHelper.php index 69e5e8a..e680d43 100644 --- a/ArrayHelper.php +++ b/ArrayHelper.php @@ -4,6 +4,7 @@ * createTime : 2018/5/4 18:37 * description: */ +namespace yiier\helpers; use yii\helpers\ArrayHelper as BaseArrayHelper; diff --git a/DateHelper.php b/DateHelper.php index d77e7dc..55aa43a 100644 --- a/DateHelper.php +++ b/DateHelper.php @@ -5,9 +5,7 @@ * description: */ - -namespace common\helpers; - +namespace yiier\helpers; class DateHelper { diff --git a/MailHelper.php b/MailHelper.php index 6d4f14f..cd29197 100644 --- a/MailHelper.php +++ b/MailHelper.php @@ -1,6 +1,6 @@ * createTime : 2015/12/29 18:37 * description: */ -namespace common\helpers; + +namespace yiier\helpers; class Security { diff --git a/Setup.php b/Setup.php index 7666066..1b0f25b 100644 --- a/Setup.php +++ b/Setup.php @@ -5,8 +5,7 @@ * description: */ -namespace common\helpers; - +namespace yiier\helpers; class Setup { diff --git a/ArrayValidator.php b/validators/ArrayValidator.php similarity index 92% rename from ArrayValidator.php rename to validators/ArrayValidator.php index 2eeb248..c7f1f09 100644 --- a/ArrayValidator.php +++ b/validators/ArrayValidator.php @@ -5,7 +5,7 @@ * description: */ -namespace common\helpers; +namespace yiier\helpers\validators; use yii\validators\Validator; diff --git a/validators/IdCardValidator.php b/validators/IdCardValidator.php new file mode 100644 index 0000000..d141e61 --- /dev/null +++ b/validators/IdCardValidator.php @@ -0,0 +1,82 @@ + + * createTime : 2018/05/08 16:21 + * description: + */ + +namespace yiier\helpers\validators; + +use yii\validators\Validator; + +class IdCardValidator extends Validator +{ + public function validateAttribute($model, $attribute) + { + if (!$this->validationFilterIdCard($model->$attribute)) { + $this->addError($model, $attribute, $this->message ?: '请输入正确的身份证号码'); + } + } + + private function validationFilterIdCard($idCard) + { + if (strlen($idCard) == 18) { + return $this->idCardChecksum18($idCard); + } elseif ((strlen($idCard) == 15)) { + $idCard = $this->idCard15to18($idCard); + return $this->idCardChecksum18($idCard); + } else { + return false; + } + } + + // 计算身份证校验码,根据国家标准GB 11643-1999 + private function idCardVerifyNumber($idCardBase) + { + if (strlen($idCardBase) != 17) { + return false; + } + //加权因子 + $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; + //校验码对应值 + $verifyNumberList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; + $checksum = 0; + for ($i = 0; $i < strlen($idCardBase); $i++) { + $checksum += substr($idCardBase, $i, 1) * $factor[$i]; + } + $mod = $checksum % 11; + $verifyNumber = $verifyNumberList[$mod]; + return $verifyNumber; + } + + // 将15位身份证升级到18位 + private function idCard15to18($idCard) + { + if (strlen($idCard) != 15) { + return false; + } else { + // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 + if (array_search(substr($idCard, 12, 3), ['996', '997', '998', '999']) !== false) { + $idCard = substr($idCard, 0, 6) . '18' . substr($idCard, 6, 9); + } else { + $idCard = substr($idCard, 0, 6) . '19' . substr($idCard, 6, 9); + } + } + $idCard = $idCard . $this->idCardVerifyNumber($idCard); + return $idCard; + } + + // 18位身份证校验码有效性检查 + private function idCardChecksum18($idCard) + { + if (strlen($idCard) != 18) { + return false; + } + $idCardBase = substr($idCard, 0, 17); + if ($this->idCardVerifyNumber($idCardBase) != strtoupper(substr($idCard, 17, 1))) { + return false; + } else { + return true; + } + } +} \ No newline at end of file