A scalable php verification class,
You can adjust the implementation of various types of verification in the class. now it is the basic implementation method.
To add a rule, define the method directly. the method name is the rule name. For details, refer to usage.
- Require_once ('./Validator. class. php ');
- $ Data = array (
- 'Nickname' => 'heno ',
- 'Realname' => 'Steven ',
- 'Age' => 25,
- 'Mobile' => '123 ');
- $ Validator = new Validator ($ data );
- $ Validator-> setRule ('nickname', 'required ');
- $ Validator-> setRule ('realname', array ('length' => array (1, 6), 'required '));
- $ Validator-> setRule ('age', array ('requestred', 'digit '));
- $ Validator-> setRule ('mobile', array ('mobile '));
- $ Result = $ validator-> validate ();
- Var_dump ($ result );
- Var_dump ($ validator-> getResultInfo ());
- /**
- * Validator data verification class
- * @ Package library
- * @ Category library
- * @ Author Steven
- * @ Version 1.0
- */
- /**
- * Validator data verification 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;
- /**
- * Verification result
- * @ Var bool
- */
- Private $ _ result = null;
- /**
- * Verify data information
- * @ Var array
- */
- Private $ _ resultInfo = array ();
- /**
- * Constructor
- * @ Param array $ data to be verified
- */
- Public function _ construct ($ data = null)
- {
- If ($ data ){
- $ This-> _ data = $ data;
- }
- }
- /**
- * Set verification rules
- * @ Param string $ var with the verification key
- * @ Param mixed $ rule verification rule
- * @ 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;
- /* Return true if no validation rule is set */
- If ($ this-> _ ruleList === null |! Count ($ this-> _ ruleList )){
- Return $ result;
- }
- /* If rules have been set, check the rules one by one */
- Foreach ($ this-> _ ruleList as $ ruleKey => $ ruleItem ){
- /* If the validation rule is a single rule */
- If (! Is_array ($ ruleItem )){
- $ RuleItem = trim ($ ruleItem );
- If (method_exists ($ this, $ ruleItem )){
- /* Verify the data and save the verification result */
- $ TmpResult = $ this-> $ ruleItem ($ ruleKey );
- If (! $ TmpResult ){
- $ This-> _ resultInfo [$ ruleKey] [$ ruleItem] = $ tmpResult;
- $ Result = false;
- }
- }
- Continue;
- }
- /* Multiple validation rules */
- Foreach ($ ruleItem as $ ruleItemKey => $ rule ){
- If (! Is_array ($ rule )){
- $ Rule = trim ($ rule );
- If (method_exists ($ this, $ rule )){
- /* Verify the data and set the result set */
- $ TmpResult = $ this-> $ rule ($ ruleKey );
- If (! $ TmpResult ){
- $ This-> _ resultInfo [$ ruleKey] [$ rule] = $ tmpResult;
- $ Result = false;
- }
- }
- } Else {
- If (method_exists ($ this, $ ruleItemKey )){
- /* Verify the data and set the result set */
- $ TmpResult = $ this-> $ ruleItemKey ($ ruleKey, $ rule );
- If (! $ TmpResult ){
- $ This-> _ resultInfo [$ ruleKey] [$ ruleItemKey] = $ tmpResult;
- $ Result = false;
- }
- }
- }
- }
- }
- Return $ result;
- }
- /**
- * Obtain verification result data
- * @ Return [type] [description]
- */
- Public function getResultInfo ()
- {
- Return $ this-> _ resultInfo;
- }
- /**
- * Required parameters for verification
- * @ 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;
- }
- /**
- * Verification parameter length
- *
- * @ Param string $ varName verification item
- * @ Param array $ lengthData array ($ minLen, $ maxLen)
- * @ Return bool
- */
- Public function length ($ varName, $ lengthData)
- {
- $ Result = true;
- /* If this item is not set, it is verified by default */
- 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 the email
- * @ Param string $ varName verification item
- * @ Return bool
- */
- Public function email ($ varName)
- {
- $ Result = true;
- /* If this item is not set, it is verified by default */
- If ($ this-> required ($ varName )){
- $ Email = trim ($ this-> _ data [$ varName]);
- If (preg_match ('/^ [-\ w] +? @ [-\ W.] +? $/', $ Email )){
- $ Result = false;
- }
- }
- Return $ result;
- }
- /**
- * Verify mobile phone number
- * @ Param string $ varName verification item
- * @ Return bool
- */
- Public function mobile ($ varName)
- {
- $ Result = true;
- /* If this item is not set, it is verified by default */
- 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 ID card.
- * @ Param string $ varName verification item
- * @ Return bool
- */
- Public function ID ($ ID)
- {
- }
- /**
- * The verification parameter is URL.
- * @ Param string $ varName verification item
- * @ Return bool
- */
- Public function url ($ url)
- {
- $ Result = true;
- /* If this item is not set, it is verified by default */
- If ($ this-> required ($ varName )){
- $ Url = trim ($ this-> _ data [$ varName]);
- If (! Preg_match ('/^ (http [s]? ::)? \ W +? (\. \ W + ?) $/', $ Url )){
- $ Result = false;
- }
- }
- Return $ result;
- }
- }
- ?>
|