['class' => EmptyValidator::class, 'method' => EmptyValidator::CAN_NOT_EMPTY,], 'not null' => ['class' => EmptyValidator::class, 'method' => EmptyValidator::CAN_NOT_NULL,], 'required' => ['class' => RequiredValidator::class,], 'enum' => ['class' => EnumValidator::class,], 'unique' => ['class' => UniqueValidator::class,], 'datetime' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::DATE_TIME,], 'date' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::DATE,], 'time' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::TIME,], 'timestamp' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::STR_TO_TIME,], 'string' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::STRING,], 'int' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::INTEGER,], 'min' => ['class' => IntegerValidator::class], 'max' => ['class' => IntegerValidator::class], 'json' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::JSON,], 'float' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::FLOAT,], 'array' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::ARRAY,], 'maxlength' => ['class' => LengthValidator::class, 'method' => 'max',], 'minlength' => ['class' => LengthValidator::class, 'method' => 'min',], 'email' => ['class' => EmailValidator::class, 'method' => 'email',], 'length' => ['class' => LengthValidator::class, 'method' => 'default',], 'round' => ['class' => RoundValidator::class,], ]; /** @var BaseValidator[] */ private ?array $validators = []; /** * @param ModelInterface $model * @param $field * @param $rules * @return $this */ public function make(ModelInterface $model, array $fields, array $rules): static { foreach ($fields as $field) { $this->createRule($field, $rules, $model); } return $this; } /** * @param string $field * @param array $rule * @param ModelInterface $model * @throws \Exception */ public function createRule(string $field, array $rule, ModelInterface $model): void { if (!isset($this->validators[$field])) { $this->validators[$field] = []; } foreach ($rule as $key => $val) { if (is_numeric($key) && method_exists($model, $val)) { $this->validators[$field][] = [$model, $val]; } else { $this->validators[$field][] = $this->mapGen($model, $key, $val); } } } /** * @param array $defined * @param $key * @param $val * @return array * @throws \Exception */ protected function mapGen(ModelInterface $model, $key, $val): array { if (is_numeric($key)) { $defined = self::classMap[$val]; } else { $defined = self::classMap[$key]; $defined['value'] = $val; } if ($defined['class'] == UniqueValidator::class) { $defined['model'] = $model; } return [Kiri::createObject($defined), 'trigger']; } /** * @param ModelInterface $model * @return bool */ public function validation(ModelInterface $model): bool { if (count($this->validators) < 1) { return true; } $attributes = $model->getChanges(); foreach ($attributes as $field => $attribute) { if (isset($this->validators[$field])) { $validator = $this->validators[$field]; foreach ($validator as $value) { if (!call_user_func($value, $field, $attribute)) { return $this->addError($field, 'field :attribute data format error.'); } } } } return true; } }