RequestCheckUtil.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * API入参静态检查类
  4. * 可以对API的参数类型、长度、最大值等进行校验
  5. *
  6. **/
  7. class RequestCheckUtil
  8. {
  9. /**
  10. * 校验字段 fieldName 的值$value非空
  11. *
  12. **/
  13. public static function checkNotNull($value,$fieldName) {
  14. if(self::checkEmpty($value)){
  15. throw new Exception("client-check-error:Missing Required Arguments: " .$fieldName , 40);
  16. }
  17. }
  18. /**
  19. * 检验字段fieldName的值value 的长度
  20. *
  21. **/
  22. public static function checkMaxLength($value,$maxLength,$fieldName){
  23. if(!self::checkEmpty($value) && mb_strlen($value , "UTF-8") > $maxLength){
  24. throw new Exception("client-check-error:Invalid Arguments:the length of " .$fieldName . " can not be larger than " . $maxLength . "." , 41);
  25. }
  26. }
  27. /**
  28. * 检验字段fieldName的值value的最大列表长度
  29. *
  30. **/
  31. public static function checkMaxListSize($value,$maxSize,$fieldName) {
  32. if(self::checkEmpty($value))
  33. return ;
  34. $list=preg_split("/,/",$value);
  35. if(count($list) > $maxSize){
  36. throw new Exception("client-check-error:Invalid Arguments:the listsize(the string split by \",\") of ". $fieldName . " must be less than " . $maxSize . " ." , 41);
  37. }
  38. }
  39. /**
  40. * 检验字段fieldName的值value 的最大值
  41. *
  42. **/
  43. public static function checkMaxValue($value,$maxValue,$fieldName){
  44. if(self::checkEmpty($value))
  45. return ;
  46. self::checkNumeric($value,$fieldName);
  47. if($value > $maxValue){
  48. throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " can not be larger than " . $maxValue ." ." , 41);
  49. }
  50. }
  51. /**
  52. * 检验字段fieldName的值value 的最小值
  53. *
  54. **/
  55. public static function checkMinValue($value,$minValue,$fieldName) {
  56. if(self::checkEmpty($value))
  57. return ;
  58. self::checkNumeric($value,$fieldName);
  59. if($value < $minValue){
  60. throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " can not be less than " . $minValue . " ." , 41);
  61. }
  62. }
  63. /**
  64. * 检验字段fieldName的值value是否是number
  65. *
  66. **/
  67. protected static function checkNumeric($value,$fieldName) {
  68. if(!is_numeric($value))
  69. throw new Exception("client-check-error:Invalid Arguments:the value of " . $fieldName . " is not number : " . $value . " ." , 41);
  70. }
  71. /**
  72. * 校验$value是否非空
  73. * if not set ,return true;
  74. * if is null , return true;
  75. *
  76. *
  77. **/
  78. public static function checkEmpty($value) {
  79. if(!isset($value))
  80. return true ;
  81. if($value === null )
  82. return true;
  83. if(is_array($value) && count($value) == 0)
  84. return true;
  85. if(is_string($value) &&trim($value) === "")
  86. return true;
  87. return false;
  88. }
  89. }
  90. ?>