<?php
/*
数据验证类
重载interface.类
支持时间验证，数字，字符，邮件，电话
采用匿名内部调用传入验证项目必须为数组
*/
class Easy_Validate_Default implements Easy_Validate_Interface{
	public static  $error = array();		//返回错误数组
	/*public static function validate($data, $DAL)
	{
		if (is_array($DAL))
		foreach ($DAL as $key=>$value){
			$pattern = $DAL[$key][0];	//提取正则
			$message = $DAL[$key][1];	//错误消息
			$required = $DAL[$key][2];	//提取是否必填;
			$min = (int)$DAL[$key][3];	//最小长度
			$max = (int)$DAL[$key][4];	//最大程度
			if ($reqiured || $value){
				if (preg_match($pattern, $data[$key])) {
					if ($max && iconv_strlen($data[$key]) > $max){
						if(is_array($message)){
							self::$error[$key] = $message['max'];
						}else{
							self::$error[$key] = $message;
						}
					}elseif ($min && iconv_strlen($data[$key]) < $min){
						if(is_array($message)){
							self::$error[$key] = $message['min'];
						}else{
							self::$error[$key] = $message;
						}
					}
				}else{
					self::$error[$key] = $message;
				}
			} //end if
		}
		if (count(self::$error)>0){
			return false;
		}else{
			return true;
		}
	}*/
	public static function validate($data, $rule){
		foreach ($rule as $key=>$val){
			$pattern = $val[0];	//判断规则
			$message = $val[1];
			$require = $val[2];
			$max = $val[4];
			$min = $val[3];
			if ($val || $require){
				if (in_array(strtolower($pattern), array('int', 'float', 'url', 'email','boolean','ip'))){
					if (self::filter($data[$key], $pattern) === false){
						if (is_array($message)){
							self::$error[$key] = $message['default'];
						}else{
							self::$error[$key] = $message;
						}
					}
				}else{
					if ( !preg_match($pattern, $data[$key])){
						if (is_array($message)){
							self::$error[$key] = $message['default'];
						}else{
							self::$error[$key] = $message;
						}
					}
				}
				if ($max && iconv_strlen($data[$key]) > $max){
					if(is_array($message)){
						self::$error[$key] = $message['max'];
					}else{
						self::$error[$key] = $message;
					}
				}elseif ($min && iconv_strlen($data[$key]) < $min){
					if(is_array($message)){
						self::$error[$key] = $message['min'];
					}else{
						self::$error[$key] = $message;
					}
				}
			}
		}
		if (count(self::$error)>0){
			return false;
		}else{
			return true;
		}
	}
	public static  function filter($str, $type){
		if (function_exists(filter_var)){
			switch (strtolower($type)){
				case 'int':
					return filter_var($str, FILTER_VALIDATE_INT);
				break;
				case 'ip':
					return filter_var($str, FILTER_VALIDATE_IP);
				break;
				case 'email':
					return filter_var($str, FILTER_VALIDATE_EMAIL);
				break;
				case 'boolean':
					return filter_var($str, FILTER_VALIDATE_BOOLEAN);
				break;
				case 'url':
					return filter_var($str, FILTER_VALIDATE_URL);
				break;
				case 'float':
					return filter_var($str, FILTER_VALIDATE_FLOAT);
				break;
			}
			return filter_var($str, FILTER_VALIDATE_.strtoupper($type));
		}else{
			switch (strtolower($type)){
				case 'int':
					return is_int($str);
				break;
				case 'ip':
					if ( ip2long($str)===false || ip2long($str) === 0 ){
						return false;
					}else{
						return true;
					}
				break;
				case 'email':
					if ( preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i', $str)){
						return true;
					}else{
						return false;
					}
				break;
				case 'boolean':
					return is_bool($str);
				break;
				case 'url':
					if (preg_match('/http:\/\/(.+?)/')){
						return true;
					}else{
						return false;
					}
				break;
				case 'float':
					return is_float($str);
				break;
			}
		}
	}
	function __call($method, $paramas){
		if( FALSE === method_exists(new self(), $method)){
			exit($method.':call to undefined method Class Easy_Validate_Default');
		}else{
			self::$method($paramas);
		}
	}
}