Files
kiri-validator/IntegerValidator.php
T

40 lines
930 B
PHP
Raw Normal View History

2022-01-09 14:01:11 +08:00
<?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 = '';
2023-07-07 17:24:03 +08:00
/**
* @return bool
* @throws \ReflectionException
*/
2022-01-09 14:01:11 +08:00
public function trigger(): bool
{
2022-02-18 17:16:45 +08:00
return $this->_validator($this->field, function ($field, $params, $origin, $type) {
$value = $params[$field] ?? null;
if ($type !== self::MIN && $value < $origin) {
2022-02-25 17:12:15 +08:00
return $this->addError($field,'The ' . $field . ' cannot be less than the default value.');
2022-02-18 17:16:45 +08:00
}
if ($type !== self::MAX && $value > $origin) {
2022-02-25 17:12:15 +08:00
return $this->addError($field,'The ' . $field . ' cannot be greater than the default value.');
2022-02-18 17:16:45 +08:00
}
2022-01-09 14:01:11 +08:00
return true;
2022-02-18 17:16:45 +08:00
}, $this->params, $this->value, $this->type);
2022-01-09 14:01:11 +08:00
}
}