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