<?php

namespace App\Utils;

use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;

/**
 * 正则
 */
class PcreMatch
{
    private static string $PregIdCard = '/^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dxX]$/';

    /**
     * 匹配身份证
     * @param string $id_number
     * @return string
     */
    public static function pregIdCard(string $id_number): string
    {
        if(empty($id_number))
        {
            throw new BusinessException("身份证号为空",HttpEnumCode::HTTP_ERROR);
        }

        preg_match(self::$PregIdCard, $id_number,$match);

        if(empty($match))
        {
            throw new BusinessException("身份证号错误", HttpEnumCode::HTTP_ERROR);
        }
        return $match[0];
    }

    /**
     * 匹配身份证最后一位，修改为大写x
     * @param string $id_number
     * @return string
     */
    public static function pregIdCardX(string $id_number): string
    {
        if(empty($id_number))
        {
            throw new BusinessException("身份证号为空",HttpEnumCode::HTTP_ERROR);
        }

        return str_replace("x","X",$id_number);
    }

    /**
     * 匹配去除oss网址
     * @param string $path
     * @return string
     */
    public static function pregRemoveOssWebsite(string $path): string
    {
        if (empty($path)){
            return $path;
        }

        return str_replace(config('alibaba.oss.custom_domain_name'),"",$path);
    }
}