Files
kiri-validator/EmptyValidator.php
T

51 lines
971 B
PHP
Raw Normal View History

2021-08-11 15:13:35 +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
{
/** @var string [不能为空] */
const CAN_NOT_EMPTY = 'not empty';
/** @var string [可为空, 不能为null] */
const CAN_NOT_NULL = 'not null';
public string $method;
/**
* @return bool
*
* 检查参数是否为NULL
*/
public function trigger(): bool
{
2022-01-04 16:53:31 +08:00
if (empty($this->params) || !isset($this->params[$this->field])) {
2021-08-11 15:13:35 +08:00
return $this->addError(':attribute not exists');
}
switch (strtolower($this->method)) {
case self::CAN_NOT_EMPTY:
2022-01-04 16:53:31 +08:00
if (strlen($this->params[$this->field]) < 1) {
2021-08-11 15:13:35 +08:00
return $this->addError('The :attribute can not empty.');
}
break;
case self::CAN_NOT_NULL:
2022-01-04 16:53:31 +08:00
if ($this->params[$this->field] === null) {
2021-08-11 15:13:35 +08:00
return $this->addError('The :attribute can not is null.');
}
break;
}
return true;
}
}