2022-01-09 14:01:11 +08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace validator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Closure;
|
2023-04-10 17:13:24 +08:00
|
|
|
use Database\ModelInterface;
|
2022-01-09 14:01:11 +08:00
|
|
|
use Exception;
|
2022-01-12 14:10:32 +08:00
|
|
|
use Kiri;
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Validator
|
|
|
|
|
* @package validator
|
|
|
|
|
*/
|
|
|
|
|
class Validator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 17:13:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @param ModelInterface $model
|
|
|
|
|
* @return Validator
|
|
|
|
|
*/
|
|
|
|
|
public static function instance(array $params, ModelInterface $model): static
|
|
|
|
|
{
|
|
|
|
|
if (static::$instance == null) {
|
|
|
|
|
static::$instance = new Validator();
|
|
|
|
|
}
|
|
|
|
|
static::$instance->setParams($params);
|
|
|
|
|
static::$instance->setModel($model);
|
|
|
|
|
return static::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 14:01:11 +08:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @param $rules
|
|
|
|
|
* @return $this
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function make($field, $rules): static
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($field)) {
|
|
|
|
|
$field = [$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$param = $this->getParams();
|
|
|
|
|
$model = $this->getModel();
|
2022-02-18 17:16:45 +08:00
|
|
|
|
|
|
|
|
$this->createRule($field, $rules, $model, $param);
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @param $rule
|
|
|
|
|
* @param $model
|
|
|
|
|
* @param $param
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* ['maxLength'=>150, 'required', 'minLength' => 100]
|
|
|
|
|
*/
|
|
|
|
|
public function createRule($field, $rule, $model, $param)
|
|
|
|
|
{
|
|
|
|
|
$define = ['field' => $field];
|
|
|
|
|
|
|
|
|
|
$is_model = is_null($model);
|
|
|
|
|
foreach ($rule as $key => $val) {
|
|
|
|
|
if (!$is_model) {
|
|
|
|
|
if (is_string($val) && method_exists($model, $val)) {
|
|
|
|
|
$this->validators[] = [$model, $val];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (is_string($key)) {
|
|
|
|
|
$type = strtolower($key);
|
|
|
|
|
$define['value'] = $val;
|
|
|
|
|
} else {
|
|
|
|
|
$type = strtolower($val);
|
|
|
|
|
}
|
|
|
|
|
if (!isset($this->classMap[$type])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->validators[] = array_merge($this->classMap[$type], $define, [
|
|
|
|
|
'params' => $param,
|
|
|
|
|
'model' => $model
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function validation(): bool
|
|
|
|
|
{
|
|
|
|
|
if (count($this->validators) < 1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
foreach ($this->validators as $val) {
|
|
|
|
|
[$result, $validator] = $this->check($val);
|
|
|
|
|
if ($result === true) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$isTrue = false;
|
|
|
|
|
if ($validator instanceof BaseValidator) {
|
2022-02-25 17:12:15 +08:00
|
|
|
$this->addError(null, $validator->getError());
|
2022-01-09 14:01:11 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$this->validators = [];
|
|
|
|
|
return !isset($isTrue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param BaseValidator|array|Closure $val
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function check(BaseValidator|array|Closure $val): mixed
|
|
|
|
|
{
|
|
|
|
|
if (is_callable($val, true)) {
|
|
|
|
|
return [call_user_func($val, $this), $val];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$class = Kiri::getDi()->get($val['class']);
|
|
|
|
|
unset($val['class']);
|
|
|
|
|
|
|
|
|
|
Kiri::configure($class, $val);
|
|
|
|
|
|
|
|
|
|
return [$class->trigger(), $class];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|