An extensible PHP verification class. Various verifications in the class can be adjusted and implemented by themselves. This is the basic implementation method now. If you need to add a rule, define the method directly, and the method name is the rule name. Please refer to the usage method for details.
- 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;
- / **
- * Verification rules
- * @var array
- */
- private $_ruleList = null;
- /**
- * Verification result
- * @var bool
- */
- private $_result = null;
- /**
- * Verification data information
- * @var array
- */
- private $_resultInfo = array();
- /**
- * Constructor
- * @param array $data Data to be verified
- */
- public function __construct($data = null)
- {
- if ($data) {
- $this->_data = $data;
- }
- }
-
- /**
- * Set verification rules
- * @param string $var with verification item key
- * @param mixed $rule Verification rules
- * @return void
- */
- public function setRule($var, $rule)
- {
- $this->_ruleList[$var] = $rule;
- }
-
- /**
- * 检验数据
- * @param array $data
- *
</li>
<li> * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);</li>
<li> * $validator = new Validator($data);</li>
<li> * $validator->setRule('nickname', 'required');</li>
<li> * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));</li>
<li> * $validator->setRule('age', array('required', 'digit'));</li>
<li> * $result = $validator->validate();</li>
<li> * var_dump($validator->getResultInfo());</li>
<li> *
- * @return bool
- */
- public function validate($ data = null)
- {
- $result = true;
-
- /* If no verification rules are set, return true directly */
- if ($this->_ruleList === null || !count($this-> _ruleList)) {
- return $result;
- }
-
- /* If the rules have been set, verify the rules one by one*/
- foreach ($this->_ruleList as $ruleKey => $ruleItem) {
-
- / * If the verification rule is a single rule*/
- if (!is_array($ruleItem)) {
- $ruleItem = trim($ruleItem);
- if (method_exists($this, $ruleItem)) {
-
- /* Verification data , save the verification result*/
- $tmpResult = $this->$ruleItem($ruleKey);
- if (!$tmpResult) {
- $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult;
- $result = false;
- }
- }
- continue;
- }
-
- /* There are multiple verification rules*/
- foreach ($ruleItem as $ruleItemKey => $rule) {
-
- if (!is_array($ rule)) {
- $rule = trim($rule);
- if (method_exists($this, $rule)) {
-
- /* Verify data, set result set*/
- $tmpResult = $this->$ rule($ruleKey);
- if (!$tmpResult) {
- $this->_resultInfo[$ruleKey][$rule] = $tmpResult;
- $result = false;
- }
- }
- } else {
- if ( method_exists($this, $ruleItemKey)) {
-
- /* Verify 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;
- }
-
- /**
- * Verification required parameters
- * @param string $varName verification item
- * @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 Check item
- * @param array $lengthData array($minLen, $maxLen)
- * @return bool
- */
- public function length($varName, $lengthData)
- {
- $result = true;
-
- /* If this item is not set, the default is verification passed*/
- 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;
- }
-
-
- /**
- * Verification email
- * @param string $varName verification item
- * @return bool
- */
- public function email($varName)
- {
- $result = true;
-
- /* If this item is not set, the default is verification passed*/
- if ($this- >required($varName)) {
- $email = trim($this->_data[$varName]);
- if (preg_match('/^[-w]+?@[-w.]+?$ /', $email)) {
- $result = false;
- }
- }
- return $result;
- }
-
- /**
- * Verify mobile phone
- * @param string $varName verification item
- * @return bool
- */
- public function mobile($varName)
- {
- $result = true ;
-
- /* If this item is not set, the default is verification passed*/
- if ($this->required($varName)) {
- $mobile = trim($this->_data[$varName]) ;
- if (!preg_match('/^1[3458]d{10}$/', $mobile)) {
- $result = false;
- }
- }
-
- return $result;
- }
-
- /**
- * The verification parameter is a number
- * @param string $varName verification item
- * @return bool
- */
- public function digit($varName)
- {
- $result = false;
- if ($this->required($varName) && is_numeric($this->_data[$varName])) {
- $result = true;
- }
- return $result;
- }
-
-
- /**
- * The verification parameter is the ID card
- * @param string $varName verification item
- * @return bool
- */
- public function ID($ID)
- {
-
- }
-
-
- /**
- * The verification parameter is the URL
- * @param string $varName verification item
- * @return bool
- */
- public function url($url)
- {
- $result = true;
-
- /* If this item is not set, the default is verification passed*/
- if ($this->required($varName)) {
- $ url = trim($this->_data[$varName]);
- if(!preg_match('/^(http[s]?::)?w+?(.w+?)$/', $url)) {
- $result = false;
- }
- }
- return $result;
- }
- }
- ?>
-
Copy code
|