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-12 14:10:32 +08:00
|
|
|
use Kiri;
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Validator
|
|
|
|
|
* @package validator
|
|
|
|
|
*/
|
|
|
|
|
class Validator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
use RuleTrait;
|
|
|
|
|
|
|
|
|
|
/** @var BaseValidator[] */
|
|
|
|
|
private ?array $validators = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @param ModelInterface $model
|
|
|
|
|
* @return Validator
|
|
|
|
|
*/
|
|
|
|
|
public static function instance(array $params, ModelInterface $model): static
|
|
|
|
|
{
|
|
|
|
|
$validator = new Validator();
|
|
|
|
|
$validator->setParams($params);
|
|
|
|
|
$validator->setModel($model);
|
|
|
|
|
return $validator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @param $rules
|
|
|
|
|
* @return $this
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-07-07 17:24:03 +08:00
|
|
|
*/
|
|
|
|
|
public function make($field, $rules): static
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($field)) {
|
|
|
|
|
$field = [$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$param = $this->getParams();
|
|
|
|
|
$model = $this->getModel();
|
|
|
|
|
|
|
|
|
|
$this->createRule($field, $rules, $model, $param);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @param $rule
|
|
|
|
|
* @param $model
|
|
|
|
|
* @param $param
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-07-07 17:24:03 +08:00
|
|
|
* ['maxLength'=>150, 'required', 'minLength' => 100]
|
|
|
|
|
*/
|
|
|
|
|
public function createRule($field, $rule, $model, $param): void
|
|
|
|
|
{
|
|
|
|
|
$define = ['field' => $field];
|
|
|
|
|
foreach ($rule as $key => $val) {
|
|
|
|
|
if (is_string($key)) {
|
2023-12-12 15:35:34 +08:00
|
|
|
$type = strtolower($key);
|
2023-07-07 17:24:03 +08:00
|
|
|
$define['value'] = $val;
|
|
|
|
|
} else {
|
|
|
|
|
$type = strtolower($val);
|
|
|
|
|
}
|
|
|
|
|
if (!isset($this->classMap[$type])) {
|
|
|
|
|
$this->validators[] = [$model, $val];
|
|
|
|
|
} else {
|
2023-12-12 15:35:34 +08:00
|
|
|
$merge = array_merge($this->classMap[$type], $define, [
|
2023-07-07 17:24:03 +08:00
|
|
|
'params' => $param,
|
|
|
|
|
'model' => $model
|
|
|
|
|
]);
|
|
|
|
|
$this->validators[] = $merge;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-07-07 17:24:03 +08:00
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
|
$this->addError(null, $validator->getError());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$this->validators = [];
|
|
|
|
|
return !isset($isTrue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param BaseValidator|array|Closure $val
|
2023-10-24 17:22:29 +08:00
|
|
|
* @return array
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-07-07 17:24:03 +08:00
|
|
|
*/
|
2023-10-24 17:22:29 +08:00
|
|
|
private function check(BaseValidator|array|Closure $val): array
|
2023-07-07 17:24:03 +08:00
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
}
|