Files
kiri-core/Validator/IntegerValidator.php
T

46 lines
928 B
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 18:44
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace validator;
class IntegerValidator extends BaseValidator
{
const MIN = 'min';
const MAX = 'max';
2020-10-29 18:17:25 +08:00
public ?int $value = null;
private string $type = '';
2020-08-31 12:38:32 +08:00
/**
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function trigger(): bool
2020-08-31 12:38:32 +08:00
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return true;
}
$value = $param[$this->field] ?? null;
if ($value === null) {
return $this->addError('The :attribute can not is null.');
}
if ($this->type !== self::MIN && $value < $this->value) {
return $this->addError('The ' . $this->field . ' cannot be less than the default value.');
}
if ($this->type !== self::MAX && $value > $this->value) {
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
}
return true;
}
}