Validator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use yii\validators\EmailValidator;
  9. use yii\validators\NumberValidator;
  10. use yii\validators\StringValidator;
  11. use yii2mod\validators\PhoneValidator;
  12. class Validator
  13. {
  14. public $error;
  15. public $success;
  16. /**
  17. * Validator constructor.
  18. * @param $attribute
  19. * [
  20. * ['string',$value|string or array,[],messge|string or array]
  21. * ]
  22. */
  23. public function __construct($attribute)
  24. {
  25. if (!is_array($attribute[0])) {
  26. $attribute = [ $attribute ];
  27. }
  28. foreach ($attribute as $v) {
  29. $rm = method_exists(self::class,$v[0]) ? new \ReflectionMethod(self::class,$v[0]) : false;
  30. if ($rm === false || !$rm->isStatic()) {
  31. $this->success = false;
  32. $this->error = json_encode($attribute).'方法不存在';
  33. return false;
  34. }
  35. if (is_array($v[1])) {
  36. foreach ($v[1] as $key => $val) {
  37. if (!call_user_func_array([self::class,$v[0]],[$val,$v[2]])) {
  38. $this->success = false;
  39. $this->error = isset($v[3]) && isset($v[3][$key]) ? $v[3][$key] : false;
  40. return false;
  41. }
  42. }
  43. } else {
  44. if (!call_user_func_array([self::class,$v[0]],[$v[1],$v[2]])) {
  45. $this->success = false;
  46. $this->error = isset($v[3]) ? $v[3] : false;
  47. return false;
  48. }
  49. }
  50. $this->success = true;
  51. $this->error = null;
  52. return true;
  53. }
  54. }
  55. /**
  56. * 验证字符串
  57. * @param $value
  58. * @param $attribute
  59. * @return bool
  60. */
  61. public static function string ($value, $attribute)
  62. {
  63. $valid = new StringValidator($attribute);
  64. return $valid->validate($value,$error);
  65. }
  66. /**
  67. * 验证数字
  68. * @param $value
  69. * @param $attribute
  70. * @return bool
  71. */
  72. public static function number ($value, $attribute)
  73. {
  74. $valid = new NumberValidator($attribute);
  75. return $valid->validate($value, $error);
  76. }
  77. /**
  78. * 验证邮箱
  79. * @param $value
  80. * @return bool
  81. */
  82. public static function email ($value, $attribute = [])
  83. {
  84. $valid = new EmailValidator();
  85. return $valid->validate($value, $error);
  86. }
  87. /**
  88. * 验证手机号
  89. * @param $value
  90. * @return bool
  91. */
  92. public static function phone ($value, $attribute = [])
  93. {
  94. $chars = "/^1[3-9]\d{9}$/";
  95. if (preg_match($chars, $value)){
  96. return true;
  97. }else{
  98. return false;
  99. }
  100. }
  101. /**
  102. * 验证单选
  103. * @param $value
  104. * @param $attribute
  105. * @return bool
  106. */
  107. public static function radio ($value, $attribute)
  108. {
  109. if (in_array($value,$attribute)) {
  110. return true;
  111. }else {
  112. return false;
  113. }
  114. }
  115. }