BaseValidate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Validate;
  3. use Illuminate\Support\Facades\Validator;
  4. /**
  5. * 扩展验证器
  6. */
  7. class BaseValidate {
  8. /**
  9. * 当前验证规则
  10. * @var array
  11. */
  12. protected $rule = [];
  13. /**
  14. * 验证提示信息
  15. * @var array
  16. */
  17. protected $message = [];
  18. /**
  19. * 验证场景定义
  20. * @var array
  21. */
  22. protected $scene = [];
  23. /**
  24. * 设置当前验证场景
  25. * @var array
  26. */
  27. protected $currentScene = null;
  28. /**
  29. * 验证失败错误信息
  30. * @var array
  31. */
  32. protected $error = [];
  33. /**
  34. * 场景需要验证的规则
  35. * @var array
  36. */
  37. protected $only = [];
  38. /**
  39. * 设置验证场景
  40. * @access public
  41. * @param string $name 场景名
  42. * @return $this
  43. */
  44. public function scene($name)
  45. {
  46. // 设置当前场景
  47. $this->currentScene = $name;
  48. return $this;
  49. }
  50. /**
  51. * 数据验证
  52. * @access public
  53. * @param array $data 数据
  54. * @param mixed $rules 验证规则
  55. * @param array $message 自定义验证信息
  56. * @param string $scene 验证场景
  57. * @return bool
  58. */
  59. public function check($data, $rules = [], $message = [],$scene = '')
  60. {
  61. $this->error =[];
  62. if (empty($rules)) {
  63. //读取验证规则
  64. $rules = $this->rule;
  65. }
  66. if (empty($message)) {
  67. $message = $this->message;
  68. }
  69. //读取场景
  70. if (!$this->getScene($scene)) {
  71. return false;
  72. }
  73. //如果场景需要验证的规则不为空
  74. if (!empty($this->only)) {
  75. $new_rules = [];
  76. foreach ($this->only as $key => $value) {
  77. if (array_key_exists($value,$rules)) {
  78. $new_rules[$value] = $rules[$value];
  79. }
  80. }
  81. $rules = $new_rules;
  82. }
  83. // var_dump($rules);die;
  84. $validator = Validator::make($data,$rules,$message);
  85. //验证失败
  86. if ($validator->fails()) {
  87. $this->error = $validator->errors()->first();
  88. return false;
  89. }
  90. return !empty($this->error) ? false : true;
  91. }
  92. /**
  93. * 获取数据验证的场景
  94. * @access protected
  95. * @param string $scene 验证场景
  96. * @return void
  97. */
  98. protected function getScene($scene = '')
  99. {
  100. if (empty($scene)) {
  101. // 读取指定场景
  102. $scene = $this->currentScene;
  103. }
  104. $this->only = [];
  105. if (empty($scene)) {
  106. return true;
  107. }
  108. if (!isset($this->scene[$scene])) {
  109. //指定场景未找到写入error
  110. $this->error = "scene:".$scene.'is not found';
  111. return false;
  112. }
  113. // 如果设置了验证适用场景
  114. $scene = $this->scene[$scene];
  115. if (is_string($scene)) {
  116. $scene = explode(',', $scene);
  117. }
  118. //将场景需要验证的字段填充入only
  119. $this->only = $scene;
  120. return true;
  121. }
  122. // 获取错误信息
  123. public function getError()
  124. {
  125. return $this->error;
  126. }
  127. }