colly_wyx
2018-05-09 d3534714ed8f07f19e0a648ab2d07c73d0bed156
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * APIÈë²Î¾²Ì¬¼ì²éÀà
 * ¿ÉÒÔ¶ÔAPIµÄ²ÎÊýÀàÐÍ¡¢³¤¶È¡¢×î´óÖµµÈ½øÐÐУÑé
 *
 **/
class RequestCheckUtil
{
    /**
     * Ð£Ñé×ֶΠfieldName µÄÖµ$value·Ç¿Õ
     *
     **/
    public static function checkNotNull($value,$fieldName) {
        
        if(self::checkEmpty($value)){
            throw new Exception("client-check-error:Missing Required Arguments: " .$fieldName , 40);
        }
    }
 
    /**
     * ¼ìÑé×Ö¶ÎfieldNameµÄÖµvalue µÄ³¤¶È
     *
     **/
    public static function checkMaxLength($value,$maxLength,$fieldName){        
        if(!self::checkEmpty($value) && mb_strlen($value , "UTF-8") > $maxLength){
            throw new Exception("client-check-error:Invalid Arguments:the length of " .$fieldName . " can not be larger than " . $maxLength . "." , 41);
        }
    }
 
    /**
     * ¼ìÑé×Ö¶ÎfieldNameµÄÖµvalueµÄ×î´óÁÐ±í³¤¶È
     *
     **/
    public static function checkMaxListSize($value,$maxSize,$fieldName) {    
 
        if(self::checkEmpty($value))
            return ;
 
        $list=preg_split("/,/",$value);
        if(count($list) > $maxSize){
                throw new Exception("client-check-error:Invalid Arguments:the listsize(the string split by \",\") of ". $fieldName . " must be less than " . $maxSize . " ." , 41);
        }
    }
 
    /**
     * ¼ìÑé×Ö¶ÎfieldNameµÄÖµvalue µÄ×î´óÖµ
     *
     **/
    public static function checkMaxValue($value,$maxValue,$fieldName){    
 
        if(self::checkEmpty($value))
            return ;
 
        self::checkNumeric($value,$fieldName);
 
        if($value > $maxValue){
                throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " can not be larger than " . $maxValue ." ." , 41);
        }
    }
 
    /**
     * ¼ìÑé×Ö¶ÎfieldNameµÄÖµvalue µÄ×îСֵ
     *
     **/
    public static function checkMinValue($value,$minValue,$fieldName) {
        
        if(self::checkEmpty($value))
            return ;
 
        self::checkNumeric($value,$fieldName);
        
        if($value < $minValue){
                throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " can not be less than " . $minValue . " ." , 41);
        }
    }
 
    /**
     * ¼ìÑé×Ö¶ÎfieldNameµÄÖµvalueÊÇ·ñÊÇnumber
     *
     **/
    protected static function checkNumeric($value,$fieldName) {
        if(!is_numeric($value))
            throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " is not number : " . $value . " ." , 41);
    }
 
    /**
     * Ð£Ñé$valueÊÇ·ñ·Ç¿Õ
     *  if not set ,return true;
     *    if is null , return true;
     *    
     *
     **/
    public static function checkEmpty($value) {
        if(!isset($value))
            return true ;
        if($value === null )
            return true;
        if(is_array($value) && count($value) == 0)
            return true;
        if(is_string($value) &&trim($value) === "")
            return true;
        
        return false;
    }
 
}
?>