<?php

namespace app\common;

class Fun
{
    /**
     * 通过身份证号返回男1 女2
     */
    public static function getSexByIdCard($idcard)
    {
        $sexint = (int)substr($idcard, 16, 1);
        return $sexint % 2 === 0 ? 2 : 1;
    }

    /**
     * 通过身份证号返回生日
     */
    public static function getBirthDayByIdCard($idcard)
    {
        $birthday = substr($idcard, 6, 4) . '-' . substr($idcard, 10, 2) . '-' . substr($idcard, 12, 2);
        return $birthday;
    }

    /**
     * 通过身份证号返回年龄
     */
    public static function getAgeByIdCard($idcard)
    {
        $birth = substr($idcard, 6, 4);
        $year  = date('Y');

        return $year - $birth;
    }

    /**
     * 身份证号是否正确
     */
    public static function isIdCard($idcard)
    {
        $vCity = [
            '11', '12', '13', '14', '15', '21', '22',
            '23', '31', '32', '33', '34', '35', '36',
            '37', '41', '42', '43', '44', '45', '46',
            '50', '51', '52', '53', '54', '61', '62',
            '63', '64', '65', '71', '81', '82', '91',
        ];
        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $idcard)) return false;
        if (!in_array(substr($idcard, 0, 2), $vCity)) return false;
        $vStr    = preg_replace('/[xX]$/i', 'a', $idcard);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }
        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17; $i >= 0; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum    += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
            }
            if ($vSum % 11 != 1) return false;
        }
        return true;
    }

    /**
     * 是否手机号
     */
    public static function isMobile($user_mobile)
    {
        $chars = "/^((\(\d{2,3}\))|(\d{3}\-))?1(3|4|5|6|7|8|9)\d{9}$/";
        if (preg_match($chars, $user_mobile)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据生日获取年龄
     */
    public static function getAgeByBirth($birthday)
    {
        if (empty($birthday)) {
            return '未知';
        }
        //格式化出生时间年月日
        $byear  = date('Y', $birthday);
        $bmonth = date('m', $birthday);
        $bday   = date('d', $birthday);

        //格式化当前时间年月日
        $tyear  = date('Y');
        $tmonth = date('m');
        $tday   = date('d');

        //开始计算年龄
        $age = $tyear - $byear;
        if ($bmonth > $tmonth || $bmonth == $tmonth && $bday > $tday) {
            $age--;
        }
        return $age;
    }

    /**
     * 获取加密姓名
     */
    public static function getEncodeName($name)
    {
        $len = mb_strlen($name,'utf-8');
        $res = mb_substr($name,0,1);
        for ($i = 1; $i < $len; $i++) {
            $res .= '*';
        }

        return $res;
    }
}