RequestCheckUtil.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * API��ξ�̬�����
  4. * ���Զ�API�IJ������͡����ȡ����ֵ�Ƚ���У��
  5. *
  6. **/
  7. class RequestCheckUtil {
  8. /**
  9. * У���ֶ� fieldName ��ֵ$value�ǿ�
  10. *
  11. **/
  12. public static function checkNotNull($value, $fieldName) {
  13. if (self::checkEmpty($value)) {
  14. throw new Exception("client-check-error:Missing Required Arguments: ".$fieldName, 40);
  15. }
  16. }
  17. /**
  18. * �����ֶ�fieldName��ֵvalue �ij���
  19. *
  20. **/
  21. public static function checkMaxLength($value, $maxLength, $fieldName) {
  22. if (!self::checkEmpty($value) && mb_strlen($value, "UTF-8") > $maxLength) {
  23. throw new Exception(
  24. "client-check-error:Invalid Arguments:the length of ".$fieldName." can not be larger than ".$maxLength
  25. .".", 41
  26. );
  27. }
  28. }
  29. /**
  30. * �����ֶ�fieldName��ֵvalue������б���
  31. *
  32. **/
  33. public static function checkMaxListSize($value, $maxSize, $fieldName) {
  34. if (self::checkEmpty($value)) {
  35. return;
  36. }
  37. $list = preg_split("/,/", $value);
  38. if (count($list) > $maxSize) {
  39. throw new Exception(
  40. "client-check-error:Invalid Arguments:the listsize(the string split by \",\") of ".$fieldName
  41. ." must be less than ".$maxSize." .", 41
  42. );
  43. }
  44. }
  45. /**
  46. * �����ֶ�fieldName��ֵvalue �����ֵ
  47. *
  48. **/
  49. public static function checkMaxValue($value, $maxValue, $fieldName) {
  50. if (self::checkEmpty($value)) {
  51. return;
  52. }
  53. self::checkNumeric($value, $fieldName);
  54. if ($value > $maxValue) {
  55. throw new Exception(
  56. "client-check-error:Invalid Arguments:the value of ".$fieldName." can not be larger than ".$maxValue
  57. ." .", 41
  58. );
  59. }
  60. }
  61. /**
  62. * �����ֶ�fieldName��ֵvalue ����Сֵ
  63. *
  64. **/
  65. public static function checkMinValue($value, $minValue, $fieldName) {
  66. if (self::checkEmpty($value)) {
  67. return;
  68. }
  69. self::checkNumeric($value, $fieldName);
  70. if ($value < $minValue) {
  71. throw new Exception(
  72. "client-check-error:Invalid Arguments:the value of ".$fieldName." can not be less than ".$minValue." .",
  73. 41
  74. );
  75. }
  76. }
  77. /**
  78. * �����ֶ�fieldName��ֵvalue�Ƿ���number
  79. *
  80. **/
  81. protected static function checkNumeric($value, $fieldName) {
  82. if (!is_numeric($value)) {
  83. throw new Exception(
  84. "client-check-error:Invalid Arguments:the value of ".$fieldName." is not number : ".$value." .", 41
  85. );
  86. }
  87. }
  88. /**
  89. * У��$value�Ƿ�ǿ�
  90. * if not set ,return true;
  91. * if is null , return true;
  92. *
  93. *
  94. **/
  95. public static function checkEmpty($value) {
  96. if (!isset($value)) {
  97. return true;
  98. }
  99. if ($value === null) {
  100. return true;
  101. }
  102. if (trim($value) === "") {
  103. return true;
  104. }
  105. return false;
  106. }
  107. }
  108. ?>