qqq
This commit is contained in:
@@ -29,9 +29,6 @@ class ArrayValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
return $this->_validator($this->field, function ($field, $params) {
|
return $this->_validator($this->field, function ($field, $params) {
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (empty($value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!is_array($value)) {
|
if (!is_array($value)) {
|
||||||
return $this->addError($field, 'The param :attribute must a array');
|
return $this->addError($field, 'The param :attribute must a array');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,6 @@ class DateTimeValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
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;
|
||||||
if (empty($value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
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),
|
||||||
|
|||||||
+2
-9
@@ -16,23 +16,16 @@ class EmailValidator extends BaseValidator
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* 检查是否存在
|
* 检查是否存在
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
return $this->_validator($this->field, function ($field, $params) {
|
return $this->_validator($this->field, function ($field, $params) {
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (empty($value)) {
|
if (!filter_var($value,FILTER_VALIDATE_EMAIL)) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
|
|
||||||
if (!preg_match($exp, $value)) {
|
|
||||||
return $this->addError($field,'The param :attribute format error');
|
return $this->addError($field,'The param :attribute format error');
|
||||||
}
|
}
|
||||||
[$account, $domain] = explode("@", $value);
|
|
||||||
if (checkdnsrr($domain, "MX")) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return $this->addError($field,'The param :attribute format error');
|
|
||||||
}, $this->params);
|
}, $this->params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -33,13 +33,13 @@ class EmptyValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
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;
|
||||||
if (empty($value)) {
|
if ($value === null) {
|
||||||
return $this->addError($field,':attribute not exists');
|
return $this->addError($field, 'The :attribute can not null.');
|
||||||
}
|
}
|
||||||
return match ($method) {
|
if (empty($value)) {
|
||||||
self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError($field,'The :attribute can not empty.'),
|
return $this->addError($field, 'The :attribute can not empty.');
|
||||||
default => $value !== null || $this->addError($field,'The :attribute can not empty.')
|
}
|
||||||
};
|
return true;
|
||||||
}, $this->params, strtolower($this->method));
|
}, $this->params, strtolower($this->method));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-7
@@ -22,17 +22,12 @@ class EnumValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
return $this->_validator($this->field, function ($field, $params, $values) {
|
return $this->_validator($this->field, function ($field, $params, $values) {
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (is_null($value)) {
|
if (in_array($value, $values)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($value === '') {
|
|
||||||
return $this->addError($field, 'The param :attribute value con\'t empty.');
|
|
||||||
}
|
|
||||||
if (!in_array($value, $values)) {
|
|
||||||
$message = 'The param :attribute value(' . $value . ') only in ' . implode(',', $values);
|
$message = 'The param :attribute value(' . $value . ') only in ' . implode(',', $values);
|
||||||
return $this->addError($field, $message);
|
return $this->addError($field, $message);
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}, $this->params, $this->value);
|
}, $this->params, $this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,12 @@ class IntegerValidator extends BaseValidator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
return $this->_validator($this->field, function ($field, $params, $origin, $type) {
|
return $this->_validator($this->field, function ($field, $params, $origin, $type) {
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (empty($value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ($type !== self::MIN && $value < $origin) {
|
if ($type !== self::MIN && $value < $origin) {
|
||||||
return $this->addError($field,'The ' . $field . ' cannot be less than the default value.');
|
return $this->addError($field,'The ' . $field . ' cannot be less than the default value.');
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-7
@@ -10,6 +10,8 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
use ReflectionException;
|
||||||
|
|
||||||
class LengthValidator extends BaseValidator
|
class LengthValidator extends BaseValidator
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -22,18 +24,12 @@ class LengthValidator extends BaseValidator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
return $this->_validator($this->field, function ($field, $params, $method, $length) {
|
return $this->_validator($this->field, function ($field, $params, $method, $length) {
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (empty($value)) {
|
|
||||||
if ($method != self::MAX_LENGTH) {
|
|
||||||
return $this->addError($field, 'The param :attribute not exists');
|
|
||||||
} else {
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return match ($method) {
|
return match ($method) {
|
||||||
self::MAX_LENGTH => $this->maxLength($field, (string)$value),
|
self::MAX_LENGTH => $this->maxLength($field, (string)$value),
|
||||||
self::MIN_LENGTH => $this->minLength($field, (string)$value),
|
self::MIN_LENGTH => $this->minLength($field, (string)$value),
|
||||||
@@ -48,6 +44,7 @@ class LengthValidator extends BaseValidator
|
|||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
* 效验长度是否大于最大长度
|
* 效验长度是否大于最大长度
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function maxLength($field, $value): bool
|
private function maxLength($field, $value): bool
|
||||||
{
|
{
|
||||||
@@ -72,6 +69,7 @@ class LengthValidator extends BaseValidator
|
|||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
* 效验长度是否小于最小长度
|
* 效验长度是否小于最小长度
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function minLength($field, $value): bool
|
private function minLength($field, $value): bool
|
||||||
{
|
{
|
||||||
@@ -96,6 +94,7 @@ class LengthValidator extends BaseValidator
|
|||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
* 效验长度是否小于最小长度
|
* 效验长度是否小于最小长度
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function defaultLength($field, $value): bool
|
private function defaultLength($field, $value): bool
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class RequiredValidator extends BaseValidator
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* 检查是否存在
|
* 检查是否存在
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
trait RuleTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $classMap = [
|
||||||
|
'not empty' => [
|
||||||
|
'class' => EmptyValidator::class,
|
||||||
|
'method' => EmptyValidator::CAN_NOT_EMPTY,
|
||||||
|
],
|
||||||
|
'not null' => [
|
||||||
|
'class' => EmptyValidator::class,
|
||||||
|
'method' => EmptyValidator::CAN_NOT_NULL,
|
||||||
|
],
|
||||||
|
'required' => [
|
||||||
|
'class' => RequiredValidator::class,
|
||||||
|
],
|
||||||
|
'enum' => [
|
||||||
|
'class' => EnumValidator::class,
|
||||||
|
],
|
||||||
|
'unique' => [
|
||||||
|
'class' => UniqueValidator::class,
|
||||||
|
],
|
||||||
|
'datetime' => [
|
||||||
|
'class' => DateTimeValidator::class,
|
||||||
|
'method' => DateTimeValidator::DATE_TIME,
|
||||||
|
],
|
||||||
|
'date' => [
|
||||||
|
'class' => DateTimeValidator::class,
|
||||||
|
'method' => DateTimeValidator::DATE,
|
||||||
|
],
|
||||||
|
'time' => [
|
||||||
|
'class' => DateTimeValidator::class,
|
||||||
|
'method' => DateTimeValidator::TIME,
|
||||||
|
],
|
||||||
|
'timestamp' => [
|
||||||
|
'class' => DateTimeValidator::class,
|
||||||
|
'method' => DateTimeValidator::STR_TO_TIME,
|
||||||
|
],
|
||||||
|
'string' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::STRING,
|
||||||
|
],
|
||||||
|
'int' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::INTEGER,
|
||||||
|
],
|
||||||
|
'min' => [
|
||||||
|
'class' => IntegerValidator::class
|
||||||
|
],
|
||||||
|
'max' => [
|
||||||
|
'class' => IntegerValidator::class
|
||||||
|
],
|
||||||
|
'json' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::JSON,
|
||||||
|
],
|
||||||
|
'float' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::FLOAT,
|
||||||
|
],
|
||||||
|
'array' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::ARRAY,
|
||||||
|
],
|
||||||
|
'serialize' => [
|
||||||
|
'class' => TypesOfValidator::class,
|
||||||
|
'method' => TypesOfValidator::SERIALIZE,
|
||||||
|
],
|
||||||
|
'maxlength' => [
|
||||||
|
'class' => LengthValidator::class,
|
||||||
|
'method' => 'max',
|
||||||
|
],
|
||||||
|
'minlength' => [
|
||||||
|
'class' => LengthValidator::class,
|
||||||
|
'method' => 'min',
|
||||||
|
],
|
||||||
|
'email' => [
|
||||||
|
'class' => EmailValidator::class,
|
||||||
|
'method' => 'email',
|
||||||
|
],
|
||||||
|
'length' => [
|
||||||
|
'class' => LengthValidator::class,
|
||||||
|
'method' => 'default',
|
||||||
|
],
|
||||||
|
'round' => [
|
||||||
|
'class' => RoundValidator::class,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
+9
-15
@@ -48,9 +48,7 @@ class TypesOfValidator extends BaseValidator
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$value = $params[$field] ?? null;
|
$value = $params[$field] ?? null;
|
||||||
if (is_null($value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return $this->{$method . 'Format'}($field, $value);
|
return $this->{$method . 'Format'}($field, $value);
|
||||||
}, $this->params, $this->method, $this->types);
|
}, $this->params, $this->method, $this->types);
|
||||||
}
|
}
|
||||||
@@ -59,12 +57,10 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @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_string($value) || is_numeric($value)) {
|
|
||||||
return $this->addError($field, 'The ' . $field . ' not is JSON data.');
|
|
||||||
}
|
|
||||||
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.');
|
||||||
}
|
}
|
||||||
@@ -75,13 +71,11 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function serializeFormat($field, $value): bool
|
public function serializeFormat($field, $value): bool
|
||||||
{
|
{
|
||||||
if (!is_string($value) || is_numeric($value)) {
|
if (false === unserialize($value)) {
|
||||||
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
|
|
||||||
}
|
|
||||||
if (false === swoole_unserialize($value)) {
|
|
||||||
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
|
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -91,6 +85,7 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function arrayFormat($field, $value): bool
|
public function arrayFormat($field, $value): bool
|
||||||
{
|
{
|
||||||
@@ -104,10 +99,11 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function stringFormat($field, $value): bool
|
public function stringFormat($field, $value): bool
|
||||||
{
|
{
|
||||||
if (is_array($value) || is_object($value) || is_bool($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;
|
||||||
@@ -117,16 +113,13 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @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 (!is_numeric($value)) {
|
|
||||||
return $this->addError($field, 'The ' . $field . ' not is number data.');
|
|
||||||
}
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,6 +127,7 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @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
|
||||||
{
|
{
|
||||||
|
|||||||
+3
-103
@@ -17,108 +17,11 @@ use Kiri;
|
|||||||
class Validator extends BaseValidator
|
class Validator extends BaseValidator
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use RuleTrait;
|
||||||
|
|
||||||
/** @var BaseValidator[] */
|
/** @var BaseValidator[] */
|
||||||
private ?array $validators = [];
|
private ?array $validators = [];
|
||||||
|
|
||||||
/** @var ?Validator */
|
|
||||||
private static ?Validator $instance = null;
|
|
||||||
|
|
||||||
protected array $classMap = [
|
|
||||||
'not empty' => [
|
|
||||||
'class' => 'validator\EmptyValidator',
|
|
||||||
'method' => EmptyValidator::CAN_NOT_EMPTY,
|
|
||||||
],
|
|
||||||
'not null' => [
|
|
||||||
'class' => 'validator\EmptyValidator',
|
|
||||||
'method' => EmptyValidator::CAN_NOT_NULL,
|
|
||||||
],
|
|
||||||
'required' => [
|
|
||||||
'class' => 'validator\RequiredValidator',
|
|
||||||
],
|
|
||||||
'enum' => [
|
|
||||||
'class' => 'validator\EnumValidator',
|
|
||||||
],
|
|
||||||
'unique' => [
|
|
||||||
'class' => 'validator\UniqueValidator',
|
|
||||||
],
|
|
||||||
'datetime' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::DATE_TIME,
|
|
||||||
],
|
|
||||||
'date' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::DATE,
|
|
||||||
],
|
|
||||||
'time' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::TIME,
|
|
||||||
],
|
|
||||||
'timestamp' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::STR_TO_TIME,
|
|
||||||
],
|
|
||||||
'string' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::STRING,
|
|
||||||
],
|
|
||||||
'int' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::INTEGER,
|
|
||||||
],
|
|
||||||
'min' => [
|
|
||||||
'class' => IntegerValidator::class
|
|
||||||
],
|
|
||||||
'max' => [
|
|
||||||
'class' => IntegerValidator::class
|
|
||||||
],
|
|
||||||
'json' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::JSON,
|
|
||||||
],
|
|
||||||
'float' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::FLOAT,
|
|
||||||
],
|
|
||||||
'array' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::ARRAY,
|
|
||||||
],
|
|
||||||
'serialize' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::SERIALIZE,
|
|
||||||
],
|
|
||||||
'maxlength' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'max',
|
|
||||||
],
|
|
||||||
'minlength' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'min',
|
|
||||||
],
|
|
||||||
'email' => [
|
|
||||||
'class' => 'validator\EmailValidator',
|
|
||||||
'method' => 'email',
|
|
||||||
],
|
|
||||||
'length' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'default',
|
|
||||||
],
|
|
||||||
'round' => [
|
|
||||||
'class' => 'validator\RoundValidator',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Validator|null
|
|
||||||
*/
|
|
||||||
public static function getInstance(): ?Validator
|
|
||||||
{
|
|
||||||
if (static::$instance == null) {
|
|
||||||
static::$instance = new Validator();
|
|
||||||
}
|
|
||||||
return static::$instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
@@ -127,10 +30,7 @@ class Validator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public static function instance(array $params, ModelInterface $model): static
|
public static function instance(array $params, ModelInterface $model): static
|
||||||
{
|
{
|
||||||
$validator = static::$instance;
|
$validator = new Validator();
|
||||||
if ($validator == null) {
|
|
||||||
$validator = static::$instance = new Validator();
|
|
||||||
}
|
|
||||||
$validator->setParams($params);
|
$validator->setParams($params);
|
||||||
$validator->setModel($model);
|
$validator->setModel($model);
|
||||||
return $validator;
|
return $validator;
|
||||||
|
|||||||
Reference in New Issue
Block a user