Files
kiri-validator/EmptyValidator.php
T

45 lines
994 B
PHP
Raw Normal View History

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
2022-01-09 14:01:11 +08:00
/** @var string [不能为空] */
const CAN_NOT_EMPTY = 'not empty';
2022-02-18 17:16:45 +08:00
2022-01-09 14:01:11 +08:00
/** @var string [可为空, 不能为null] */
const CAN_NOT_NULL = 'not null';
public string $method;
2022-02-18 17:16:45 +08:00
2022-01-09 14:01:11 +08:00
/**
* @return bool
*
* 检查参数是否为NULL
*/
public function trigger(): bool
{
2022-02-18 17:16:45 +08:00
return $this->_validator($this->field, function ($field, $params, $method) {
$value = $params[$field] ?? null;
if (empty($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field,':attribute not exists');
2022-02-18 17:16:45 +08:00
}
return match ($method) {
2022-02-25 17:12:15 +08:00
self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError($field,'The :attribute can not empty.'),
default => $value !== null || $this->addError($field,'The :attribute can not empty.')
2022-02-18 17:16:45 +08:00
};
}, $this->params, strtolower($this->method));
2022-01-09 14:01:11 +08:00
}
}