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;
|
|
|
|
|
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
2022-01-09 14:01:11 +08:00
|
|
|
class LengthValidator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const MAX_LENGTH = 'max';
|
|
|
|
|
const MIN_LENGTH = 'min';
|
|
|
|
|
|
|
|
|
|
public string $method;
|
|
|
|
|
|
|
|
|
|
public int $value;
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
2022-01-09 14:01:11 +08:00
|
|
|
public function trigger(): bool
|
|
|
|
|
{
|
2023-04-21 23:09:35 +08:00
|
|
|
return $this->_validator($this->field, function ($field, $params, $method, $length) {
|
|
|
|
|
$value = $params[$field] ?? null;
|
2022-02-18 17:16:45 +08:00
|
|
|
return match ($method) {
|
2023-04-25 17:05:06 +08:00
|
|
|
self::MAX_LENGTH => $this->maxLength($field, (string)$value),
|
|
|
|
|
self::MIN_LENGTH => $this->minLength($field, (string)$value),
|
|
|
|
|
default => $this->defaultLength($field, (string)$value),
|
2022-02-18 17:16:45 +08:00
|
|
|
};
|
|
|
|
|
}, $this->params, strtolower($this->method), $this->value);
|
2022-01-09 14:01:11 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|