ValidationChecker.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Web;
  3. class ValidationChecker
  4. {
  5. protected $rules = [];
  6. protected $failClosure;
  7. /**
  8. * @return ValidationChecker
  9. */
  10. public static function make()
  11. {
  12. return new self();
  13. }
  14. /**
  15. * @param array $rules
  16. * @return $this
  17. */
  18. public function rules(array $rules)
  19. {
  20. $this->rules = $rules;
  21. return $this;
  22. }
  23. /**
  24. * @param callable $closure
  25. * @return $this
  26. */
  27. public function onFail(callable $closure)
  28. {
  29. $this->failClosure = $closure;
  30. return $this;
  31. }
  32. /**
  33. * @return bool
  34. */
  35. public function fails()
  36. {
  37. foreach ($this->rules as $rule => $condition) {
  38. if (!$condition) {
  39. if (is_callable($this->failClosure)) {
  40. ($this->failClosure)($rule);
  41. }
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * @param string $key
  49. * @return bool
  50. */
  51. public function removeRule(string $key)
  52. {
  53. $condition = $this->rules[$key];
  54. unset($this->rules[$key]);
  55. return $condition;
  56. }
  57. }