<?php
class Validar {
// Mensaje
protected $_mensaje;
// Errores
protected $_errors;
// Patron de expresion regular
protected $_patron;
// Constantes para los tipos de validacion
protected $VALIDA_TEXTO = 1;
protected $VALIDA_NUM = 2;
protected $VALIDA_EMAIL = 4;
/**
* validar()
*
* función principal, donde se mandaran a llamar los diferentes tipos
* de validaciones.
*
* @param string $campo Nombre del campo el cual se validara
* @param string $valor valor del campo a validar
* @param int $tipo de valicación a ejecutar
* @param int $noEmpty bandera para verificar que no este vacio el campo.
*
**/
public function validar($campo, $valor, $tipo)
{
if ($tipo == $this->VALIDA_TEXTO )
{
$valida = $this->_texto($valor);
if($valida != true ) {
$this->_mensaje['error'] .= "Campo {$campo}, debe contener solo texo%%";
}
}
if ($tipo == $this->VALIDA_NUM)
{
$valida = $this->_numero($valor);
if($valida != true ) {
$this->_mensaje['error'] .= "Campo {$campo}, debe contener solo Numeros%%";
}
}
if ($tipo == $this->VALIDA_EMAIL)
{
$valida = $this->_email($valor);
if($valida != true ) {
$this->_mensaje['error'] .= "Campo {$campo}, debe contener una cuenta valida%%";
}
}
}
/**
* getErrors()
*
* Regresa los errores que puedan existir
*
* return string $errors errores encontrados
*/
public function getErrors()
{
$this->_errors = $this->_mensaje['error'];
if ($this->_errors)
{
//hago espli de los %% que se generan cada que escribo un mensaje de error.
$errores = split("%%", $this->_errors
);
foreach ($errores as $error):
$errors .= "<p class='error'>".$error."</p>";
endforeach;
$this->_errors = $errors;
}
return $this->_errors;
}
/**
* _texto
*
* Valida que el valor sea solo texto
*
* @param string $valor a validar
*
* @return true
*/
private function _texto($valor)
{
$expresion = true;
$this->_patron = "/^[a-zA-Z ,.áéíóúÁÉÍÓÚñÑ\- 0-9]*$/";
return $expresion;
}
/**
* _numero
*
* Valida que solo se contengan numeros
*
* @param string $valor a validar
*
* @return true
*/
private function _numero($valor)
{
$this->_patron = "/^[0-9]*$/";
return $expresion;
}
/**
* _email()
*
* Valida que el valor sea una cuenta de correo electronico
*
*
* @param string $valor a validar
*
* @return true
*
**/
private function _email($valor)
{
$this->_patron = "/^([-A-z0-9\.])+@(([-A-z0-9]+\.){1,3})([-A-z0-9]{2,4})$/";
return $expresion;
}
}
?>