<?php
/**
 * 身份证相关操作函数
 * Created by 乐杨俊
 */

namespace leyangjun\Lib;

class IdCard
{
    public static function validate($id_number)
    {
        $id_number = strtoupper($id_number);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if(!preg_match($regx, $id_number)) {
            return false;
        }
        //检查15位
        if(15==strlen($id_number)) {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
            preg_match($regx, $id_number, $arr_split);
            //检查生日日期是否正确
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            return (bool) strtotime($dtm_birth);
        } else {
            //检查18位
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            preg_match($regx, $id_number, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            //检查生日日期是否正确
            if(!strtotime($dtm_birth)) {
                return false;
            } else {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ( $i = 0; $i < 17; $i++ ) {
                    $b = (int) $id_number{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n  = $sign % 11;
                return $arr_ch[$n] == substr($id_number,17, 1);
            }
        }
    }

    // 获取生日
    public static function getBirthDay($id_number)
    {
        $birth_year = substr($id_number, 6,4);
        $birth_month = substr($id_number, 10,2);
        $birth_day = substr($id_number, 12,2);

        return "{$birth_year}-{$birth_month}-{$birth_day}";
    }

    // 判断是否已经成年
    public static function isAdult($id_number)
    {
        $birth_year = (int) substr($id_number, 6,4);
        $birth_month = (int) substr($id_number, 10,2);
        $birth_day = (int) substr($id_number, 12,2);
        $target_time = strtotime(($birth_year + 18).'-'.$birth_month.'-'.$birth_day);

        // 过完生日后的零时为成年人
        // 02-29出生的孩子,03-01是成年,因此strtotime会直接变为03-01,其他的日期都需要过一天
        $target_time += ($birth_month == 02 && $birth_day == 29) ? 0 : 86400;
        return $this->getCurrentTime() > $target_time;
    }


    //根据身份证号获取性别
    function getSex($cardNum) {
        return ((int)substr($cardNum, 16, 1)) % 2 === 0 ? '女' : '男';
    }

    
    public function getCurrentTime()
    {
        if (php_sapi_name() == 'cli') {
            return time();
        }
        if (!$currentTime) {
            if (isset($_SERVER['REQUEST_TIME'])) {
                $currentTime = $_SERVER['REQUEST_TIME'];
            } else {
                $currentTime = time();
            }
        }
        return $currentTime;
    }


}