This commit is contained in:
2023-09-12 20:52:18 +08:00
parent 80f9bfc443
commit 0547948b3b
+71 -87
View File
@@ -14,128 +14,112 @@ class TypesOfValidator extends BaseValidator
{ {
const JSON = 'json'; const JSON = 'json';
const FLOAT = 'float'; const FLOAT = 'float';
const ARRAY = 'array'; const ARRAY = 'array';
const STRING = 'string'; const STRING = 'string';
const INTEGER = 'integer'; const INTEGER = 'integer';
const SERIALIZE = 'serialize';
private ?int $min = null; private ?int $min = null;
private ?int $max = null; private ?int $max = null;
/** @var array */ /** @var array */
public array $types = [ public array $types = [
self::JSON => 'json', self::JSON => 'json',
self::FLOAT => 'float', self::FLOAT => 'float',
self::ARRAY => 'array', self::ARRAY => 'array',
self::STRING => 'string', self::STRING => 'string',
self::INTEGER => 'integer', self::INTEGER => 'integer'
self::SERIALIZE => 'serialize', ];
];
/** @var string */ /** @var string */
public string $method; public string $method;
/** /**
* @return bool * @return bool
*/ */
public function trigger(): bool public function trigger(): bool
{ {
return $this->_validator($this->field, function ($field, $params, $method, $types) { return $this->_validator($this->field, function ($field, $params, $method, $types) {
if (!in_array($method, $types)) { if (!in_array($method, $types)) {
return true; return true;
} }
$value = $params[$field] ?? null; $value = $params[$field] ?? null;
return match ($method) {
return $this->{$method . 'Format'}($field, $value); self::INTEGER => $this->integerFormat($field, $value),
}, $this->params, $this->method, $this->types); self::FLOAT => $this->floatFormat($field, $value),
} self::JSON => $this->jsonFormat($field, $value),
self::STRING => $this->stringFormat($field, $value),
self::ARRAY => $this->arrayFormat($field, $value),
};
}, $this->params, $this->method, $this->types);
}
/** /**
* @param $field * @param $field
* @param $value * @param $value
* @return bool * @return bool
* @throws \ReflectionException
*/ */
public function jsonFormat($field, $value): bool public function jsonFormat($field, $value): bool
{ {
if (is_null(json_decode($value))) { if (is_null(json_decode($value))) {
return $this->addError($field, 'The ' . $field . ' not is JSON data.'); return $this->addError($field, 'The ' . $field . ' not is JSON data.');
} }
return true; return true;
} }
/** /**
* @param $field * @param $field
* @param $value * @param $value
* @return bool * @return bool
* @throws \ReflectionException
*/ */
public function serializeFormat($field, $value): bool public function arrayFormat($field, $value): bool
{ {
if (false === unserialize($value)) { if (!is_array($value)) {
return $this->addError($field, 'The ' . $field . ' not is serialize data.'); return $this->addError($field, 'The ' . $field . ' not is array data.');
} }
return true; return true;
} }
/** /**
* @param $field * @param $field
* @param $value * @param $value
* @return bool * @return bool
* @throws \ReflectionException
*/ */
public function arrayFormat($field, $value): bool public function stringFormat($field, $value): bool
{ {
if (!is_array($value)) {
return $this->addError($field, 'The ' . $field . ' not is array data.');
}
return true;
}
/**
* @param $field
* @param $value
* @return bool
* @throws \ReflectionException
*/
public function stringFormat($field, $value): bool
{
if (!is_string($value)) { if (!is_string($value)) {
return $this->addError($field, 'The ' . $field . ' not is string data.'); return $this->addError($field, 'The ' . $field . ' not is string data.');
} }
return true; return true;
} }
/** /**
* @param $field * @param $field
* @param $value * @param $value
* @return bool * @return bool
* @throws \ReflectionException
*/ */
public function integerFormat($field, $value): bool public function integerFormat($field, $value): bool
{ {
if ((int)$value != $value) { if ((int)$value != $value) {
return $this->addError($field, 'The ' . $field . ' not is number data.'); return $this->addError($field, 'The ' . $field . ' not is number data.');
} }
return true; return true;
} }
/** /**
* @param $field * @param $field
* @param $value * @param $value
* @return bool * @return bool
* @throws \ReflectionException
*/ */
public function floatFormat($field, $value): bool public function floatFormat($field, $value): bool
{ {
$trim = (float)$value; $trim = (float)$value;
if ($trim != $value) { if ($trim != $value) {
return $this->addError($field, 'The ' . $field . ' not is float data.'); return $this->addError($field, 'The ' . $field . ' not is float data.');
} }
return true; return true;
} }
} }