diff --git a/LANG/de.php b/LANG/de.php new file mode 100644 index 0000000..118c0f9 --- /dev/null +++ b/LANG/de.php @@ -0,0 +1,17 @@ + [ + "val_pattern" => "Ungültiges Datenformat.", + "val_required" => "Dieses Feld ist erforderlich.", + "val_min" => "Der Wert ist zu klein.", + "val_max" => "Der Wert ist zu groß.", + "val_equal" => "Der Wert sollte gleich sein wie ", + "val_file_max_size" => "Die Datei ist zu groß.", + "val_file_ext" => "Ungültiges Dateiformat.", + ], +)); diff --git a/LANG/en.php b/LANG/en.php new file mode 100644 index 0000000..5bb0249 --- /dev/null +++ b/LANG/en.php @@ -0,0 +1,17 @@ + [ + "val_pattern" => "Invalid data format.", + "val_required" => "This field is required.", + "val_min" => "The value is too small.", + "val_max" => "The value is too large.", + "val_equal" => "The value should be equal to ", + "val_file_max_size" => "The file is too large.", + "val_file_ext" => "Invalid file format.", + ], +)); diff --git a/LANG/es.php b/LANG/es.php new file mode 100644 index 0000000..35d1d5a --- /dev/null +++ b/LANG/es.php @@ -0,0 +1,17 @@ + [ + "val_pattern" => "Formato de datos inválido.", + "val_required" => "Este campo es obligatorio.", + "val_min" => "El valor es demasiado pequeño.", + "val_max" => "El valor es demasiado grande.", + "val_equal" => "El valor debe ser igual a ", + "val_file_max_size" => "El archivo es demasiado grande.", + "val_file_ext" => "Formato de archivo inválido.", + ], +)); diff --git a/LANG/fr.php b/LANG/fr.php new file mode 100644 index 0000000..64ced36 --- /dev/null +++ b/LANG/fr.php @@ -0,0 +1,18 @@ + [ + "val_pattern" => "Format de données invalide.", + "val_required" => "Ce champ est requis.", + "val_min" => "La valeur est trop petite.", + "val_max" => "La valeur est trop grande.", + "val_equal" => "La valeur doit être égale à ", + "val_file_max_size" => "Le fichier est trop volumineux.", + "val_file_ext" => "Format de fichier invalide.", + ], +)); diff --git a/LANG/it.php b/LANG/it.php new file mode 100644 index 0000000..df78bbf --- /dev/null +++ b/LANG/it.php @@ -0,0 +1,18 @@ + [ + "val_pattern" => "Formato dati non valido.", + "val_required" => "Questo campo è obbligatorio.", + "val_min" => "Il valore è troppo piccolo.", + "val_max" => "Il valore è troppo grande.", + "val_equal" => "Il valore dovrebbe essere uguale a ", + "val_file_max_size" => "Il file è troppo grande.", + "val_file_ext" => "Formato file non valido.", + ], +)); diff --git a/LANG/pl.php b/LANG/pl.php new file mode 100644 index 0000000..b0f114e --- /dev/null +++ b/LANG/pl.php @@ -0,0 +1,18 @@ + [ + "val_pattern" => "Niepoprawny format danych.", + "val_required" => "Pole jest wymagane.", + "val_min" => "Wartość jest zbyt mała.", + "val_max" => "Wartość jest zbyt duza.", + "val_equal" => "Wartość powinna być równa ", + "val_file_max_size" => "Plik jest zbyt duży.", + "val_file_ext" => "Niedozwolony format pliku.", + ], +)); diff --git a/LANG/pt.php b/LANG/pt.php new file mode 100644 index 0000000..446fdb0 --- /dev/null +++ b/LANG/pt.php @@ -0,0 +1,18 @@ + [ + "val_pattern" => "Formato de dados inválido.", + "val_required" => "Este campo é obrigatório.", + "val_min" => "O valor é muito pequeno.", + "val_max" => "O valor é muito grande.", + "val_equal" => "O valor deve ser igual a ", + "val_file_max_size" => "O arquivo é muito grande.", + "val_file_ext" => "Formato de arquivo inválido.", + ], +)); diff --git a/LANG/ru.php b/LANG/ru.php new file mode 100644 index 0000000..7fe4c17 --- /dev/null +++ b/LANG/ru.php @@ -0,0 +1,17 @@ + [ + "val_pattern" => "Неверный формат данных.", + "val_required" => "Это поле обязательно для заполнения.", + "val_min" => "Значение слишком маленькое.", + "val_max" => "Значение слишком большое.", + "val_equal" => "Значение должно быть равным ", + "val_file_max_size" => "Файл слишком большой.", + "val_file_ext" => "Неверный формат файла.", + ], +)); diff --git a/Validation.php b/Validation.php index f3650cc..e5d1e11 100644 --- a/Validation.php +++ b/Validation.php @@ -1,389 +1,433 @@ + * @license https://github.com/davidecesarano/Validation/blob/master/LICENSE MIT License + * @link https://github.com/davidecesarano/Validation + */ + +class Validation { + + /** + * @var array $patterns + */ + public $patterns = array( + 'uri' => '[A-Za-z0-9-\/_?&=]+', + 'url' => '[A-Za-z0-9-:.\/_?&=#]+', + 'alpha' => '[\p{L}]+', + 'words' => '[\p{L}\s]+', + 'alphanum' => '[\p{L}0-9]+', + 'int' => '[0-9]+', + 'float' => '[0-9\.,]+', + 'tel' => '[0-9+\s()-]+', + 'text' => '[\p{L}0-9\s-.,;:!"%&()?+\'°#\/@]+', + 'file' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+\.[A-Za-z0-9]{2,4}', + 'folder' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+', + 'address' => '[\p{L}0-9\s.,()°-]+', + 'date_dmy' => '[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}', + 'date_ymd' => '[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}' + ); + + /** + * @var array $errors + */ + public $errors = array(); + + /** + * @var mixed $value + */ + public $value = null; + + /** + * @var string $name + */ + public $name = null; + + /** + * @var mixed $file + */ + public $file = null; + /** - * Validation + * Field name * - * Semplice classe PHP per la validazione. + * @param string $name + * @return self + */ + public function name(string $name) : Validation + { + $this->name = $name; + return $this; + } + + /** + * Field value * - * @author Davide Cesarano - * @copyright (c) 2016, Davide Cesarano - * @license https://github.com/davidecesarano/Validation/blob/master/LICENSE MIT License - * @link https://github.com/davidecesarano/Validation + * @param mixed $value + * @return self */ - - class Validation { - - /** - * @var array $patterns - */ - public $patterns = array( - 'uri' => '[A-Za-z0-9-\/_?&=]+', - 'url' => '[A-Za-z0-9-:.\/_?&=#]+', - 'alpha' => '[\p{L}]+', - 'words' => '[\p{L}\s]+', - 'alphanum' => '[\p{L}0-9]+', - 'int' => '[0-9]+', - 'float' => '[0-9\.,]+', - 'tel' => '[0-9+\s()-]+', - 'text' => '[\p{L}0-9\s-.,;:!"%&()?+\'°#\/@]+', - 'file' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+\.[A-Za-z0-9]{2,4}', - 'folder' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+', - 'address' => '[\p{L}0-9\s.,()°-]+', - 'date_dmy' => '[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}', - 'date_ymd' => '[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}', - 'email' => '[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+[.]+[a-z-A-Z]' - ); - - /** - * @var array $errors - */ - public $errors = array(); - - /** - * Nome del campo - * - * @param string $name - * @return this - */ - public function name($name){ - - $this->name = $name; - return $this; - - } - - /** - * Valore del campo - * - * @param mixed $value - * @return this - */ - public function value($value){ - - $this->value = $value; - return $this; - - } - - /** - * File - * - * @param mixed $value - * @return this - */ - public function file($value){ - - $this->file = $value; - return $this; - - } - - /** - * Pattern da applicare al riconoscimento - * dell'espressione regolare - * - * @param string $name nome del pattern - * @return this - */ - public function pattern($name){ - - if($name == 'array'){ - - if(!is_array($this->value)){ - $this->errors[] = 'Formato campo '.$this->name.' non valido.'; - } - - }else{ - - $regex = '/^('.$this->patterns[$name].')$/u'; - if($this->value != '' && !preg_match($regex, $this->value)){ - $this->errors[] = 'Formato campo '.$this->name.' non valido.'; - } - + public function value($value) : Validation + { + $this->value = $value; + return $this; + } + + /** + * File + * + * @param mixed $value + * @return self + */ + public function file($value) : Validation + { + $this->file = $value; + return $this; + } + + /** + * Pattern to be used for regular expression matching + * + * @param string $name pattern name + * @return self + */ + public function pattern(string $name) : Validation + { + + if($name == 'array'){ + + if(!is_array($this->value)){ + $this->errors[$this->name] = array('error' => "val_pattern"); } - return $this; - + + }elseif($name == 'email'){ + if($this->value != '' && !filter_var($this->value, FILTER_VALIDATE_EMAIL)){ + + $this->errors[$this->name] = array('error' => "val_pattern"); + } } - - /** - * Pattern personalizzata - * - * @param string $pattern - * @return this - */ - public function customPattern($pattern){ - - $regex = '/^('.$pattern.')$/u'; + else{ + + $regex = '/^('.$this->patterns[$name].')$/u'; if($this->value != '' && !preg_match($regex, $this->value)){ - $this->errors[] = 'Formato campo '.$this->name.' non valido.'; + $this->errors[$this->name] = array('error' => "val_pattern"); } - return $this; - + } - - /** - * Campo obbligatorio - * - * @return this - */ - public function required(){ - - if((isset($this->file) && $this->file['error'] == 4) || ($this->value == '' || $this->value == null)){ - $this->errors[] = 'Campo '.$this->name.' obbligatorio.'; - } - return $this; - + return $this; + } + + /** + * Custom pattern + * + * @param string $pattern + * @return self + */ + public function customPattern(string $pattern) : Validation + { + $regex = '/^('.$pattern.')$/u'; + if($this->value != '' && !preg_match($regex, $this->value)){ + $this->errors[$this->name] = array('error' => "val_pattern"); } - - /** - * Lunghezza minima - * del valore del campo - * - * @param int $min - * @return this - */ - public function min($length){ - - if(is_string($this->value)){ - - if(strlen($this->value) < $length){ - $this->errors[] = 'Valore campo '.$this->name.' inferiore al valore minimo'; - } - - }else{ - - if($this->value < $length){ - $this->errors[] = 'Valore campo '.$this->name.' inferiore al valore minimo'; - } - - } - return $this; - + return $this; + } + + /** + * Required field + * + * @return self + */ + public function required() : Validation + { + if((isset($this->file) && $this->file['error'] == 4) || ($this->value == '' || $this->value == null)){ + $this->errors[$this->name] = array('error' => "val_required"); } - - /** - * Lunghezza massima - * del valore del campo - * - * @param int $max - * @return this - */ - public function max($length){ - - if(is_string($this->value)){ - - if(strlen($this->value) > $length){ - $this->errors[] = 'Valore campo '.$this->name.' superiore al valore massimo'; - } - - }else{ - - if($this->value > $length){ - $this->errors[] = 'Valore campo '.$this->name.' superiore al valore massimo'; - } - + return $this; + } + + /** + * Minimum length of field value + * + * @param int $min + * @return self + */ + public function min(int $length) : Validation + { + if(is_string($this->value)){ + + if(strlen($this->value) < $length){ + $this->errors[$this->name] = array('error' => "val_min"); } - return $this; - - } - - /** - * Confronta con il valore di - * un altro campo - * - * @param mixed $value - * @return this - */ - public function equal($value){ - - if($this->value != $value){ - $this->errors[] = 'Valore campo '.$this->name.' non corrispondente.'; + + }else{ + + if($this->value < $length){ + $this->errors[$this->name] = array('error' => "val_min"); } - return $this; - + } - - /** - * Dimensione massima del file - * - * @param int $size - * @return this - */ - public function maxSize($size){ - - if($this->file['error'] != 4 && $this->file['size'] > $size){ - $this->errors[] = 'Il file '.$this->name.' supera la dimensione massima di '.number_format($size / 1048576, 2).' MB.'; + return $this; + } + + /** + * Maximum length of field value + * + * @param int $max + * @return self + */ + public function max($length) : Validation + { + if(is_string($this->value)){ + + if(strlen($this->value) > $length){ + $this->errors[$this->name] = array('error' => "val_max"); } - return $this; - - } - - /** - * Estensione (formato) del file - * - * @param string $extension - * @return this - */ - public function ext($extension){ - - if($this->file['error'] != 4 && pathinfo($this->file['name'], PATHINFO_EXTENSION) != $extension && strtoupper(pathinfo($this->file['name'], PATHINFO_EXTENSION)) != $extension){ - $this->errors[] = 'Il file '.$this->name.' non è un '.$extension.'.'; + + }else{ + + if($this->value > $length){ + $this->errors[$this->name] = array('error' => "val_max"); } - return $this; - + } - - /** - * Purifica per prevenire attacchi XSS - * - * @param string $string - * @return $string - */ - public function purify($string){ - return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); + return $this; + } + + /** + * Compare with the value of another field + * + * @param mixed $value + * @return self + */ + public function equal($value) : Validation + { + if($this->value != $value){ + $this->errors[$this->name] = array('error' => "val_equal"); } - - /** - * Campi validati - * - * @return boolean - */ - public function isSuccess(){ - if(empty($this->errors)) return true; + return $this; + } + + /** + * Maximum file size + * + * @param int $size + * @return self + */ + public function maxSize(int $size) : Validation + { + if($this->file['error'] != 4 && $this->file['size'] > $size){ + $this->errors[$this->name] = array('error' => "val_file_max_size", 'size' => number_format($size / 1048576, 2)); } - - /** - * Errori della validazione - * - * @return array $this->errors - */ - public function getErrors(){ - if(!$this->isSuccess()) return $this->errors; + return $this; + } + + /** + * File extension + * + * @param string $extension + * @return self + */ + public function ext(string $extension) : Validation + { + if($this->file['error'] != 4 && pathinfo($this->file['name'], PATHINFO_EXTENSION) != $extension && strtoupper(pathinfo($this->file['name'], PATHINFO_EXTENSION)) != $extension){ + $this->errors[$this->name] = array('error' => "val_file_ext", 'ext' => $extension); } - - /** - * Visualizza errori in formato Html - * - * @return string $html - */ - public function displayErrors(){ - - $html = ''; - - return $html; - + return $this; + } + + /** + * Sanitize against XSS attacks + * + * @param string $string + * @return $string + */ + public function purify(string $string) : string + { + return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); + } + + /** + * Check if fields are valid + * + * @return boolean + */ + public function isSuccess() : bool + { + return empty($this->errors); + } + + /** + * Validation errors + * + * @return array $this->errors + */ + public function getErrors() : array|null + { + if(!$this->isSuccess()){ + return $this->errors; + }else{ + return null; } - - /** - * Visualizza risultato della validazione - * - * @return booelan|string - */ - public function result(){ - - if(!$this->isSuccess()){ - - foreach($this->getErrors() as $error){ - echo "$error\n"; - } - exit; - - }else{ - return true; + } + + /** + * Display errors in HTML format + * + * @return string $html + */ + public function displayErrors() : string + { + $html = ''; + + return $html; + } + + /** + * Display validation result + * + * @return void + */ + public function result() : void + { + if(!$this->isSuccess()){ + foreach($this->getErrors() as $error){ + echo "$error\n"; } - } - - /** - * Verifica se il valore è - * un numero intero - * - * @param mixed $value - * @return boolean - */ - public static function is_int($value){ - if(filter_var($value, FILTER_VALIDATE_INT)) return true; + } + + /** + * Check if value is an integer + * + * @param mixed $value + * @return boolean + */ + public static function is_int($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_INT)){ + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * un numero float - * - * @param mixed $value - * @return boolean - */ - public static function is_float($value){ - if(filter_var($value, FILTER_VALIDATE_FLOAT)) return true; + } + + /** + * Check if value is a float + * + * @param mixed $value + * @return boolean + */ + public static function is_float($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_FLOAT)){ + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * una lettera dell'alfabeto - * - * @param mixed $value - * @return boolean - */ - public static function is_alpha($value){ - if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")))) return true; + } + + /** + * Check if value is an alphabetic character + * + * @param mixed $value + * @return boolean + */ + public static function is_alpha($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")))){ + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * una lettera o un numero - * - * @param mixed $value - * @return boolean - */ - public static function is_alphanum($value){ - if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")))) return true; + } + + /** + * Check if value is an alphanumeric character + * + * @param mixed $value + * @return boolean + */ + public static function is_alphanum($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")))){ + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * un url - * - * @param mixed $value - * @return boolean - */ - public static function is_url($value){ - if(filter_var($value, FILTER_VALIDATE_URL)) return true; + } + + /** + * Check if value is a URL + * + * @param mixed $value + * @return boolean + */ + public static function is_url($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_URL)) { + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * un uri - * - * @param mixed $value - * @return boolean - */ - public static function is_uri($value){ - if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[A-Za-z0-9-\/_]+$/")))) return true; + } + + /** + * Check if value is a URI + * + * @param mixed $value + * @return boolean + */ + public static function is_uri($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[A-Za-z0-9-\/_?&=]+$/")))){ + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * true o false - * - * @param mixed $value - * @return boolean - */ - public static function is_bool($value){ - if(is_bool(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) return true; + } + + /** + * Check if value is true or false + * + * @param mixed $value + * @return boolean + */ + public static function is_bool($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_BOOLEAN)) { + return true; + }else{ + return false; } - - /** - * Verifica se il valore è - * un'e-mail - * - * @param mixed $value - * @return boolean - */ - public static function is_email($value){ - if(filter_var($value, FILTER_VALIDATE_EMAIL)) return true; + } + + /** + * Check if value is an email address + * + * @param mixed $value + * @return boolean + */ + public static function is_email($value) : bool + { + if(filter_var($value, FILTER_VALIDATE_EMAIL)) { + return true; + }else{ + return false; } - - } \ No newline at end of file + } + +}