1. UML类图

TP6 ValidateRule类源码分析 - 图1

2. 方法详解

2.1 添加验证因子 addItem() 方法

  • 参数:

string $name 验证名称
mixed $rule 验证规则
string $msg 提示信息

  • 返回:self
  • 源码:如下 ```php protected function addItem(string $name, $rule = null, string $msg = ‘’)
    { if ($rule || 0 === $rule) {

    1. $this->rule[$name] = $rule;

    } else {

    1. $this->rule[] = $name;

    }

    $this->message[] = $msg;

    return $this; }

  1. <a name="fYmjk"></a>
  2. #### 2.2 获取验证规则 `getRule()` 方法
  3. - **参数:**无<br />
  4. - **返回:**`array`
  5. - **源码:**如下
  6. ```php
  7. public function getRule(): array
  8. {
  9. return $this->rule;
  10. }

2.3 获取验证字段名称(描述) getTitle() 方法

  • 参数:
  • 返回:string
  • 源码:如下

    1. public function getTitle(): string
    2. {
    3. return $this->title ?: '';
    4. }

    2.4 获取验证提示 getMsg() 方法

  • 参数:

  • 返回:array
  • 源码:如下

    1. public function getMsg(): array
    2. {
    3. return $this->message;
    4. }

    2.5 设置验证字段名称 title() 方法

  • 参数:

    • string $title 验证字段名称
  • 返回:self
  • 源码:如下

    1. public function title(string $title)
    2. {
    3. $this->title = $title;
    4. return $this;
    5. }

    2.6 魔术方法 __call()

    源码:如下

    1. public function __call($method, $args)
    2. {
    3. if ('is' == strtolower(substr($method, 0, 2))) {
    4. $method = substr($method, 2);
    5. }
    6. array_unshift($args, lcfirst($method));
    7. return call_user_func_array([$this, 'addItem'], $args);
    8. }

    2.7 魔术方法 __callStatic

    源码:如下:w

    1. public static function __callStatic($method, $args)
    2. {
    3. $rule = new static();
    4. if ('is' == strtolower(substr($method, 0, 2))) {
    5. $method = substr($method, 2);
    6. }
    7. array_unshift($args, lcfirst($method));
    8. return call_user_func_array([$rule, 'addItem'], $args);
    9. }