qqq
This commit is contained in:
@@ -29,9 +29,6 @@ class ArrayValidator extends BaseValidator
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
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) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
return match ($method) {
|
||||
self::DATE => $this->validatorDate($field, $value),
|
||||
self::DATE_TIME => $this->validateDatetime($field, $value),
|
||||
|
||||
+2
-9
@@ -16,23 +16,16 @@ class EmailValidator extends BaseValidator
|
||||
/**
|
||||
* @return bool
|
||||
* 检查是否存在
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function trigger(): bool
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
|
||||
if (!preg_match($exp, $value)) {
|
||||
if (!filter_var($value,FILTER_VALIDATE_EMAIL)) {
|
||||
return $this->addError($field,'The param :attribute format error');
|
||||
}
|
||||
[$account, $domain] = explode("@", $value);
|
||||
if (checkdnsrr($domain, "MX")) {
|
||||
return true;
|
||||
}
|
||||
return $this->addError($field,'The param :attribute format error');
|
||||
}, $this->params);
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -33,13 +33,13 @@ class EmptyValidator extends BaseValidator
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params, $method) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (empty($value)) {
|
||||
return $this->addError($field,':attribute not exists');
|
||||
if ($value === null) {
|
||||
return $this->addError($field, 'The :attribute can not null.');
|
||||
}
|
||||
return match ($method) {
|
||||
self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError($field,'The :attribute can not empty.'),
|
||||
default => $value !== null || $this->addError($field,'The :attribute can not empty.')
|
||||
};
|
||||
if (empty($value)) {
|
||||
return $this->addError($field, 'The :attribute can not empty.');
|
||||
}
|
||||
return true;
|
||||
}, $this->params, strtolower($this->method));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-7
@@ -22,17 +22,12 @@ class EnumValidator extends BaseValidator
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params, $values) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (is_null($value)) {
|
||||
if (in_array($value, $values)) {
|
||||
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);
|
||||
return $this->addError($field, $message);
|
||||
}
|
||||
return true;
|
||||
|
||||
}, $this->params, $this->value);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,14 +21,12 @@ class IntegerValidator extends BaseValidator
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function trigger(): bool
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params, $origin, $type) {
|
||||
$value = $params[$field] ?? null;
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
if ($type !== self::MIN && $value < $origin) {
|
||||
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;
|
||||
|
||||
|
||||
use ReflectionException;
|
||||
|
||||
class LengthValidator extends BaseValidator
|
||||
{
|
||||
|
||||
@@ -22,18 +24,12 @@ class LengthValidator extends BaseValidator
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function trigger(): bool
|
||||
{
|
||||
return $this->_validator($this->field, function ($field, $params, $method, $length) {
|
||||
$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) {
|
||||
self::MAX_LENGTH => $this->maxLength($field, (string)$value),
|
||||
self::MIN_LENGTH => $this->minLength($field, (string)$value),
|
||||
@@ -48,6 +44,7 @@ class LengthValidator extends BaseValidator
|
||||
* @return bool
|
||||
*
|
||||
* 效验长度是否大于最大长度
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function maxLength($field, $value): bool
|
||||
{
|
||||
@@ -72,6 +69,7 @@ class LengthValidator extends BaseValidator
|
||||
* @return bool
|
||||
*
|
||||
* 效验长度是否小于最小长度
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function minLength($field, $value): bool
|
||||
{
|
||||
@@ -96,6 +94,7 @@ class LengthValidator extends BaseValidator
|
||||
* @return bool
|
||||
*
|
||||
* 效验长度是否小于最小长度
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function defaultLength($field, $value): bool
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@ class RequiredValidator extends BaseValidator
|
||||
/**
|
||||
* @return bool
|
||||
* 检查是否存在
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
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;
|
||||
}
|
||||
$value = $params[$field] ?? null;
|
||||
if (is_null($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->{$method . 'Format'}($field, $value);
|
||||
}, $this->params, $this->method, $this->types);
|
||||
}
|
||||
@@ -59,12 +57,10 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
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))) {
|
||||
return $this->addError($field, 'The ' . $field . ' not is JSON data.');
|
||||
}
|
||||
@@ -75,13 +71,11 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function serializeFormat($field, $value): bool
|
||||
{
|
||||
if (!is_string($value) || is_numeric($value)) {
|
||||
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
|
||||
}
|
||||
if (false === swoole_unserialize($value)) {
|
||||
if (false === unserialize($value)) {
|
||||
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
|
||||
}
|
||||
return true;
|
||||
@@ -91,6 +85,7 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function arrayFormat($field, $value): bool
|
||||
{
|
||||
@@ -104,10 +99,11 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
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 true;
|
||||
@@ -117,16 +113,13 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function integerFormat($field, $value): bool
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
return $this->addError($field, 'The ' . $field . ' not is number data.');
|
||||
}
|
||||
if ((int)$value != $value) {
|
||||
return $this->addError($field, 'The ' . $field . ' not is number data.');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -134,6 +127,7 @@ class TypesOfValidator extends BaseValidator
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function floatFormat($field, $value): bool
|
||||
{
|
||||
|
||||
+3
-103
@@ -17,108 +17,11 @@ use Kiri;
|
||||
class Validator extends BaseValidator
|
||||
{
|
||||
|
||||
use RuleTrait;
|
||||
|
||||
/** @var BaseValidator[] */
|
||||
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
|
||||
@@ -127,10 +30,7 @@ class Validator extends BaseValidator
|
||||
*/
|
||||
public static function instance(array $params, ModelInterface $model): static
|
||||
{
|
||||
$validator = static::$instance;
|
||||
if ($validator == null) {
|
||||
$validator = static::$instance = new Validator();
|
||||
}
|
||||
$validator = new Validator();
|
||||
$validator->setParams($params);
|
||||
$validator->setModel($model);
|
||||
return $validator;
|
||||
|
||||
Reference in New Issue
Block a user