Created
June 27, 2011 16:04
-
-
Save dgmike/1049157 to your computer and use it in GitHub Desktop.
Validation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// How to use | |
require 'Validation.php'; | |
$validation = new Validation; | |
$validation->before_label = '<strong>'; | |
$validation->after_label = '</strong>'; | |
$erros = $validation->validate( $validacoes = array( | |
'campanha' => array('trim', 'required', 'minlength[3]', 'maxlength[50]'), | |
'cliente' => array('trim', 'required'), | |
'chapeu' => array('trim', 'required', 'minlength[3]', 'maxlength[45]'), | |
'chamada' => array('trim', 'required', 'minlength[3]', 'maxlength[45]'), | |
'url' => array('trim', 'required', 'minlength[5]', 'maxlength[120]', 'isURL'), | |
'pixel' => array('trim', 'required', 'minlength[5]', 'maxlength[120]', 'isURL'), | |
) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Classe de validacao de formularios | |
* | |
* PHP Version 5.2 | |
* | |
* @category Helper | |
* @package Virgula | |
* @author Michael Granados <[email protected]> | |
* @copyright 2011 (C) Michael Granados | |
* @license Creative Commons Atribuição 3.0 Brasil License http://creativecommons.org/licenses/by/3.0/br/ | |
* @link http://virgula.uol.com.br | |
*/ | |
/** | |
* Validate | |
* | |
* PHP Version 5.2 | |
* | |
* @category Helper | |
* @package Virgula | |
* @author Michael Granados <[email protected]> | |
* @copyright 2011 (C) Michael Granados | |
* @license Creative Commons Atribuição 3.0 Brasil License http://creativecommons.org/licenses/by/3.0/br/ | |
* @version Release: 1.0 | |
* @link http://virgula.uol.com.br | |
*/ | |
class Validation | |
{ | |
public $errors_messages = array( | |
'required' => 'O campo [before_label]%s[after_label] é obrigatório', | |
'isURL' => 'O campo [before_label]%s[after_label] deve conter uma URL válida', | |
'minlength' => 'O tamanho do campo [before_label]%s[after_label] não pode ser menor de %s caracteres', | |
'maxlength' => 'O tamanho do campo [before_label]%s[after_label] não deve ter mais de %s caracteres', | |
); | |
public $errors; | |
public $before_label = '"'; | |
public $after_label = '"'; | |
/** | |
* validate | |
* | |
* @param array $validation Dados que deseja validar | |
* @param array $args Argumentos para validar, por padrão, usa $_POST | |
* | |
* @static | |
* @return object Resultado das validacoes | |
*/ | |
public function validate(array $validation, $args = null) | |
{ | |
if (func_num_args() == 1) { | |
$args = $_POST; | |
} | |
foreach ($validation as $key => $validations) { | |
$value = isset($args[$key]) ? $args[$key] : ''; | |
$this->runValidations($value, $validations, $key); | |
} | |
return $this->errors; | |
} | |
/** | |
* run_validation - run validation for each field | |
* | |
* @param mixed $value Value, similar to $_POST['name'] | |
* @param array $validations Array of validations like array('trim', 'required') | |
* @param string $key Value to validate | |
* | |
* @return void | |
*/ | |
public function runValidations($value, array $validations, $key = false) | |
{ | |
foreach ($validations as $validation) { | |
if (preg_match('@^\w+@', $validation, $matches)) { | |
if (!$matches) { | |
continue; | |
} | |
$method = $matches[0]; | |
} | |
$args = preg_replace('@^\w+\[?@', '', $validation); | |
if ($args && substr($args, -1) == ']') { | |
$args = substr($args, 0, -1); | |
$args = explode(',', $args); | |
array_unshift($args, $value); | |
} else { | |
$args = array($value); | |
} | |
if (function_exists($method)) { | |
$value = call_user_func_array($method, $args); | |
} elseif ( method_exists($this, $method) | |
&& is_callable(array($this, $method))) { | |
$method = array($this, $method); | |
if ('filter_' === substr($method[1], 0, 7)) { | |
$value = call_user_func_array($method, $args); | |
} else { | |
$valid = (bool) call_user_func_array($method, $args); | |
if (!$valid) { | |
$args[0] = $key; | |
if (isset($this->errors_messages[$method[1]])) { | |
$msg = $this->errors_messages[$method[1]]; | |
$msg = str_replace('[before_label]', $this->before_label, $msg); | |
$msg = str_replace('[after_label]', $this->after_label, $msg); | |
$this->errors[$key] = vsprintf($msg, $args); | |
} else { | |
$this->errors[$key] = 'error on ' . $method[1]; | |
} | |
return false; | |
} | |
} | |
} | |
} | |
} | |
/** | |
* filterOnlyDigit - filter to retrive only digits | |
* | |
* @param string $string String to convert | |
* | |
* @return string | |
*/ | |
public function filterOnlyDigit($string) | |
{ | |
return preg_replace('@\D@', '', $string); | |
} | |
/** | |
* required - verifies if a string is not empty | |
* | |
* @param string $string String to validate | |
* | |
* @return bool | |
*/ | |
function required($string) | |
{ | |
return !empty($string); | |
} | |
/** | |
* isCPF - verifies if a string is a valid CPF | |
* | |
* @param string $string String to validate | |
* | |
* @return bool | |
*/ | |
public function isCPF($string) | |
{ | |
return (bool) String::isCPF($string); | |
} | |
/** | |
* isEmail - verifies if a string is valid email | |
* | |
* @param string $string String to validate | |
* | |
* @return bool | |
*/ | |
public function isEmail($string) | |
{ | |
return (bool) String::isEmail($string); | |
} | |
/** | |
* isURL - verifies if a string is valid URL | |
* | |
* @param string $string String to validate | |
* | |
* @uses String::isURL | |
* | |
* @return bool | |
*/ | |
public function isURL($string) | |
{ | |
return (bool) String::isURL($string); | |
} | |
/** | |
* minlength - verifies if a string is shorter than | |
* | |
* @param string $string String to validate | |
* @param int $size Value min to validate | |
* | |
* @uses String::isURL | |
* | |
* @return bool | |
*/ | |
public function minlength($string, $size) | |
{ | |
return !(strlen($string) < $size); | |
} | |
/** | |
* maxlength - verifies if a string is larger than | |
* | |
* @param string $string String to validate | |
* @param int $size Value max to validate | |
* | |
* @uses String::isURL | |
* | |
* @return bool | |
*/ | |
public function maxlength($string, $size) | |
{ | |
return !(strlen($string) > intval($size)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment