Files
kiri-validator/IntegerValidator.php
T
2022-02-18 17:16:45 +08:00

42 lines
919 B
PHP

<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 18:44
*/
declare(strict_types=1);
namespace validator;
class IntegerValidator extends BaseValidator
{
const MIN = 'min';
const MAX = 'max';
public ?int $value = null;
private string $type = '';
/**
* @return bool
*/
public function trigger(): bool
{
return $this->_validator($this->field, function ($field, $params, $origin, $type) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true;
}
if ($type !== self::MIN && $value < $origin) {
return $this->addError('The ' . $field . ' cannot be less than the default value.');
}
if ($type !== self::MAX && $value > $origin) {
return $this->addError('The ' . $field . ' cannot be greater than the default value.');
}
return true;
}, $this->params, $this->value, $this->type);
}
}