Location: PHPKode > projects > Mocovie web framework > mocovi/library/Validator.php
<?php
/**
 *  Copyright (C) 2010  Kai Dorschner
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2010, Kai Dorschner
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @package mocovi
 * @subpackage helpers
 */

class FormException extends Exception {}

class WrongTypeException extends FormException {}
class RequiredException extends FormException {}
class EmptyException extends FormException {}
class UnequalException extends FormException {}
class WrongFormatException extends FormException {}
class FileTooBigException extends FormException {}
class StringTooLongException extends FormException {}

/*
Pre-implemented and usable SPL Exceptions:

BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
LogicException
OutOfBoundsException
OutOfRangeException
OverflowException
RangeException
RuntimeException
UnderflowException
UnexpectedValueException
*/

/**
 * Validates user input.
 */
class Validator
{
	protected static $templates = array
	( 'email'		=> '/^[a-zA-Z0-9._%+-]+\@[a-zA-Z0-9.-]{2,}\.(?:[a-zA-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/'
	, 'name'		=> '/^[\w._-]{2,49}$/'
	);

	public static function getRange($i, $min, $max, $base = 1)
	{
		if($i < $min || $i > $max)
			throw new OutOfRangeException('Please provide a number that equals or is larger than '.$min.' and equals or is smaller than '.$max);
		if(($i % $base) === 0)
			return $i;
		throw new WrongTypeException('Not divisible by '.$base);
	}

	public static function getRegex($template)
	{
		if(isset(self::$templates[$template]))
			return self::$templates[$template];
	}

	public static function validateByRegex($i, $regex)
	{
		if(strlen($i) == 0)
			return $i; //throw new EmptyException();
		if(preg_match($regex, $i))
			return $i;
		throw new WrongFormatException();
	}

	public static function minLength($i, $length)
	{
		if(strlen($i) < $length)
			throw new LengthException();
		return $i;
	}

	public static function maxLength($i, $length)
	{
		if(strlen($i) > $length)
			throw new LengthException();
		return $i;
	}

	public static function required($i)
	{
		if(strlen($i) > 0)
			return $i;
		throw new RequiredException();
	}

	public function equals($x, $y)
	{
		if($x !== $y)
			throw new UnequalException();
	}

}
Return current item: Mocovie web framework