<?php
/**
* Helper -> Validator
* Helps validating stuff
*/
class Validator {
/**
* Checks if string is empty
* @param string
* @return boolean
*/
public function isEmpty($uri)
{
if($uri == ""){
return true;
} else {
return false;
}
}
/**
* Checks if param is numeric
*/
public function isNumeric($var)
{
if(is_numeric($var)) return TRUE;
return FALSE;
}
/**
* Checks if string is not empty
* @param string
* @return boolean
*/
public function notEmpty($uri)
{
if($uri != ""){
return true;
} else {
return false;
}
}
/**
* Checks if a date is valid
* @param date
* @return bool
*/
public function validDate($date)
{
$dateA = explode("-", $date);
if(checkdate($dateA[1], $dateA[2], $dateA[0]))
{
return true;
}
return false;
}
/**
* Checks if a string is alphanumeric
* @param array extra allowed chars
* @param string
* @return bool
*/
public function alphaNumeric($string, $allowed = array())
{
if(self::isEmpty($string)) return false;
// Capital
$minus = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
// Mayus
foreach($minus as $letter)
$mayus[] = strtoupper($letter);
// Mix them
$accepted = array_keys(array_flip(array_merge($minus, $mayus, $allowed)));
/*
* Work
* We will iterate each character looking if it is accepted
* A string is an array itself =)
*/
for($it = 0; $it < strlen($string); $it++)
{
if(!in_array($string[$it], $accepted, true))
{
if(!is_numeric($string[$it]))
return false;
}
}
return true;
}
/**
* Checks lenght of a string between a range given
* @param string
* @param int min
* @param int max
*/
public function validLenght($string, $min, $max)
{
if(strlen($string) < $min || strlen($string) > $max)
{
return false;
} else {
return true;
}
}
/**
* Checks wether an email is valid or not
* At leas __ @ _ . _ _ _
* Complex extensions supported (ej: .co.uk);
*
* @param string mail
* @return bool
*/
public function validMail($mail)
{
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $mail))
{
return false;
} else {
return true;
}
}
/**
* Checks that given field is unique in his table (ex: a nickname)
*
* @param string field name
* @param string value for field
* @param Model Name
*/
public function validUniqueField($fieldName, $value, $modelName)
{
$results = Query::searchBy($fieldName, $value, $modelName);
// If results are not NULL, that field value is already taken
if($results != NULL)
{
return false;
}
return true;
}
}
?>