Files
kiri-validator/LengthValidator.php
T

117 lines
2.8 KiB
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: 17:04
*/
declare(strict_types=1);
namespace validator;
class LengthValidator extends BaseValidator
{
const MAX_LENGTH = 'max';
const MIN_LENGTH = 'min';
public string $method;
public int $value;
/**
* @return bool
*/
public function trigger(): bool
{
2022-02-18 17:16:45 +08:00
return $this->_validator($this->field, function ($field, $params, $method, $value) {
$value = $params[$this->field] ?? null;
if (empty($value)) {
if ($method != self::MAX_LENGTH) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute not exists');
2022-02-18 17:16:45 +08:00
} else {
return TRUE;
}
2022-01-09 14:01:11 +08:00
}
2022-02-18 17:16:45 +08:00
return match ($method) {
2022-02-25 17:12:15 +08:00
self::MAX_LENGTH => $this->maxLength($field, $value),
self::MIN_LENGTH => $this->minLength($field, $value),
default => $this->defaultLength($field, $value),
2022-02-18 17:16:45 +08:00
};
}, $this->params, strtolower($this->method), $this->value);
2022-01-09 14:01:11 +08:00
}
/**
2022-02-27 15:29:23 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*
* 效验长度是否大于最大长度
*/
2022-02-25 17:12:15 +08:00
private function maxLength($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (is_array($value)) {
if (count($value) > $value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length overflow');
2022-01-09 14:01:11 +08:00
}
} else {
if (is_numeric($value) && strlen((string)$value) > $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length overflow');
2022-01-09 14:01:11 +08:00
}
if (strlen($value) > $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length overflow');
2022-01-09 14:01:11 +08:00
}
}
return TRUE;
}
/**
2022-02-27 15:29:23 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
2022-02-25 17:12:15 +08:00
private function minLength($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (is_array($value)) {
if (count($value) < $value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length error');
2022-01-09 14:01:11 +08:00
}
} else {
if (is_numeric($value) && strlen((string)$value) < $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length overflow');
2022-01-09 14:01:11 +08:00
}
if (strlen($value) < $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length error');
2022-01-09 14:01:11 +08:00
}
}
return TRUE;
}
/**
2022-02-27 15:29:23 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
2022-02-25 17:12:15 +08:00
private function defaultLength($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (is_array($value)) {
if (count($value) !== $value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length error');
2022-01-09 14:01:11 +08:00
}
} else {
if (is_numeric($value) && strlen((string)$value) !== $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length overflow');
2022-01-09 14:01:11 +08:00
}
if (mb_strlen($value) !== $this->value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value);
2022-01-09 14:01:11 +08:00
}
}
return TRUE;
}
}