<?php

abstract class Validate {

	private static $_lastErrors = array();

	private static $_errorMessages = array(
		'LEN' => 'L&auml;nge stimmt nicht (%1$s Zeichen)',
		'SET' => 'Darf nicht leer sein',
		'ALN' => 'Nur alphanummerische Zeichen',
		'EXP' => 'Nur alphanummerische, Satz- und Leerzeichen',
		'NUM' => 'Nur Ziffern',
		'DEC' => 'Muss eine Kommazahl sein',
		'DAT' => 'Muss ein g&uuml;ltiges Datum sein',
		'INT' => 'Muss eine Ganzzahl sein',
		'INTRANGE' => 'Nicht im Wertebereich',
		'INTRANGEERROR' => 'Gegebener Wertebereich ist ungültig',
		'CUR' => 'Muss eine W&auml;hrungsangabe sein'
	);

	public function validateArray(Array $pattern, Array $data, $returnType = 'ALL') {
		$checked = array();
		$allPassed = true;
		$errors = array();

		foreach ($pattern as $key => $expression) {
			self::$_lastErrors = array();

			$expArr = explode('|', $expression);
			$checked[$key] = true;
			$errors[$key] = '';

			foreach ($expArr as $checkExp) {
				$exprTmp = explode(':', $checkExp);

				if (preg_match("@^\?@", $exprTmp[0]) && strlen($data[$key]) == 0) {
					$currentCheck = true;
				} else {
					$valTest = strtolower(str_replace('?', '', $exprTmp[0]));
					$currentCheck = self::$valTest($data[$key], isset($exprTmp[1]) ? $exprTmp[1] : false);
					if (!$currentCheck) {
						$errors[$key] = implode(' | ', self::$_lastErrors);
					}
				}

				$allPassed = $allPassed & $currentCheck;
				$checked[$key] = $checked[$key] & $currentCheck;
			}
		}

		if ($returnType == 'BOOL') {
			return $allPassed;
		} else if ($returnType == 'CHECKED') {
			return $checked;
		} else if ($returnType == 'ERRORS') {
			return $errors;
		} else if ($returnType == 'ALL') {
			return array('passed' => $allPassed, 'checked' => $checked, 'errors' => $errors);
		}
		//		Debugger::pr($checked);
	}

	public function int($val, $args = false) {
		$check = true;

		if (preg_replace("@([^0-9])@", '', $val) != $val) {
			self::$_lastErrors[] = self::$_errorMessages['INT'];

			$check &= false;
		}

		if ($args) {
			$check &= self::len($val, $args);
		}

		return $check;
	}

	public function intrange($val, $args = false) {
		$check = true;

		if (preg_replace("@([^0-9])@", '', $val) != $val) {
			self::$_lastErrors[] = self::$_errorMessages['INT'];

			$check &= false;
		}

		if ($args) {
			//valid args [mininteger],[maxinteger]
			$argsList = explode(',',$args);
			if (count($argsList)=2) {
				if (intval($var)>=intval($argsList[0]) AND intval($var)<=intval($argsList[1])) {
					$check &= true;
				} else {
					self::$_lastErrors[] = self::$_errorMessages['INTRANGE'];
					$check &= false;
				}
			} else if (count($argsList)=1) {
				if (intval($var)>=intval($argsList[0])) {
					$check &= true;
				} else {
					self::$_lastErrors[] = self::$_errorMessages['INTRANGE'];
					$check &= false;
				}
			} else {
				self::$_lastErrors[] = self::$_errorMessages['INTRANGEERROR'];
				$check &= false;
			}
		}

		return $check;
	}

	public function cur($val, $args = false) {
		$check = true;

		if (empty($val)) {
			return $check;
		}

		if ($args) {
			$float_arg = explode('.', $args);
			$pre = '{1,'.$float_arg[0].'}';
			$pos = '{1,'.$float_arg[1].'}';
		} else {
			$pre = '+';
			$pos = '+';
		}

		$pattern = "@^[+-]?(([0-9]".$pre.")|([0-9]".$pre."[\.,][0-9]".$pos."|[0-9]".$pre."[\.,][0-9]".$pos.")|(([0-9]".$pre."|([0-9]".$pre."[\.,][0-9]".$pos."|[0-9]".$pre."[\.,][0-9]".$pos."))[eE][+-]?[0-9]".$pre."))$@";

		if (!preg_match($pattern, $val)) {
			self::$_lastErrors[] = self::$_errorMessages['CUR'];

			$check &= false;
		}

		return $check;
	}

	public function dec($val, $args = false) {
		$check = true;

		if (empty($val)) {
			return $check;
		}

		if ($args) {
			$float_arg = explode('.', $args);
			$pre = '{1,'.$float_arg[0].'}';
			$pos = '{1,'.$float_arg[1].'}';
		} else {
			$pre = '+';
			$pos = '+';
		}

		$pattern = "@^[+-]?(([0-9]".$pre.")|([0-9]".$pre."[\.,][0-9]".$pos."|[0-9]".$pre."[\.,][0-9]".$pos.")|(([0-9]".$pre."|([0-9]".$pre."[\.,][0-9]".$pos."|[0-9]".$pre."[\.,][0-9]".$pos."))[eE][+-]?[0-9]".$pre."))$@";

		if (!preg_match($pattern, $val)) {
			self::$_lastErrors[] = self::$_errorMessages['DEC'];

			$check &= false;
		}


		return $check;
	}

	public function num($val, $args = false) {
		$check = true;

		if (!is_numeric($val)) {
			self::$_lastErrors[] = self::$_errorMessages['NUM'];

			$check &= false;
		}

		if ($args) {
			$check &= self::len($val, $args);
		}

		return $check;
	}

	public function set($val, $args = false) {
		if ((!isset($val) || empty($val)) && $val != '0') {
			self::$_lastErrors[] = self::$_errorMessages['SET'];

			return false;
		}

		return true;
	}

	public function aln($val, $args = false) {
		$check = true;

		$pattern = '/[^\p{L}\p{N}]/u';

		if (preg_replace($pattern, '', $val) != $val) {
			self::$_lastErrors[] = self::$_errorMessages['ALN'];

			$check &= false;
		}

		if ($args) {
			$check &= self::len($val, $args);
		}

		return $check;
	}

	public function exp($val, $args = false) {
		$check = true;

		$pattern = '/[^\p{L}\p{N}\s\.\,\-\_\+]/u';

		if (preg_replace($pattern, '', $val) != $val) {
			self::$_lastErrors[] = self::$_errorMessages['EXP'];

			$check &= false;
		}

		if ($args) {
			$check &= self::len($val, $args);
		}

		return $check;
	}

	public function len($val, $args) {

		if (preg_match("@^=[0-9]+$@", $args)) {
			if (strlen($val) != (int)preg_replace("@[^0-9]@", '', $args)) {
				self::$_lastErrors[] = sprintf(self::$_errorMessages['LEN'], $args);

				return false;
			}
			return true;
		}

		if (preg_match("@^<[0-9]+$@", $args)) {
			return (strlen($val) < (int)preg_replace("@[^0-9]@", '', $args));
		}

		if (preg_match("@^>[0-9]+$@", $args)) {
			return (strlen($val) > (int)preg_replace("@[^0-9]@", '', $args));
		}

		if (preg_match("@^<=[0-9]+$@", $args)) {
			if (strlen($val) > (int)preg_replace("@[^0-9]@", '', $args)) {
				self::$_lastErrors[] = sprintf(self::$_errorMessages['LEN'], $args);

				return false;
			}
			return true;
		}

		if (preg_match("@^>=[0-9]+$@", $args)) {
			return (strlen($val) >= (int)preg_replace("@[^0-9]@", '', $args));
		}

		if (preg_match("@^[0-9]+-[0-9]+$@", $args)) {
			return (strlen($val) >= (int)preg_replace("@([0-9]+)-([0-9]+)@", "$1", $args)) & (strlen($val) <= (int)preg_replace("@([0-9]+)-([0-9]+)@", "$2", $args));
		}
	}

	public function dat($date, $args = false) {
		$check = true;

		if (!empty($date) && strlen($date) != 10) {
			$check = false;
		}

		if (!empty($date) && $check) {
			if (preg_match("/^(\d{2})\.(\d{2})\.(\d{4})$/", $date, $matches)) {
				$user_date = array('m' => $matches[2], 'd' => $matches[1], 'y' => $matches[3]);
			} else if (preg_match("/^(\d{4})\-(\d{2})\-(\d{2})$/", $date, $matches)) {
				$user_date = array('m' => $matches[2], 'd' => $matches[3], 'y' => $matches[1]);
			} else if (preg_match("/^(\d{2})\/(\d{2})\/(\d{4})$/", $date, $matches)) {
				$user_date = array('m' => $matches[1], 'd' => $matches[2], 'y' => $matches[3]);
			} else {
				$check &= false;
			}

			if (isset($user_date) && !checkdate($user_date['m'], $user_date['d'], $user_date['y'])) {
				$check &= false;
			}
		}

		if ($check == false) {
			self::$_lastErrors[] = self::$_errorMessages['DAT'];
		}

		return $check;
	}
}

?>