An extensible PHP validation class, Class can be a variety of validation can be self-tuning implementation, now the basic implementation. If you need to add a rule, define the method directly, and the method name is the rule name. Refer to the use method for specific reference.
- Require_once ('./validator.class.php ');
- $data = Array (
- ' Nickname ' = ' Heno ',
- ' Realname ' = ' Steven ',
- ' Age ' = 25,
- ' Mobile ' = ' 1521060426 ');
- $validator = new Validator ($data);
- $validator->setrule (' nickname ', ' required ');
- $validator->setrule (' Realname ', array (' length ' = = = Array (1,6), ' required '));
- $validator->setrule (' Age ', array (' required ', ' digit '));
- $validator->setrule (' Mobile ', Array (' mobile '));
- $result = $validator->validate ();
- Var_dump ($result);
- Var_dump ($validator->getresultinfo ());
Copy Code
-
- /**
- * Validator Data validation class
- * @package Library
- * @category Library
- * @author Steven
- * @version 1.0
- */
- /**
- * Validator Data validation class
- * @package Library
- * @category Library
- * @author Steven
- * @version 1.0
- */
- Class Validator {
- /**
- * Data to be verified
- * @var Array
- */
- Private $_data;
- /**
- * Validation rules
- * @var Array
- */
- Private $_rulelist = null;
- /**
- * Check Results
- * @var BOOL
- */
- Private $_result = null;
- /**
- * Verify data information
- * @var Array
- */
- Private $_resultinfo = Array ();
- /**
- * Constructor function
- * @param array $data The data to be verified
- */
- Public function __construct ($data = null)
- {
- if ($data) {
- $this->_data = $data;
- }
- }
- /**
- * Set validation rules
- * @param string $var with Check key
- * @param mixed $rule validation rules
- * @return void
- */
- Public Function Setrule ($var, $rule)
- {
- $this->_rulelist[$var] = $rule;
- }
- /**
- * Test Data
- * @param array $data
- *
* $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);
* $validator = new Validator($data);
* $validator->setRule('nickname', 'required');
* $validator->setRule('realname', array('lenght' => array(1,4), 'required'));
* $validator->setRule('age', array('required', 'digit'));
* $result = $validator->validate();
* var_dump($validator->getResultInfo());
*
- * @return BOOL
- */
- Public function Validate ($data = null)
- {
- $result = true;
- /* Returns TRUE if no validation rules are set */
- if ($this->_rulelist = = = NULL | |!count ($this->_rulelist)) {
- return $result;
- }
- /* rules are already set, then the rule is checked by article by clause */
- foreach ($this->_rulelist as $ruleKey = + $ruleItem) {
- /* If the test rule is a single rule */
- if (!is_array ($ruleItem)) {
- $ruleItem = Trim ($ruleItem);
- if (Method_exists ($this, $ruleItem)) {
- /* Verify the data, save the check results */
- $tmpResult = $this $ruleItem ($ruleKey);
- if (! $tmpResult) {
- $this->_resultinfo[$ruleKey] [$ruleItem] = $tmpResult;
- $result = false;
- }
- }
- Continue
- }
- /* Check rule is multiple */
- foreach ($ruleItem as $ruleItemKey = = $rule) {
- if (!is_array ($rule)) {
- $rule = Trim ($rule);
- if (Method_exists ($this, $rule)) {
- /* Validate data, set result set */
- $tmpResult = $this $rule ($ruleKey);
- if (! $tmpResult) {
- $this->_resultinfo[$ruleKey] [$rule] = $tmpResult;
- $result = false;
- }
- }
- } else {
- if (Method_exists ($this, $ruleItemKey)) {
- /* Validate data, set result set */
- $tmpResult = $this $ruleItemKey ($ruleKey, $rule);
- if (! $tmpResult) {
- $this->_resultinfo[$ruleKey] [$ruleItemKey] = $tmpResult;
- $result = false;
- }
- }
- }
- }
- }
- return $result;
- }
- /**
- * Get verification result data
- * @return [Type] [description]
- */
- Public Function Getresultinfo ()
- {
- return $this->_resultinfo;
- }
- /**
- * Check Required Parameters
- * @param string $varName checksum
- * @return BOOL
- */
- Public function required ($varName)
- {
- $result = false;
- if (Is_array ($this->_data) && isset ($this->_data[$varName])) {
- $result = true;
- }
- return $result;
- }
- /**
- * Check parameter length
- *
- * @param string $varName checksum
- * @param array $lengthData Array ($minLen, $maxLen)
- * @return BOOL
- */
- Public function Length ($varName, $lengthData)
- {
- $result = true;
- /* If the item is not set, the default is check through */
- if ($this->required ($varName)) {
- $varLen = Mb_strlen ($this->_data[$varName]);
- $minLen = $lengthData [0];
- $maxLen = $lengthData [1];
- if ($varLen < $minLen | | $varLen > $maxLen) {
- $result = true;
- }
- }
- return $result;
- }
- /**
- * Verify Mail
- * @param string $varName checksum
- * @return BOOL
- */
- Public Function Email ($varName)
- {
- $result = true;
- /* If the item is not set, the default is check through */
- if ($this->required ($varName)) {
- $email = Trim ($this->_data[$varName]);
- if (Preg_match ('/^[-\w]+?@[-\w. +?$/', $email)) {
- $result = false;
- }
- }
- return $result;
- }
- /**
- * Check your phone
- * @param string $varName checksum
- * @return BOOL
- */
- Public Function Mobile ($varName)
- {
- $result = true;
- /* If the item is not set, the default is check through */
- if ($this->required ($varName)) {
- $mobile = Trim ($this->_data[$varName]);
- if (!preg_match ('/^1[3458]\d{10}$/', $mobile)) {
- $result = false;
- }
- }
- return $result;
- }
- /**
- * Calibration parameters are numeric
- * @param string $varName checksum
- * @return BOOL
- */
- Public function digit ($varName)
- {
- $result = false;
- if ($this->required ($varName) && is_numeric ($this->_data[$varName])) {
- $result = true;
- }
- return $result;
- }
- /**
- * Check parameters for ID card
- * @param string $varName checksum
- * @return BOOL
- */
- Public Function ID ($ID)
- {
- }
- /**
- * Check parameter is URL
- * @param string $varName checksum
- * @return BOOL
- */
- Public function URL ($url)
- {
- $result = true;
- /* If the item is not set, the default is check through */
- if ($this->required ($varName)) {
- $url = Trim ($this->_data[$varName]);
- if (!preg_match ('/^ (http[s]?::)? \w+? ( \.\w+?) $/', $url)) {
- $result = false;
- }
- }
- return $result;
- }
- }
- ?>
Copy Code |