Files
kiri-validator/src/Validator.php
T

224 lines
4.6 KiB
PHP
Raw Normal View History

2021-08-11 15:13:35 +08:00
<?php
declare(strict_types=1);
namespace validator;
use Closure;
use Exception;
use Kiri\Kiri;
/**
* 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',
],
2021-10-25 16:39:56 +08:00
'enum' => [
2021-08-11 15:13:35 +08:00
'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',
],
2021-10-25 16:39:56 +08:00
'round' => [
'class' => 'validator\RoundValidator',
2021-08-11 15:13:35 +08:00
],
];
/**
* @return Validator|null
*/
public static function getInstance(): ?Validator
{
if (static::$instance == null) {
static::$instance = new Validator();
}
return static::$instance;
}
/**
* @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();
foreach ($field as $val) {
$this->createRule($val, $rules, $model, $param);
}
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];
2021-10-25 16:39:56 +08:00
$is_model = is_null($model);
foreach ($rule as $key => $val) {
if (!$is_model) {
2021-08-11 15:13:35 +08:00
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;
}
2021-10-25 16:39:56 +08:00
$constr = array_merge($this->classMap[$type], $define, [
'params' => $param,
'model' => $model
]);
$this->validators[] = $constr;
2021-08-11 15:13:35 +08:00
}
}
/**
* @return bool
* @throws Exception
*/
public function validation(): bool
{
if (count($this->validators) < 1) {
return true;
}
foreach ($this->validators as $val) {
if ($this->check($val)) {
continue;
}
$isTrue = false;
if ($val instanceof BaseValidator) {
2021-12-02 16:31:41 +08:00
var_dump($val->getError());
2021-08-11 15:13:35 +08:00
$this->addError($val->getError());
}
break;
}
$this->validators = null;
$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);
}
2021-10-25 16:39:56 +08:00
$class = Kiri::getDi()->get($val['class']);
unset($val['class']);
Kiri::configure($class, $val);
return $class->trigger();
2021-08-11 15:13:35 +08:00
}
}