Rule.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php declare(strict_types=1);
  2. /**
  3. * Created by PhpStorm.
  4. * User: Inhere
  5. * Date: 2018/1/8 0008
  6. * Time: 21:47
  7. */
  8. namespace Inhere\Validate\Validator;
  9. use Closure;
  10. /**
  11. * Class Rule
  12. *
  13. * @package Inhere\Validate\Validator
  14. * @TODO
  15. */
  16. final class Rule
  17. {
  18. /**
  19. * @var string
  20. */
  21. public string $field;
  22. /**
  23. * validator name OR validator object
  24. *
  25. * @var string|callable
  26. */
  27. public $validator;
  28. /**
  29. * @var array
  30. */
  31. public array $params = [];
  32. /**
  33. * @var Closure
  34. */
  35. public Closure $when;
  36. /**
  37. * @var mixed
  38. */
  39. public mixed $value;
  40. /**
  41. * @var mixed
  42. */
  43. public mixed $default;
  44. /**
  45. * default error message
  46. *
  47. * @var mixed
  48. */
  49. public mixed $message;
  50. /**
  51. * check Empty
  52. *
  53. * @var callable
  54. */
  55. public $isEmpty;
  56. /**
  57. * @var bool
  58. */
  59. public bool $skipOnEmpty = true;
  60. /**
  61. * @var array|null
  62. */
  63. public ?array $filters;
  64. public static function createByArray(array $config): void
  65. {
  66. }
  67. /**
  68. * @param string $field 属性名称
  69. * @param mixed $value 属性值
  70. * @param string|Closure $validator 验证器
  71. * @param array $params 验证需要的参数
  72. * @param string $message default error message
  73. * @param mixed $default default value
  74. *
  75. * @return Rule
  76. */
  77. public function init(string $field, mixed $value, string|Closure $validator, array $params, string $message, mixed $default): Rule
  78. {
  79. $this->field = $field;
  80. $this->value = $value;
  81. $this->validator = $validator;
  82. $this->params = $params;
  83. $this->message = $message;
  84. $this->default = $default;
  85. return $this;
  86. }
  87. }