<?php
declare (strict_types=1);
namespace library\utils;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2021/3/16 上午9:01
// +----------------------------------------------------------------------
class IdentityCard
{
    /**
     * 校验身份证号是否合法
     * @param string $num 待校验的身份证号
     * @return bool
     */
    public static function isValid(string $num)
    {
        //老身份证长度15位，新身份证长度18位
        $length = strlen($num);
        if ($length == 15) { //如果是15位身份证

            //15位身份证没有字母
            if (!is_numeric($num)) {
                return false;
            }
            // 省市县（6位）
            $areaNum = substr($num, 0, 6);
            // 出生年月（6位）
            $dateNum = substr($num, 6, 6);

        } else if ($length == 18) { //如果是18位身份证

            //基本格式校验
            if (!preg_match('/^\d{17}[0-9xX]$/', $num)) {
                return false;
            }
            // 省市县（6位）
            $areaNum = substr($num, 0, 6);
            // 出生年月日（8位）
            $dateNum = substr($num, 6, 8);

        } else { //假身份证
            return false;
        }

        //验证地区
        if (!self::isAreaCodeValid($areaNum)) {
            return false;
        }

        //验证日期
        if (!self::isDateValid($dateNum)) {
            return false;
        }

        //验证最后一位
        if (!self::isVerifyCodeValid($num)) {
            return false;
        }

        return true;
    }

    /**
     * 省市自治区校验
     * @param string $area 省、直辖市代码
     * @return bool
     */
    private static function isAreaCodeValid(string $area)
    {
        $provinceCode = substr($area, 0, 2);

        // 根据GB/T2260—999，省市代码11到65
        if (11 <= $provinceCode && $provinceCode <= 65) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证出生日期合法性
     * @param string $date 日期
     * @return bool
     */
    private static function isDateValid(string $date)
    {
        if (strlen($date) == 6) { //15位身份证号没有年份，这里拼上年份
            $date = '19' . $date;
        }
        $year  = intval(substr($date, 0, 4));
        $month = intval(substr($date, 4, 2));
        $day   = intval(substr($date, 6, 2));

        //日期基本格式校验
        if (!checkdate($month, $day, $year)) {
            return false;
        }

        //日期格式正确，但是逻辑存在问题(如:年份大于当前年)
        $currYear = date('Y');
        if ($year > $currYear) {
            return false;
        }
        return true;
    }

    /**
     * 验证18位身份证最后一位
     * @param string $num 待校验的身份证号
     * @return bool
     */
    private static function isVerifyCodeValid(string $num)
    {
        if (strlen($num) == 18) {
            $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            $tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

            $checkSum = 0;
            for ($i = 0; $i < 17; $i++) {
                $checkSum += intval($num[$i]) * $factor[$i];
            }

            $mod   = $checkSum % 11;
            $token = $tokens[$mod];

            $lastChar = strtoupper($num[17]);

            if ($lastChar != $token) {
                return false;
            }
        }
        return true;
    }
    
    
    
    /**
     *  根据身份证号码获取性别
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     *  @return int $sex 性别 1男 2女 0未知
     */
    public static function get_sex($idcard) {
        if(empty($idcard)) return null; 
        $sexint = (int) substr($idcard, 16, 1);
        return $sexint % 2 === 0 ? '女' : '男';
    }
 
    /**
     *  根据身份证号码获取生日
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     *  @return $birthday
     */
    public static function get_birthday($idcard) {
        if(empty($idcard)) return null; 
        $bir = substr($idcard, 6, 8);
        $year = (int) substr($bir, 0, 4);
        $month = (int) substr($bir, 4, 2);
        $day = (int) substr($bir, 6, 2);
        return $year . "-" . $month . "-" . $day;
    }
 
    /**
     *  根据身份证号码计算年龄
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     *  @return int $age
     */
    public static function get_age($idcard){  
        if(empty($idcard)) return null; 
        #  获得出生年月日的时间戳 
        $date = strtotime(substr($idcard,6,8));
        #  获得今日的时间戳 
        $today = strtotime('today');
        #  得到两个日期相差的大体年数 
        $diff = floor(($today-$date)/86400/365);
        #  strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 
        $age = strtotime(substr($idcard,6,8).' +'.$diff.'years')>$today?($diff+1):$diff; 
        return $age; 
    } 
    
    /**
     *  根据身份证号码获取出身地址
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     *  @return string $address
     */
    public static function get_address($idcard, $type=1){
        if(empty($idcard)) return null;
        $address = include('./address.php');
        switch ($type) {
            case 1:
                    # 截取前六位数(获取基体到县区的地址)
                    $key = substr($idcard,0,6);
                    if(!empty($address[$key])) return $address[$key];
                    # 截取前两位数(没有基体到县区的地址就获取省份)
                    $key = substr($idcard,0,2); 
                    if(!empty($address[$key])) return $address[$key];
                    # 都没有
                    return '未知地址';
                break;
            case 2:
                    # 截取前两位数(只获取省份)
                    $key = substr($idcard,0,2); 
                    if(!empty($address[$key])) return $address[$key];
                break;
            default:
                    return null;
                break;
        }
    }
    /**
     *  根据身份证号，返回对应的生肖
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     */
    public static function get_zodiac($idcard){ //
        if(empty($idcard)) return null;
        $start = 1901;
        $end = (int)substr($idcard, 6, 4);
        $x = ($start - $end) % 12;
        $val = '';
        if ($x == 1 || $x == -11) $val = '鼠';
        if ($x == 0)              $val = '牛';
        if ($x == 11 || $x == -1) $val = '虎';
        if ($x == 10 || $x == -2) $val = '兔';
        if ($x == 9 || $x == -3)  $val = '龙';
        if ($x == 8 || $x == -4)  $val = '蛇';
        if ($x == 7 || $x == -5)  $val = '马';
        if ($x == 6 || $x == -6)  $val = '羊';
        if ($x == 5 || $x == -7)  $val = '猴';
        if ($x == 4 || $x == -8)  $val = '鸡';
        if ($x == 3 || $x == -9)  $val = '狗';
        if ($x == 2 || $x == -10) $val = '猪';
        return $val;
    }
 
    /**
     *  根据身份证号，返回对应的星座
     *  author:xiaochuan
     *  @param string $idcard    身份证号码
     */
    public static function get_starsign($idcard){
        if(empty($idcard)) return null;
        $b = substr($idcard, 10, 4);
        $m = (int)substr($b, 0, 2);
        $d = (int)substr($b, 2);
        $val = '';
        if(($m == 1 && $d <= 21) || ($m == 2 && $d <= 19)){
            $val = "水瓶座";
        }else if (($m == 2 && $d > 20) || ($m == 3 && $d <= 20)){
            $val = "双鱼座";
        }else if (($m == 3 && $d > 20) || ($m == 4 && $d <= 20)){
            $val = "白羊座";
        }else if (($m == 4 && $d > 20) || ($m == 5 && $d <= 21)){
            $val = "金牛座";
        }else if (($m == 5 && $d > 21) || ($m == 6 && $d <= 21)){
            $val = "双子座";
        }else if (($m == 6 && $d > 21) || ($m == 7 && $d <= 22)){
            $val = "巨蟹座";
        }else if (($m == 7 && $d > 22) || ($m == 8 && $d <= 23)){
            $val = "狮子座";
        }else if (($m == 8 && $d > 23) || ($m == 9 && $d <= 23)){
            $val = "处女座";
        }else if (($m == 9 && $d > 23) || ($m == 10 && $d <= 23)){
            $val = "天秤座";
        }else if (($m == 10 && $d > 23) || ($m == 11 && $d <= 22)){
            $val = "天蝎座";
        }else if (($m == 11 && $d > 22) || ($m == 12 && $d <= 21)){
            $val = "射手座";
        }else if (($m == 12 && $d > 21) || ($m == 1 && $d <= 20)){
            $val = "魔羯座";
        }
        return $val;
    }
    
    
}