This commit is contained in:
2023-12-12 15:35:34 +08:00
parent 26030fd148
commit d860269a7c
7 changed files with 217 additions and 218 deletions
+116 -116
View File
@@ -14,150 +14,150 @@ use Exception;
abstract class BaseValidator abstract class BaseValidator
{ {
public array $field = []; public array $field = [];
public array $rules = []; public array $rules = [];
public string $method; public string $method;
protected bool $isFail = TRUE; protected bool $isFail = TRUE;
protected string $message = ''; protected string $message = '';
protected array $params = []; protected array $params = [];
protected ?Model $model = null; protected ?Model $model = null;
/** /**
* @param $model * @param $model
*/ */
public function setModel($model): void public function setModel($model): void
{ {
$this->model = $model; $this->model = $model;
} }
/** /**
* @return Model|null * @return Model|null
*/ */
public function getModel(): ?Model public function getModel(): ?Model
{ {
return $this->model; return $this->model;
} }
/** /**
* BaseValidator constructor. * BaseValidator constructor.
* @param array $config * @param array $config
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->regConfig($config); $this->regConfig($config);
} }
/** /**
* @param $config * @param $config
*/ */
private function regConfig($config): void private function regConfig($config): void
{ {
if (count($config) < 1) { if (count($config) < 1) {
return; return;
} }
foreach ($config as $key => $val) { foreach ($config as $key => $val) {
$this->$key = $val; $this->$key = $val;
} }
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function trigger(): bool public function trigger(): bool
{ {
throw new Exception('Child Class must define method of trigger'); throw new Exception('Child Class must define method of trigger');
} }
/** /**
* @return array * @return array
*/ */
protected function getParams(): array protected function getParams(): array
{ {
return $this->params; return $this->params;
} }
/** /**
* @param array $data * @param array $data
* @return $this * @return $this
*/ */
public function setParams(array $data): static public function setParams(array $data): static
{ {
$this->params = $data; $this->params = $data;
return $this; return $this;
} }
/** /**
* @param $field * @param $field
* @param $message * @param $message
* @return bool * @return bool
*/ */
public function addError($field, $message): bool public function addError($field, $message): bool
{ {
$this->isFail = FALSE; $this->isFail = FALSE;
if (!is_null($field)) { if (!is_null($field)) {
$message = str_replace(':attribute', $field, $message); $message = str_replace(':attribute', $field, $message);
} }
\trigger_print_error($message,"mysql"); \trigger_print_error($message, "mysql");
$this->message = $message; $this->message = $message;
return $this->isFail; return $this->isFail;
} }
/** /**
* @param string|array $fields * @param string|array $fields
* @param callable $callback * @param callable $callback
* @param ...$params * @param ...$params
* @return bool * @return bool
*/ */
protected function _validator(string|array $fields, callable $callback, ...$params): bool protected function _validator(string|array $fields, callable $callback, ...$params): bool
{ {
if (is_string($fields)) { if (is_string($fields)) {
$fields = [$fields]; $fields = [$fields];
} }
foreach ($fields as $field) { foreach ($fields as $field) {
if (!$callback($field, ...$params)) { if (!$callback($field, ...$params)) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* @return string * @return string
*/ */
public function getError(): string public function getError(): string
{ {
return $this->message; return $this->message;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
* @throws Exception * @throws
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
$method = 'set' . ucfirst($name); $method = 'set' . ucfirst($name);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->$method($value); $this->$method($value);
} else if (property_exists($this, $name)) { } else if (property_exists($this, $name)) {
$this->$name = $value; $this->$name = $value;
} else { } else {
throw new Exception('unknown property ' . $name . ' in class ' . static::class); throw new Exception('unknown property ' . $name . ' in class ' . static::class);
} }
} }
} }
+71 -71
View File
@@ -13,31 +13,31 @@ namespace validator;
class DateTimeValidator extends BaseValidator class DateTimeValidator extends BaseValidator
{ {
const DATE = 'date'; const string DATE = 'date';
const DATE_TIME = 'datetime'; const string DATE_TIME = 'datetime';
const TIME = 'time'; const string TIME = 'time';
const STR_TO_TIME = 'timestamp'; const string STR_TO_TIME = 'timestamp';
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) { return $this->_validator($this->field, function ($field, $params, $method) {
$value = $params[$field] ?? null; $value = $params[$field] ?? null;
return match ($method) { return match ($method) {
self::DATE => $this->validatorDate($field, $value), self::DATE => $this->validatorDate($field, $value),
self::DATE_TIME => $this->validateDatetime($field, $value), self::DATE_TIME => $this->validateDatetime($field, $value),
self::TIME => $this->validatorTime($field, $value), self::TIME => $this->validatorTime($field, $value),
self::STR_TO_TIME => $this->validatorTimestamp($field, $value), self::STR_TO_TIME => $this->validatorTimestamp($field, $value),
default => true, default => true,
}; };
}, $this->params, strtolower($this->method)); }, $this->params, strtolower($this->method));
} }
/** /**
* @param $field * @param $field
@@ -46,18 +46,18 @@ class DateTimeValidator extends BaseValidator
* *
* 效验分秒 格式如 01:02 or 01-02 * 效验分秒 格式如 01:02 or 01-02
*/ */
public function validatorTime($field, $value): bool public function validatorTime($field, $value): bool
{ {
if (!is_string($value)) { if (!is_string($value)) {
return $this->addError($field, 'The param :attribute not is a date value'); return $this->addError($field, 'The param :attribute not is a date value');
} }
$match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result); $match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result);
if ($match && $result[0] == $value) { if ($match && $result[0] == $value) {
return true; return true;
} else { } else {
return $this->addError($field, 'The param :attribute format error'); return $this->addError($field, 'The param :attribute format error');
} }
} }
/** /**
@@ -67,19 +67,19 @@ class DateTimeValidator extends BaseValidator
* *
* 效验分秒 格式如 2017-12-22 01:02 * 效验分秒 格式如 2017-12-22 01:02
*/ */
public function validateDatetime($field, $value): bool public function validateDatetime($field, $value): bool
{ {
if (!is_string($value)) { if (!is_string($value)) {
return $this->addError($field, 'The param :attribute not is a date value'); return $this->addError($field, 'The param :attribute not is a date value');
} }
$match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/'; $match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/';
$match = preg_match($match, $value, $result); $match = preg_match($match, $value, $result);
if ($match && $result[0] == $value) { if ($match && $result[0] == $value) {
return true; return true;
} else { } else {
return $this->addError($field, 'The param :attribute format error'); return $this->addError($field, 'The param :attribute format error');
} }
} }
/** /**
* @param $field * @param $field
@@ -88,18 +88,18 @@ class DateTimeValidator extends BaseValidator
* *
* 效验分秒 格式如 2017-12-22 * 效验分秒 格式如 2017-12-22
*/ */
public function validatorDate($field, $value): bool public function validatorDate($field, $value): bool
{ {
if (!is_string($value)) { if (!is_string($value)) {
return $this->addError($field, 'The param :attribute not is a date value'); return $this->addError($field, 'The param :attribute not is a date value');
} }
$match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result); $match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result);
if ($match && $result[0] == $value) { if ($match && $result[0] == $value) {
return true; return true;
} else { } else {
return $this->addError($field, 'The param :attribute format error'); return $this->addError($field, 'The param :attribute format error');
} }
} }
/** /**
* @param $field * @param $field
@@ -108,17 +108,17 @@ class DateTimeValidator extends BaseValidator
* *
* 效验时间戳 格式如 1521452254 * 效验时间戳 格式如 1521452254
*/ */
public function validatorTimestamp($field, $value): bool public function validatorTimestamp($field, $value): bool
{ {
if (!is_numeric($value)) { if (!is_numeric($value)) {
return $this->addError($field, 'The param :attribute not is a timestamp value'); return $this->addError($field, 'The param :attribute not is a timestamp value');
} }
if (strlen((string)$value) != 10) { if (strlen((string)$value) != 10) {
return $this->addError($field, 'The param :attribute not is a timestamp value'); return $this->addError($field, 'The param :attribute not is a timestamp value');
} }
if (!date('YmdHis', $value)) { if (!date('YmdHis', $value)) {
return $this->addError($field, 'The param :attribute format error'); return $this->addError($field, 'The param :attribute format error');
} }
return true; return true;
} }
} }
+2 -2
View File
@@ -19,10 +19,10 @@ class EmptyValidator extends BaseValidator
{ {
/** @var string [不能为空] */ /** @var string [不能为空] */
const CAN_NOT_EMPTY = 'not empty'; const string CAN_NOT_EMPTY = 'not empty';
/** @var string [可为空, 不能为null] */ /** @var string [可为空, 不能为null] */
const CAN_NOT_NULL = 'not null'; const string CAN_NOT_NULL = 'not null';
public string $method; public string $method;
+2 -2
View File
@@ -13,8 +13,8 @@ namespace validator;
class LengthValidator extends BaseValidator class LengthValidator extends BaseValidator
{ {
const MAX_LENGTH = 'max'; const string MAX_LENGTH = 'max';
const MIN_LENGTH = 'min'; const string MIN_LENGTH = 'min';
public string $method; public string $method;
+15 -15
View File
@@ -14,23 +14,23 @@ class RoundValidator extends BaseValidator
{ {
public ?int $value = null; public ?int $value = null;
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function trigger(): bool public function trigger(): bool
{ {
return $this->_validator($this->field, function ($field, $model, $param) { return $this->_validator($this->field, function ($field, $model, $param) {
$value = $model->getAttribute($field); $value = $model->getAttribute($field);
if ($value == null || round($value, $param) != $value) { if ($value == null || round($value, $param) != $value) {
return $this->addError($field,'The param :attribute length error'); return $this->addError($field, 'The param :attribute length error');
} }
return true; return true;
}, $this->model, $this->value); }, $this->model, $this->value);
} }
} }
+5 -5
View File
@@ -14,11 +14,11 @@ class TypesOfValidator extends BaseValidator
{ {
const JSON = 'json'; const string JSON = 'json';
const FLOAT = 'float'; const string FLOAT = 'float';
const ARRAY = 'array'; const string ARRAY = 'array';
const STRING = 'string'; const string STRING = 'string';
const INTEGER = 'integer'; const string INTEGER = 'integer';
private ?int $min = null; private ?int $min = null;
private ?int $max = null; private ?int $max = null;
+6 -7
View File
@@ -7,7 +7,6 @@ namespace validator;
use Closure; use Closure;
use Database\ModelInterface; use Database\ModelInterface;
use Exception;
use Kiri; use Kiri;
/** /**
@@ -40,7 +39,7 @@ class Validator extends BaseValidator
* @param $field * @param $field
* @param $rules * @param $rules
* @return $this * @return $this
* @throws Exception * @throws
*/ */
public function make($field, $rules): static public function make($field, $rules): static
{ {
@@ -61,7 +60,7 @@ class Validator extends BaseValidator
* @param $rule * @param $rule
* @param $model * @param $model
* @param $param * @param $param
* @throws Exception * @throws
* ['maxLength'=>150, 'required', 'minLength' => 100] * ['maxLength'=>150, 'required', 'minLength' => 100]
*/ */
public function createRule($field, $rule, $model, $param): void public function createRule($field, $rule, $model, $param): void
@@ -69,7 +68,7 @@ class Validator extends BaseValidator
$define = ['field' => $field]; $define = ['field' => $field];
foreach ($rule as $key => $val) { foreach ($rule as $key => $val) {
if (is_string($key)) { if (is_string($key)) {
$type = strtolower($key); $type = strtolower($key);
$define['value'] = $val; $define['value'] = $val;
} else { } else {
$type = strtolower($val); $type = strtolower($val);
@@ -77,7 +76,7 @@ class Validator extends BaseValidator
if (!isset($this->classMap[$type])) { if (!isset($this->classMap[$type])) {
$this->validators[] = [$model, $val]; $this->validators[] = [$model, $val];
} else { } else {
$merge = array_merge($this->classMap[$type], $define, [ $merge = array_merge($this->classMap[$type], $define, [
'params' => $param, 'params' => $param,
'model' => $model 'model' => $model
]); ]);
@@ -88,7 +87,7 @@ class Validator extends BaseValidator
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function validation(): bool public function validation(): bool
{ {
@@ -113,7 +112,7 @@ class Validator extends BaseValidator
/** /**
* @param BaseValidator|array|Closure $val * @param BaseValidator|array|Closure $val
* @return array * @return array
* @throws Exception * @throws
*/ */
private function check(BaseValidator|array|Closure $val): array private function check(BaseValidator|array|Closure $val): array
{ {