# 规则验证器

## 使用方法

### Required验证器
 - 基础使用
```php
    [
        // 检查 "username" 与 "password" 是否为空.
        // null, '', [] 无法通过验证
        [['username', 'password'], 'required'],
    ]
```
 - 使用严格模式
```php
    [
        // null, 无法通过验证.
        [['username', 'password'], 'required', 'strict' => true],
    ]
```
- 使用自定义required值
```php
    [
        // 0， undefined, false, 无法通过验证.
        [['username', 'password'], 'required', 'requiredValue' => [0, 'undefined', false]],
    ]
```
### String验证器
多规则联合
- 验证规则:POST参数, `name`, `age`不能为空并且必须为数字, `age`大于0, `name`大于1个字符

```php
    use Application\Service\Extension\Validator\ValidatorApplication;
    use Laminas\Validator\NotEmpty;
    use Laminas\Validator\Digits;
    use Laminas\Validator\GreaterThan;
    use Laminas\Validator\StringLength;
    
    $validator = new ValidatorApplication();
    $validator->attach(
        [['name', 'age'], 'class' => new NotEmpty(), 'message' => fn($attr) => "参数{$attr}不能为空。"]
        [['age'], 'class' => new Digits(), "message" => fn($attr) =>  "{$attr}必须为一个数字"],
        [['age'], 'class' => new GreaterThan(['min' => 0]), "message" fn($attr) => "{$attr}必须大于0"],
        [['name'], 'class' => new StringLength(['mix' => 1]), "message" fn($attr) => "{$attr}长度必须大于1"],
    );
    // 进行验证
    if (!$validator->isValid()) {
        echo current($validator->getErrors())
    }
```

使用自定义规则

- 验证规则:POST参数, `name`必须是一个数组并且数组数量必须大于5

```php
    use Application\Service\Extension\Validator\ValidatorApplication;
    
    $validator = new ValidatorApplication();
    $validator->attach(
        [['name', 'age'], 'class' => new NotEmpty(), 'message' => fn($attr) => "{$attr}参数不能为空。"]
    );
```

验证内置表单规则

```php
    use Application\Service\Extension\Validator\ValidatorApplication;
    use \Application\Service\Extension\Validator\FormValidator;
    
    $validator = new ValidatorApplication();
    $validator->attach(
        // 默认使用$_POST['formName']作为表单的key
        // 'test'会覆盖$_POST['formName']的值
        ['class' => new FormValidator('test')]
    );
    $validator->isValid();
```

获取验证器内的验证属性值

```php
    use Application\Service\Extension\Validator\ValidatorApplication;
    
    $validator = new ValidatorApplication();
    $validator->attach(
        [['name'], 'class' => static function ($attr, $value) {
            if (is_array($value) && count($value) > 5) {
                return true;
            }
            // 这里的错误会被全局的异常处理捕捉
            throw new \Laminas\Validator\Exception\InvalidArgumentException("参数name验证不通过");
        }]
    );

    var_dump($validator->attributes);
```
## 配置

 - 配置`attributesLabels`将错误提示`name不能为空`转成`姓名不能为空`

```php
    $validator = new \Application\Service\Extension\Validator\ValidatorApplication();
    $validator->setAttributesLabels([
        'name' => '姓名'
    ]);
    $validator->attach(
        [['name'], 'class' => new \Laminas\Validator\NotEmpty(), 'message' => fn($attr) => "参数{$attr}不能为空。"]
    );
    // 进行验证
    if (!$validator->isValid()) {
        echo current($validator->getErrors())
    }
```

 - 配置不同的请求参数
```php
    use \Application\Service\Extension\Validator\ValidatorApplication;
    
    // 默认使用POST数组当作数据源进行验证
    $validator = new ValidatorApplication();
    
    // 将对GET传递的数组当作数据源进行验证
    $validator = new ValidatorApplication($_GET);
```

## 一些常用验证类
 - 所有的验证类 https://docs.laminas.dev/laminas-validator/
```php
// 空数据验证类 https://docs.laminas.dev/laminas-validator/validators/not-empty/
\Laminas\Validator\NotEmpty::class

// between验证类 https://docs.laminas.dev/laminas-validator/validators/between/
\Laminas\Validator\Between::class

// 数据库数据是否存在验证类 https://docs.laminas.dev/laminas-validator/validators/db/
\Laminas\Validator\Db\RecordExists::class
\Laminas\Validator\Db\NoRecordExists::class

// 数字验证类 https://docs.laminas.dev/laminas-validator/validators/digits/
\Laminas\Validator\Digits::class

// 数字大于某个值验证类 https://docs.laminas.dev/laminas-validator/validators/greater-than/
\Laminas\Validator\GreaterThan::class

// 数字小于某个值验证类 https://docs.laminas.dev/laminas-validator/validators/less-than/
\Laminas\Validator\LessThan::class

// 正则表达式验证类 https://docs.laminas.dev/laminas-validator/validators/regex/
\Laminas\Validator\Regex::class

// 字符串长度验证类 https://docs.laminas.dev/laminas-validator/validators/string-length/
\Laminas\Validator\StringLength::class
```

## 一些说明

`$validator->getErrors()` 可能取不到值的问题
```php
    
    /**
    * 如果使用自定义验证规则, 会涉及到匿名函数, 对错误的响应不太好处理。
    * 所以直接抛出了 \Laminas\Validator\Exception\InvalidArgumentException(),
    * 由于全局会捕获 \Logic异常, 并将错误信息以json格式返回, 所以暂时采用了这种方法。
    * @see \Application\Service\Extension\ErrorHandler 异常处理器
    * @see \Laminas\Validator\Exception\InvalidArgumentException 抛出异常类
    */
    
```