<?php

namespace App\Http\Services;

use DateTime;

class CheckParamService
{
    
    public function __construct()
    {
    }
    
    public function LenMToN($str, $m, $n)
    {
        if (mb_strlen($str) >= $m && mb_strlen($str) <= $n) {
            return true;
        } else {
            return false;
        }
    }
    
    public function isascii($str)
    {
        if (mb_detect_encoding($str) == "ASCII") {
            return true;
        } else {
            return false;
        }
    }
    
    public function isengnum($str)
    {
        return (preg_match("/^[a-zA-Z0-9]+$/", $str) == 1);
    }
    
    public function isnum($str)
    {
        return (preg_match("/^[0-9]+$/", $str) == 1);
    }
    
    public function validateDate($date, $format = 'Y-m-d H:i:s')
    {
        $d = DateTime::createFromFormat($format, $date);
        
        return $d && $d->format($format) == $date;
    }
    
    public function RemoveChars($str, $chars)
    {
        $chars = str_split($chars, 1);
        for ($i = 0; $i < count($chars); $i++) $str = str_replace($chars[ $i ], "", $str);
        
        return $str;
    }
    
    public function isIdentity($id)
    {
        $id = strtoupper(trim($id));
        $alphabetTable = [
            'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16,
            'H' => 17, 'I' => 34, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22,
            'O' => 35, 'P' => 23, 'Q' => 24, 'R' => 25, 'S' => 26, 'T' => 27, 'U' => 28,
            'V' => 29, 'X' => 30, 'Y' => 31, 'Z' => 33
        ];
        if (!preg_match("/^[A-Z]{1}[12ABCD]{1}[0-9]{8}$/", $id)) {
            return false;
        }
        $idArray = str_split($id);
        $alphabet = $alphabetTable[ $idArray[0] ];
        $point = substr($alphabet, 0, 1) * 1 + substr($alphabet, 1, 1) * 9;
        for ($i = 1; $i <= 8; $i++) {
            $point += $idArray[ $i ] * (9 - $i);
        }
        $point = $point + $idArray[9];
        
        return $point % 10 == 0 ? true : false;
    }
    
}
