<?php

class Tool {
	
    public static function datecheck($input,$format="")
    {
        $separator_type= array(
            "/",
            "-",
            "."
        );
        foreach ($separator_type as $separator) {
            $find= stripos($input,$separator);
            if($find<>false){
                $separator_used= $separator;
            }
        }
        $input_array= explode($separator_used,$input);
        if ($format=="mdy") {
            return checkdate($input_array[0],$input_array[1],$input_array[2]);
        } elseif ($format=="ymd") {
            return checkdate($input_array[1],$input_array[2],$input_array[0]);
        } else {
            return checkdate($input_array[1],$input_array[0],$input_array[2]);
        }
        $input_array=array();
    }	
    
	public static function validarFechadmy($fecha){
		$sep = "[\/\-\.]";
		$reg = "#^(((0? [1-9] |1\d|2 [0-8] ){$sep}(0? [1-9] |1 [012] )|(29|30){$sep}(0? [13456789] |1 [012] )|31{$sep}(0? [13578] |1 [02] )){$sep}(19| [2-9] \d)\d{2}|29{$sep}0?2{$sep}((19| [2-9] \d)(0 [48] | [2468]  [048] | [13579]  [26] )|(( [2468]  [048] | [3579]  [26] )00)))$#";
		return preg_match($reg, $fecha);
	}
	public static function validarFechaymd($fecha){
		$sep = "[\/\-\.]";
		$reg = "/^(19|20)[0-9]{2}{$sep}(0[1-9]|1[012]){$sep}(0[1-9]|[12][0-9]|3[01])$/";
		return preg_match($reg, $fecha);
	}    
	

	public static function getConstante($nombre){
	    $constantes = Zend_Registry::get('constantes')->toArray();
	    return $constantes[$nombre];
	}
	
	public static function getTodasConstantes(){
	    $constantes = Zend_Registry::get('constantes')->toArray();
	    return $constantes;
	}
    
	public static function getUrlActual(){
		$url="http://".$_SERVER['HTTP_HOST'].":".$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		return $url;
	}
	
	public static function utf8_string_array_encode(&$array){
		$func = function(&$value,&$key){
			if(is_string($value)){
				$value = utf8_encode($value);
			}
			if(is_string($key)){
				$key = utf8_encode($key);
			}
			if(is_array($value)){
				Tool::utf8_string_array_encode($value);
			}
		};
		array_walk($array,$func);
		return $array;
	}

	public static function utf8_string_array_decode(&$array){
		$func = function(&$value,&$key){
			if(is_string($value)){
				$value = utf8_decode($value);
			}
			if(is_string($key)){
				$key = utf8_decode($key);
			}
			if(is_array($value)){
				Tool::utf8_string_array_decode($value);
			}
		};
		array_walk($array,$func);
		return $array;
	}
	
	public static function fechaSeparar($fecha){
		$fecha_arr = explode('/', $fecha);
		$resultado = array();
		$resultado['dia'] = $fecha_arr[0];
		$resultado['mes'] = $fecha_arr[1];
		$resultado['aņo'] = $fecha_arr[2];
		return $resultado;
	}
	public static function fecha_NumeroMesACadena($mes){
		setlocale(LC_TIME, 'spanish');
		$nombre=strftime("%B",mktime(0, 0, 0, $mes, 1, 2000));
		return $nombre;
	}
	
}