<?php

namespace Hura8\System\Security;


use Hura8\System\Constant;
use Hura8\Traits\ClassCacheTrait;

class DataValidator
{

    use ClassCacheTrait;


    public static function isValidLength($str, $max_length=50) : bool {
        if(!is_string($str)) return '';

        return (strlen($str) <= $max_length);
    }


    public static function isIPAddress($input, $type = '') : bool {
        //validates IPv4
        if($type == '4') {
            return filter_var($input, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4);
        }

        //validates IPv6
        if($type == '6') {
            return filter_var($input, FILTER_VALIDATE_IP,FILTER_FLAG_IPV6);
        }

        // all type
        // return filter_var($input, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4);

        //validates IPv4 and IPv6, excluding reserved and private ranges
        return filter_var($input, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
    }



    public static function isDate($input) {
        // support pattern:
        // date = d-m-Y
        // datetime = d-m-Y H:i:a
        $pattern = "/([0-9]{2})-([0-9]{2})-([0-9]{2,4})(\s)?([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})?)?/i";
        return (preg_match($pattern, $input));
    }


    /**
     * @param string $input
     * @return string|int
     */
    public static function isMobile(string $input) {
        $clean = preg_replace("/[^0-9]/", '', $input);
        return (preg_match('/[0-9]{8,14}$/', $clean));
    }


    /**
     * @param $input
     * @return bool
     */
    public static function isEmail($input): bool
    {
        return filter_var(trim($input), FILTER_VALIDATE_EMAIL);
    }


    public static function isGender($input) : bool {
        $system_list = Constant::genderList();

        return (array_key_exists($input, $system_list));
    }


}
