Code:<?php/*** Batch Validation class * @author [Lee] <[<www.dollarphp.com>]>* 1, verify that it is empty * 2, verify the data type * 3, verify that the length is compliant * 4, verify that it matches the regular match */ Class Validation{private $data;p rivate $msg = Array ();p ublic function __construct ($main) {foreach ($main as $k + = $v) { $this->data = $k; $bool = $v [0]; $type = $v [1]; $len _arr = $v [2]; $preg = $v [3]; $this->isempty ($bool)->datatype ($type)->datalength ($len _arr)->pregmath ($preg); }}private function IsEmpty ($bool) {$data = $this->data; if ($bool) {if (empty ($data)) {$msg = "' {$data} ' cannot be empty"; Array_push ($this->msg, $msg); }} return $this;} Private Function DataType ($type) {$data = $this->data; if ($type = = ' bool ') {if (!is_bool ($data)) {$msg = "' {$data} ' cannot be a non-boolean value"; Array_push ($this->msg, $msg); }}elseif ($type = = ' String ') {if (!is_string ($data)) {$msg = "' {$data} ' cannot be a non-string value"; Array_push ($thisMSG, $msg); }}elseif ($type = = ' int ') {if (!is_int ($data)) {$msg = "' {$data} ' cannot be a non-integer value"; Array_push ($this->msg, $msg); }}elseif ($type = = ' float ') {if (!is_float ($data)) {$msg = "' {$data} ' cannot be a non-floating-point value"; Array_push ($this->msg, $msg); }}elseif ($type = = ' array ') {if (!is_array ($data)) {$msg = "' {$data} ' cannot be a non-array value"; Array_push ($this->msg, $msg); }}elseif ($type = = ' object ') {if (!is_object ($data)) {$msg = "' {$data} ' cannot be a non-object value"; Array_push ($this->msg, $msg); }} return $this;} Private Function datalength ($len _arr) {$data = $this->data; $min = ABS ($len _arr[0]); $max = ABS ($len _arr[1]); $type = GetType ($data); if ($type = = ' string ') {$len = strlen ($data); }elseif ($type = = ' Integer ') {$len = strlen ($data); }elseif ($type = = ' Double ') {$len 0 = strlen ($data); $len 1 = Explode ('. ', $len 0); $len = $lEN1[1]; }elseif ($type = = ' array ') {$len = count ($data); }elseif ($type = = ' object ') {$data = (array) $data; $len = count ($data); } if (! ( $len >= $min && $len <= $max) {$msg = "' {$data} ' length cannot be less than {$min}, cannot be greater than {$max}"; Array_push ($this->msg, $msg); } return $this;} Private Function Pregmath ($preg) {$data = $this->data; if (!empty ($preg)) {if (!preg_match ($preg, $data)) {$msg = "' {$data} ' format does not match ' {$preg} '"; Array_push ($this->msg, $msg); }}}public function Checkdata () {$msg = $this->msg; return $msg;}} $data = Array (' Lee ' =>array (True, ' string ', Array (5,20), '/^a.*/') ), ' Hello ' =>array (false, ' int ', array (8,20), '/^a\s*/')); $val Idation = new Validation ($data), $ret = $validation->checkdata (); Var_dump ($ret);
Output:array(5) {[0]=>string(41) "‘lee‘长度不能小于5,不能大于20"[1]=>string(28) "‘lee‘格式不匹配‘/^a.*/‘"[2]=>string(28) "‘hello‘不能为非整型值"[3]=>string(43) "‘hello‘长度不能小于8,不能大于20"[4]=>string(31) "‘hello‘格式不匹配‘/^a\s*/‘"}
PHP Batch Validation class