Files
kiri-core/Validator/LengthValidator.php
T

116 lines
2.6 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 17:04
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace validator;
class LengthValidator extends BaseValidator
{
const MAX_LENGTH = 'max';
const MIN_LENGTH = 'min';
2020-10-30 10:38:31 +08:00
public string $method;
2020-08-31 12:38:32 +08:00
2020-10-29 18:17:25 +08:00
public int $value;
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])) {
if ($this->method != self::MAX_LENGTH) {
return $this->addError('The param :attribute not exists');
} else {
return TRUE;
}
}
$value = $param[$this->field];
if (is_null($value)) {
return $this->addError('The param :attribute is null');
}
2021-07-07 18:36:20 +08:00
return match (strtolower($this->method)) {
self::MAX_LENGTH => $this->maxLength($value),
self::MIN_LENGTH => $this->minLength($value),
default => $this->defaultLength($value),
};
2020-08-31 12:38:32 +08:00
}
/**
* @param $value
* @return bool
*
* 效验长度是否大于最大长度
*/
2020-12-17 14:09:14 +08:00
private function maxLength($value): bool
2020-08-31 12:38:32 +08:00
{
if (is_array($value)) {
if (count($value) > $value) {
return $this->addError('The param :attribute length overflow');
}
} else {
2021-04-07 01:53:32 +08:00
if (is_numeric($value) && strlen((string)$value) > $this->value) {
2020-08-31 12:38:32 +08:00
return $this->addError('The param :attribute length overflow');
}
if (strlen($value) > $this->value) {
return $this->addError('The param :attribute length overflow');
}
}
return TRUE;
}
/**
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
2020-12-17 14:09:14 +08:00
private function minLength($value): bool
2020-08-31 12:38:32 +08:00
{
if (is_array($value)) {
if (count($value) < $value) {
return $this->addError('The param :attribute length error');
}
} else {
2021-04-07 01:53:32 +08:00
if (is_numeric($value) && strlen((string)$value) < $this->value) {
2020-08-31 12:38:32 +08:00
return $this->addError('The param :attribute length overflow');
}
if (strlen($value) < $this->value) {
return $this->addError('The param :attribute length error');
}
}
return TRUE;
}
/**
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
2020-12-17 14:09:14 +08:00
private function defaultLength($value): bool
2020-08-31 12:38:32 +08:00
{
if (is_array($value)) {
if (count($value) !== $value) {
return $this->addError('The param :attribute length error');
}
} else {
2021-04-07 01:53:32 +08:00
if (is_numeric($value) && strlen((string)$value) !== $this->value) {
2020-08-31 12:38:32 +08:00
return $this->addError('The param :attribute length overflow');
}
if (mb_strlen($value) !== $this->value) {
return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value);
}
}
return TRUE;
}
}