<?php
namespace Model;
class Validation {
private $_rules = array(); private $_errors = array();
public function setRule($rule, $value, $params = null, $name = null) {
$this->_rules
[] = array('val' => $value, 'rule' => $rule, 'par' => $params, 'name' => $name); return $this;
}
public function validate() {
$this->_errors
= array(); if (count($this->_rules
) > 0) { foreach ($this->_rules as $v) {
if (!$this->$v['rule']($v['val'], $v['par'])) {
if ($v['name']) {
$this->_errors[] = $v['name'];
} else {
$this->_errors[] = $v['rule'];
}
}
}
}
return (bool
) !count($this->_errors
); }
public function getErrors() {
return $this->_errors;
}
public function __call($a, $b) {
//TODO
throw new \Exception('Invalid validation rule', 500);
}
public static function required($val) {
} else {
return $val != '';
}
}
public static function matches($val1, $val2) {
return $val1 == $val2;
}
public static function matchesStrict($val1, $val2) {
return $val1 === $val2;
}
public static function different($val1, $val2) {
return $val1 != $val2;
}
public static function differentStrict($val1, $val2) {
return $val1 !== $val2;
}
public static function minlenght($val, $param) {
}
public static function maxlenght($val, $param) {
}
public static function exaclenght($val, $param) {
}
public static function big($val1, $val2) {
return $val1 > $val2;
}
public static function litle($val1, $val2) {
return $val1 < $val2;
}
public static function letter($val) {
return (bool
) \
preg_match('/^([a-z])+$/i/', $val); }
public static function letternum($val) {
return (bool
) \
preg_match('/^([a-z0-9])+$/i/', $val); }
public static function letternumdash($val) {
return (bool
) \
preg_match('/^([a-z0-9_-])+$/i/', $val); }
public static function num($val) {
}
public static function emaill($val) {
return \
filter_var($val, FILTER_VALIDATE_EMAIL
) !== false; }
public static function emaills($val) {
foreach ($val as $v) {
if (!self::emaill($v)) {
return false;
}
}
} else {
return false;
}
return true;
}
public static function url($val) {
return \
filter_var($val, FILTER_VALIDATE_URL
) !== false; }
public static function ip($val) {
return \
filter_var($val, FILTER_VALIDATE_IP
) !== false; }
public static function regexp($val1, $val2) {
}
public static function custom($val1, $val2) {
if ($val2 instanceof \Closure) {
} else {
throw new \Exception('Invalid validation function', 500);
}
}
}