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
{
if (empty($this->params)) {
return $this->_validator($this->field, function ($field, $params) {
$value = $params[$field] ?? null;
if (empty($value)) {
return true;
}
if (!isset($this->params[$this->field])) {
return true;
}
if (!is_array($this->params[$this->field])) {
return $this->addError("The param :attribute must a array");
if (!is_array($value)) {
return $this->addError('The param :attribute must a array');
}
return true;
}
/**
* @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;
}, $this->params);
}
}
+21
View File
@@ -110,6 +110,27 @@ abstract class BaseValidator
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
*/
+15 -13
View File
@@ -25,19 +25,21 @@ class DateTimeValidator extends BaseValidator
*/
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;
}
if (!isset($this->params[$this->field]) || empty($this->params[$this->field])) {
return true;
}
return match (strtolower($this->method)) {
self::DATE => $this->validatorDate($this->params[$this->field]),
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]),
return match ($method) {
self::DATE => $this->validatorDate($value),
self::DATE_TIME => $this->validateDatetime($value),
self::TIME => $this->validatorTime($value),
self::STR_TO_TIME => $this->validatorTimestamp($value),
default => true,
};
}, $this->params, strtolower($this->method));
}
/**
@@ -48,7 +50,7 @@ class DateTimeValidator extends BaseValidator
*/
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');
}
$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
{
if (empty($value) || !is_string($value)) {
if (!is_string($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}$/';
@@ -88,7 +90,7 @@ class DateTimeValidator extends BaseValidator
*/
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');
}
$match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result);
@@ -107,7 +109,7 @@ class DateTimeValidator extends BaseValidator
*/
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');
}
if (strlen((string)$value) != 10) {
+11 -4
View File
@@ -19,14 +19,21 @@ class EmailValidator extends BaseValidator
*/
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;
}
if (preg_match('/^[a-zA-Z0-9]+([._]+)[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $this->params[$this->field])) {
return true;
} else {
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
if (!preg_match($exp, $value)) {
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
{
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');
}
switch (strtolower($this->method)) {
case self::CAN_NOT_EMPTY:
if (strlen($this->params[$this->field]) < 1) {
return $this->addError('The :attribute can not empty.');
}
break;
case self::CAN_NOT_NULL:
if ($this->params[$this->field] === null) {
return $this->addError('The :attribute can not is null.');
}
break;
}
return true;
return match ($method) {
self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError('The :attribute can not empty.'),
default => $value !== null || $this->addError('The :attribute can not empty.')
};
}, $this->params, strtolower($this->method));
}
}
+6 -15
View File
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace validator;
/**
* Class EnumValidator
* @package validator
@@ -20,24 +19,16 @@ class EnumValidator extends BaseValidator
*/
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');
}
if (is_null($this->params[$this->field])) {
return $this->addError('The param :attribute is null');
}
if (!in_array($this->params[$this->field], $this->value)) {
return $this->addError($this->i());
if (!in_array($value, $values)) {
return $this->addError('The param :attribute value only in ' . implode(',', $values));
}
return true;
}
/**
* @return string
*/
private function i(): string
{
return 'The param :attribute value only in ' . implode(',', $this->value);
}, $this->params, $this->value);
}
}
+8 -5
View File
@@ -24,15 +24,18 @@ class IntegerValidator extends BaseValidator
*/
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;
}
if ($this->type !== self::MIN && $this->params[$this->field] < $this->value) {
return $this->addError('The ' . $this->field . ' cannot be less than the default value.');
if ($type !== self::MIN && $value < $origin) {
return $this->addError('The ' . $field . ' cannot be less than the default value.');
}
if ($this->type !== self::MAX && $this->params[$this->field] > $this->value) {
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
if ($type !== self::MAX && $value > $origin) {
return $this->addError('The ' . $field . ' cannot be greater than the default value.');
}
return true;
}, $this->params, $this->value, $this->type);
}
}
+9 -6
View File
@@ -25,18 +25,21 @@ class LengthValidator extends BaseValidator
*/
public function trigger(): bool
{
if (empty($this->params) || !isset($this->params[$this->field])) {
if ($this->method != self::MAX_LENGTH) {
return $this->_validator($this->field, function ($field, $params, $method, $value) {
$value = $params[$this->field] ?? null;
if (empty($value)) {
if ($method != self::MAX_LENGTH) {
return $this->addError('The param :attribute not exists');
} else {
return TRUE;
}
}
return match (strtolower($this->method)) {
self::MAX_LENGTH => $this->maxLength($this->params[$this->field]),
self::MIN_LENGTH => $this->minLength($this->params[$this->field]),
default => $this->defaultLength($this->params[$this->field]),
return match ($method) {
self::MAX_LENGTH => $this->maxLength($value),
self::MIN_LENGTH => $this->minLength($value),
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
{
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');
} else {
return true;
}
});
}
}
+4 -2
View File
@@ -23,11 +23,13 @@ class RoundValidator extends BaseValidator
*/
public function trigger(): bool
{
$value = $this->model->getAttribute($this->field);
if ($value == null || round($value, $this->value) != $value) {
return $this->_validator($this->field, function ($field, $model, $param) {
$value = $model->getAttribute($field);
if ($value == null || round($value, $param) != $value) {
return $this->addError('The param :attribute length error');
}
return true;
}, $this->model, $this->value);
}
+30 -21
View File
@@ -43,97 +43,106 @@ class TypesOfValidator extends BaseValidator
*/
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;
}
if (empty($this->params) || !isset($this->params[$this->field])) {
if (!isset($params[$field])) {
return true;
}
if ($this->params[$this->field] === null) {
return $this->addError('This ' . $this->field . ' is not an empty data.');
$value = $params[$field] ?? null;
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
* @return bool
*/
public function jsonFormat($value): bool
public function jsonFormat($field, $value): bool
{
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))) {
return $this->addError('The ' . $this->field . ' not is JSON data.');
return $this->addError('The ' . $field . ' not is JSON data.');
}
return true;
}
/**
* @param $field
* @param $value
* @return bool
*/
public function serializeFormat($value): bool
public function serializeFormat($field, $value): bool
{
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)) {
return $this->addError('The ' . $this->field . ' not is serialize data.');
return $this->addError('The ' . $field . ' not is serialize data.');
}
return true;
}
/**
* @param $field
* @param $value
* @return bool
*/
public function arrayFormat($value): bool
public function arrayFormat($field, $value): bool
{
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;
}
/**
* @param $field
* @param $value
* @return bool
*/
public function stringFormat($value): bool
public function stringFormat($field, $value): bool
{
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;
}
/**
* @param $field
* @param $value
* @return bool
*/
public function integerFormat($value): bool
public function integerFormat($field, $value): bool
{
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) {
return $this->addError('The ' . $this->field . ' not is number data.');
return $this->addError('The ' . $field . ' not is number data.');
}
return true;
}
/**
* @param $field
* @param $value
* @return bool
*/
public function floatFormat($value): bool
public function floatFormat($field, $value): bool
{
$trim = (float)$value;
if ($trim != $value || !is_float($trim)) {
return $this->addError('The ' . $this->field . ' not is float data.');
if ($trim != $value) {
return $this->addError('The ' . $field . ' not is float data.');
}
return true;
}
+10 -9
View File
@@ -20,21 +20,22 @@ class UniqueValidator extends BaseValidator
*/
public function trigger(): bool
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return TRUE;
}
if (empty($this->model)) {
if (empty($model)) {
return $this->addError('Model error.');
}
if (!$this->model->getIsNowExample()) {
if (!$model->getIsNowExample()) {
return true;
}
if ($this->model::query()->where([$this->field => $param[$this->field]])->exists()) {
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
return $this->_validator($this->field, function ($field, $params, $model) {
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;
}, $this->params, $this->model);
}
+2 -3
View File
@@ -132,9 +132,8 @@ class Validator extends BaseValidator
$param = $this->getParams();
$model = $this->getModel();
foreach ($field as $val) {
$this->createRule($val, $rules, $model, $param);
}
$this->createRule($field, $rules, $model, $param);
return $this;
}