modify plugin name

This commit is contained in:
2022-02-18 17:16:45 +08:00
parent 1acf3fb48b
commit ba410c90da
13 changed files with 182 additions and 178 deletions
+6 -36
View File
@@ -24,46 +24,16 @@ class ArrayValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params)) { return $this->_validator($this->field, function ($field, $params) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true; return true;
} }
if (!isset($this->params[$this->field])) { if (!is_array($value)) {
return true; return $this->addError('The param :attribute must a array');
}
if (!is_array($this->params[$this->field])) {
return $this->addError("The param :attribute must a array");
} }
return true; return true;
} }, $this->params);
/**
* @param $data
* @return array
*
* 转成数组
*/
private function toArray($data): array
{
if (is_numeric($data)) {
return [];
} else if (is_null(json_decode($data, true))) {
return [];
} elseif (is_object($data)) {
$data = get_object_vars($data);
}
$_tmp = [];
foreach ($data as $key => $val) {
if (is_object($val)) {
$_tmp[$key] = $this->toArray($val);
} else if (is_array($val)) {
$_tmp[$key] = $this->toArray($val);
} else {
$_tmp[$key] = $val;
}
}
return $_tmp;
} }
} }
+21
View File
@@ -110,6 +110,27 @@ abstract class BaseValidator
return $this->isFail; return $this->isFail;
} }
/**
* @param string|array $fields
* @param callable $callback
* @param ...$params
* @return bool
*/
protected function _validator(string|array $fields, callable $callback, ...$params): bool
{
if (is_string($fields)) {
$fields = [$fields];
}
foreach ($fields as $field) {
if (!$callback($field, ...$params)) {
return false;
}
}
return true;
}
/** /**
* @return string * @return string
*/ */
+15 -13
View File
@@ -25,19 +25,21 @@ class DateTimeValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params)) { return $this->_validator($this->field, function ($field, $params, $method) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true; return true;
} }
if (!isset($this->params[$this->field]) || empty($this->params[$this->field])) { return match ($method) {
return true; self::DATE => $this->validatorDate($value),
} self::DATE_TIME => $this->validateDatetime($value),
return match (strtolower($this->method)) { self::TIME => $this->validatorTime($value),
self::DATE => $this->validatorDate($this->params[$this->field]), self::STR_TO_TIME => $this->validatorTimestamp($value),
self::DATE_TIME => $this->validateDatetime($this->params[$this->field]),
self::TIME => $this->validatorTime($this->params[$this->field]),
self::STR_TO_TIME => $this->validatorTimestamp($this->params[$this->field]),
default => true, default => true,
}; };
}, $this->params, strtolower($this->method));
} }
/** /**
@@ -48,7 +50,7 @@ class DateTimeValidator extends BaseValidator
*/ */
public function validatorTime($value): bool public function validatorTime($value): bool
{ {
if (empty($value) || !is_string($value)) { if (!is_string($value)) {
return $this->addError('The param :attribute not is a date value'); return $this->addError('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);
@@ -68,7 +70,7 @@ class DateTimeValidator extends BaseValidator
*/ */
public function validateDatetime($value): bool public function validateDatetime($value): bool
{ {
if (empty($value) || !is_string($value)) { if (!is_string($value)) {
return $this->addError('The param :attribute not is a date value'); return $this->addError('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}$/';
@@ -88,7 +90,7 @@ class DateTimeValidator extends BaseValidator
*/ */
public function validatorDate($value): bool public function validatorDate($value): bool
{ {
if (empty($value) || !is_string($value)) { if (!is_string($value)) {
return $this->addError('The param :attribute not is a date value'); return $this->addError('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);
@@ -107,7 +109,7 @@ class DateTimeValidator extends BaseValidator
*/ */
public function validatorTimestamp($value): bool public function validatorTimestamp($value): bool
{ {
if (empty($value) || !is_numeric($value)) { if (!is_numeric($value)) {
return $this->addError('The param :attribute not is a timestamp value'); return $this->addError('The param :attribute not is a timestamp value');
} }
if (strlen((string)$value) != 10) { if (strlen((string)$value) != 10) {
+11 -4
View File
@@ -19,14 +19,21 @@ class EmailValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params) || !isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true; return true;
} }
if (preg_match('/^[a-zA-Z0-9]+([._]+)[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $this->params[$this->field])) { $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
return true; if (!preg_match($exp, $value)) {
} else {
return $this->addError('The param :attribute format error'); return $this->addError('The param :attribute format error');
} }
[$account, $domain] = explode("@", $value);
if (checkdnsrr($domain, "MX")) {
return true;
}
return $this->addError('The param :attribute format error');
}, $this->params);
} }
} }
+8 -14
View File
@@ -30,21 +30,15 @@ class EmptyValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params) || !isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params, $method) {
$value = $params[$field] ?? null;
if (empty($value)) {
return $this->addError(':attribute not exists'); return $this->addError(':attribute not exists');
} }
switch (strtolower($this->method)) { return match ($method) {
case self::CAN_NOT_EMPTY: self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError('The :attribute can not empty.'),
if (strlen($this->params[$this->field]) < 1) { default => $value !== null || $this->addError('The :attribute can not empty.')
return $this->addError('The :attribute can not empty.'); };
} }, $this->params, strtolower($this->method));
break;
case self::CAN_NOT_NULL:
if ($this->params[$this->field] === null) {
return $this->addError('The :attribute can not is null.');
}
break;
}
return true;
} }
} }
+6 -15
View File
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace validator; namespace validator;
/** /**
* Class EnumValidator * Class EnumValidator
* @package validator * @package validator
@@ -20,24 +19,16 @@ class EnumValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params) || !isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params, $values) {
$value = $params[$field] ?? null;
if (empty($value)) {
return $this->addError('The param :attribute is null'); return $this->addError('The param :attribute is null');
} }
if (is_null($this->params[$this->field])) { if (!in_array($value, $values)) {
return $this->addError('The param :attribute is null'); return $this->addError('The param :attribute value only in ' . implode(',', $values));
}
if (!in_array($this->params[$this->field], $this->value)) {
return $this->addError($this->i());
} }
return true; return true;
} }, $this->params, $this->value);
/**
* @return string
*/
private function i(): string
{
return 'The param :attribute value only in ' . implode(',', $this->value);
} }
} }
+8 -5
View File
@@ -24,15 +24,18 @@ class IntegerValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params) || !isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params, $origin, $type) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true; return true;
} }
if ($this->type !== self::MIN && $this->params[$this->field] < $this->value) { if ($type !== self::MIN && $value < $origin) {
return $this->addError('The ' . $this->field . ' cannot be less than the default value.'); return $this->addError('The ' . $field . ' cannot be less than the default value.');
} }
if ($this->type !== self::MAX && $this->params[$this->field] > $this->value) { if ($type !== self::MAX && $value > $origin) {
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.'); return $this->addError('The ' . $field . ' cannot be greater than the default value.');
} }
return true; return true;
}, $this->params, $this->value, $this->type);
} }
} }
+9 -6
View File
@@ -25,18 +25,21 @@ class LengthValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (empty($this->params) || !isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params, $method, $value) {
if ($this->method != self::MAX_LENGTH) { $value = $params[$this->field] ?? null;
if (empty($value)) {
if ($method != self::MAX_LENGTH) {
return $this->addError('The param :attribute not exists'); return $this->addError('The param :attribute not exists');
} else { } else {
return TRUE; return TRUE;
} }
} }
return match (strtolower($this->method)) { return match ($method) {
self::MAX_LENGTH => $this->maxLength($this->params[$this->field]), self::MAX_LENGTH => $this->maxLength($value),
self::MIN_LENGTH => $this->minLength($this->params[$this->field]), self::MIN_LENGTH => $this->minLength($value),
default => $this->defaultLength($this->params[$this->field]), default => $this->defaultLength($value),
}; };
}, $this->params, strtolower($this->method), $this->value);
} }
/** /**
+3 -1
View File
@@ -19,11 +19,13 @@ class RequiredValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (!isset($this->params[$this->field])) { return $this->_validator($this->field, function ($field, $params) {
if (!isset($params[$field])) {
return $this->addError('The param :attribute not exists'); return $this->addError('The param :attribute not exists');
} else { } else {
return true; return true;
} }
});
} }
} }
+4 -2
View File
@@ -23,11 +23,13 @@ class RoundValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
$value = $this->model->getAttribute($this->field); return $this->_validator($this->field, function ($field, $model, $param) {
if ($value == null || round($value, $this->value) != $value) { $value = $model->getAttribute($field);
if ($value == null || round($value, $param) != $value) {
return $this->addError('The param :attribute length error'); return $this->addError('The param :attribute length error');
} }
return true; return true;
}, $this->model, $this->value);
} }
+30 -21
View File
@@ -43,97 +43,106 @@ class TypesOfValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
if (!in_array($this->method, $this->types)) { return $this->_validator($this->field, function ($field, $params, $method, $types) {
if (!in_array($method, $types)) {
return true; return true;
} }
if (empty($this->params) || !isset($this->params[$this->field])) { if (!isset($params[$field])) {
return true; return true;
} }
if ($this->params[$this->field] === null) { $value = $params[$field] ?? null;
return $this->addError('This ' . $this->field . ' is not an empty data.'); if (empty($value)) {
return $this->addError('This ' . $field . ' is not an empty data.');
} }
return $this->{$this->method . 'Format'}($this->params[$this->field]); return $this->{$method . 'Format'}($field, $value);
}, $this->params, $this->method, $this->types);
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function jsonFormat($value): bool public function jsonFormat($field, $value): bool
{ {
if (!is_string($value) || is_numeric($value)) { if (!is_string($value) || is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is JSON data.'); return $this->addError('The ' . $field . ' not is JSON data.');
} }
if (is_null(json_decode($value))) { if (is_null(json_decode($value))) {
return $this->addError('The ' . $this->field . ' not is JSON data.'); return $this->addError('The ' . $field . ' not is JSON data.');
} }
return true; return true;
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function serializeFormat($value): bool public function serializeFormat($field, $value): bool
{ {
if (!is_string($value) || is_numeric($value)) { if (!is_string($value) || is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.'); return $this->addError('The ' . $field . ' not is serialize data.');
} }
if (false === swoole_unserialize($value)) { if (false === swoole_unserialize($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.'); return $this->addError('The ' . $field . ' not is serialize data.');
} }
return true; return true;
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function arrayFormat($value): bool public function arrayFormat($field, $value): bool
{ {
if (!is_array($value)) { if (!is_array($value)) {
return $this->addError('The ' . $this->field . ' not is array data.'); return $this->addError('The ' . $field . ' not is array data.');
} }
return true; return true;
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function stringFormat($value): bool public function stringFormat($field, $value): bool
{ {
if (is_array($value) || is_object($value) || is_bool($value)) { if (is_array($value) || is_object($value) || is_bool($value)) {
return $this->addError('The ' . $this->field . ' not is string data.'); return $this->addError('The ' . $field . ' not is string data.');
} }
return true; return true;
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function integerFormat($value): bool public function integerFormat($field, $value): bool
{ {
if (!is_numeric($value)) { if (!is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is number data.'); return $this->addError('The ' . $field . ' not is number data.');
} }
if ((int)$value != $value) { if ((int)$value != $value) {
return $this->addError('The ' . $this->field . ' not is number data.'); return $this->addError('The ' . $field . ' not is number data.');
} }
return true; return true;
} }
/** /**
* @param $field
* @param $value * @param $value
* @return bool * @return bool
*/ */
public function floatFormat($value): bool public function floatFormat($field, $value): bool
{ {
$trim = (float)$value; $trim = (float)$value;
if ($trim != $value || !is_float($trim)) { if ($trim != $value) {
return $this->addError('The ' . $this->field . ' not is float data.'); return $this->addError('The ' . $field . ' not is float data.');
} }
return true; return true;
} }
+10 -9
View File
@@ -20,21 +20,22 @@ class UniqueValidator extends BaseValidator
*/ */
public function trigger(): bool public function trigger(): bool
{ {
$param = $this->getParams(); if (empty($model)) {
if (empty($param) || !isset($param[$this->field])) {
return TRUE;
}
if (empty($this->model)) {
return $this->addError('Model error.'); return $this->addError('Model error.');
} }
if (!$this->model->getIsNowExample()) { if (!$model->getIsNowExample()) {
return true; return true;
} }
if ($this->model::query()->where([$this->field => $param[$this->field]])->exists()) { return $this->_validator($this->field, function ($field, $params, $model) {
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!'); if (!isset($params[$field])) {
return true;
}
$param = $params[$field];
if ($model::query()->where([$field => $param])->exists()) {
return $this->addError('The :attribute \'' . $param . '\' is exists!');
} }
return $this->isFail = TRUE; return $this->isFail = TRUE;
}, $this->params, $this->model);
} }
+2 -3
View File
@@ -132,9 +132,8 @@ class Validator extends BaseValidator
$param = $this->getParams(); $param = $this->getParams();
$model = $this->getModel(); $model = $this->getModel();
foreach ($field as $val) {
$this->createRule($val, $rules, $model, $param); $this->createRule($field, $rules, $model, $param);
}
return $this; return $this;
} }