eee
This commit is contained in:
+1
-2
@@ -18,13 +18,12 @@ class ArrayValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
* 检查
|
* 检查
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return is_array($value);
|
return is_array($value);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-4
@@ -4,8 +4,6 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Database\Model;
|
|
||||||
use Database\ModelInterface;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
@@ -22,12 +20,11 @@ abstract class BaseValidator
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param float $value
|
* @param float $value
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
throw new Exception('Child Class must define method of trigger');
|
throw new Exception('Child Class must define method of trigger');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,11 @@ class DateTimeValidator extends BaseValidator
|
|||||||
public string $method;
|
public string $method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
$value = $params[$field] ?? null;
|
|
||||||
return match ($this->method) {
|
return match ($this->method) {
|
||||||
self::DATE => $this->validatorDate($value),
|
self::DATE => $this->validatorDate($value),
|
||||||
self::DATE_TIME => $this->validateDatetime($value),
|
self::DATE_TIME => $this->validateDatetime($value),
|
||||||
|
|||||||
+1
-2
@@ -16,12 +16,11 @@ class EmailValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
* 检查是否存在
|
* 检查是否存在
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return filter_var($value, FILTER_VALIDATE_EMAIL);
|
return filter_var($value, FILTER_VALIDATE_EMAIL);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -27,13 +27,12 @@ class EmptyValidator extends BaseValidator
|
|||||||
public string $method;
|
public string $method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
* 检查参数是否为NULL
|
* 检查参数是否为NULL
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return match ($this->method) {
|
return match ($this->method) {
|
||||||
self::CAN_NOT_NULL => !is_null($value),
|
self::CAN_NOT_NULL => !is_null($value),
|
||||||
|
|||||||
+1
-2
@@ -13,11 +13,10 @@ class EnumValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return in_array($value, $this->value);
|
return in_array($value, $this->value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Database\ModelInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -19,11 +17,10 @@ class IntegerValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param float $value
|
* @param float $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return (float)$value == $value;
|
return (float)$value == $value;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -22,11 +22,10 @@ class LengthValidator extends BaseValidator
|
|||||||
public string $method = 'default';
|
public string $method = 'default';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return match ($this->method) {
|
return match ($this->method) {
|
||||||
self::MAX_LENGTH => $this->maxLength((string)$value),
|
self::MAX_LENGTH => $this->maxLength((string)$value),
|
||||||
|
|||||||
@@ -14,12 +14,11 @@ class RequiredValidator extends BaseValidator
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
* 检查是否存在
|
* 检查是否存在
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return !is_null($value);
|
return !is_null($value);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -4,8 +4,6 @@
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RoundValidator
|
* Class RoundValidator
|
||||||
* @package validator
|
* @package validator
|
||||||
@@ -19,7 +17,7 @@ class RoundValidator extends BaseValidator
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return round($value, $this->value) == $value;
|
return round($value, $this->value) == $value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Database\ModelInterface;
|
|
||||||
use function json_validate;
|
use function json_validate;
|
||||||
|
|
||||||
class TypesOfValidator extends BaseValidator
|
class TypesOfValidator extends BaseValidator
|
||||||
@@ -31,7 +30,7 @@ class TypesOfValidator extends BaseValidator
|
|||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return match ($this->method) {
|
return match ($this->method) {
|
||||||
self::INTEGER => $this->integerFormat($value),
|
self::INTEGER => $this->integerFormat($value),
|
||||||
|
|||||||
+7
-3
@@ -23,15 +23,19 @@ class UniqueValidator extends BaseValidator
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @var string
|
||||||
|
*/
|
||||||
|
public string $field;
|
||||||
|
|
||||||
|
/**
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
* 检查是否存在
|
* 检查是否存在
|
||||||
*/
|
*/
|
||||||
public function trigger(string $field, mixed $value): bool
|
public function trigger(mixed $value): bool
|
||||||
{
|
{
|
||||||
return $this->model::query()->where([$field => $value])->exists() === false;
|
return $this->model::query()->where([$this->field => $value])->exists() === false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ class Validator extends BaseValidator
|
|||||||
if (is_numeric($key) && method_exists($model, $val)) {
|
if (is_numeric($key) && method_exists($model, $val)) {
|
||||||
$this->validators[$field][] = [$model, $val];
|
$this->validators[$field][] = [$model, $val];
|
||||||
} else {
|
} else {
|
||||||
$this->validators[$field][] = $this->mapGen($model, $key, $val);
|
$this->validators[$field][] = $this->mapGen($model, $field, $key, $val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,7 +77,7 @@ class Validator extends BaseValidator
|
|||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
protected function mapGen(ModelInterface $model, $key, $val): array
|
protected function mapGen(ModelInterface $model, $field, $key, $val): array
|
||||||
{
|
{
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key)) {
|
||||||
$defined = self::classMap[$val];
|
$defined = self::classMap[$val];
|
||||||
@@ -88,6 +87,7 @@ class Validator extends BaseValidator
|
|||||||
}
|
}
|
||||||
if ($defined['class'] == UniqueValidator::class) {
|
if ($defined['class'] == UniqueValidator::class) {
|
||||||
$defined['model'] = $model;
|
$defined['model'] = $model;
|
||||||
|
$defined['field'] = $field;
|
||||||
}
|
}
|
||||||
return [Kiri::createObject($defined), 'trigger'];
|
return [Kiri::createObject($defined), 'trigger'];
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ class Validator extends BaseValidator
|
|||||||
if (isset($this->validators[$field])) {
|
if (isset($this->validators[$field])) {
|
||||||
$validator = $this->validators[$field];
|
$validator = $this->validators[$field];
|
||||||
foreach ($validator as $value) {
|
foreach ($validator as $value) {
|
||||||
if (!call_user_func($value, $field, $attribute)) {
|
if (!call_user_func($value, $attribute)) {
|
||||||
return $this->addError($field, 'field :attribute data format error.');
|
return $this->addError($field, 'field :attribute data format error.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user