2022-01-09 14:01:11 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/3 0003
|
|
|
|
|
* Time: 15:46
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace validator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EmptyValidator extends BaseValidator
|
|
|
|
|
{
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/** @var string [不能为空] */
|
|
|
|
|
const CAN_NOT_EMPTY = 'not empty';
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/** @var string [可为空, 不能为null] */
|
|
|
|
|
const CAN_NOT_NULL = 'not null';
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
public string $method;
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-05-09 10:11:21 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* 检查参数是否为NULL
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
2023-07-07 17:24:03 +08:00
|
|
|
public function trigger(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->_validator($this->field, function ($field, $params, $method) {
|
|
|
|
|
$value = $params[$field] ?? null;
|
|
|
|
|
if ($value === null) {
|
|
|
|
|
return $this->addError($field, 'The :attribute can not null.');
|
|
|
|
|
}
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
return $this->addError($field, 'The :attribute can not empty.');
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}, $this->params, strtolower($this->method));
|
|
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
}
|