BaseRules.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\form\validate;
  12. use crmeb\form\CommonRule;
  13. /**
  14. * Class BaseRules
  15. * @package crmeb\form\validate
  16. * @method CommonRule required() 是否必填
  17. * @method CommonRule message(string $message) 设置错误提示
  18. * @method CommonRule enum(array $enum) 枚举
  19. * @method CommonRule pattern(string $pattern) 正则表达式
  20. * @method CommonRule field($field, array $rule = []) 验证规则
  21. */
  22. abstract class BaseRules
  23. {
  24. /**
  25. * 是否初始化
  26. * @var bool
  27. */
  28. protected static $init = false;
  29. /**
  30. * @var CommonRule
  31. */
  32. protected static $rule;
  33. /**
  34. * @return mixed
  35. */
  36. public static function getType(): string
  37. {
  38. return '';
  39. }
  40. /**
  41. * 初始化
  42. */
  43. public static function init()
  44. {
  45. if (!self::$init) {
  46. self::$rule = new CommonRule();
  47. self::$rule->type(static::getType());
  48. self::$init = true;
  49. }
  50. }
  51. /**
  52. * @param $name
  53. * @param $arguments
  54. * @return mixed
  55. */
  56. public static function __callStatic($name, $arguments)
  57. {
  58. self::init();
  59. if (method_exists(self::$rule, $name)) {
  60. return self::$rule->{$name}(...$arguments);
  61. }
  62. throw new FormValidate(__CLASS__ . ' Method does not exist' . $name . '()');
  63. }
  64. }