Files
kiri-validator/Validator.php
T

118 lines
4.2 KiB
PHP
Raw Normal View History

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-12-18 15:01:09 +08:00
/**
* classMap
*/
const array classMap = [
'not empty' => ['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,],
2023-12-18 15:04:56 +08:00
'maxlength' => ['class' => LengthValidator::class, 'method' => LengthValidator::MAX_LENGTH,],
'minlength' => ['class' => LengthValidator::class, 'method' => LengthValidator::MIN_LENGTH,],
'email' => ['class' => EmailValidator::class],
'length' => ['class' => LengthValidator::class],
2023-12-18 15:01:09 +08:00
'round' => ['class' => RoundValidator::class,],
];
2023-07-07 17:24:03 +08:00
/** @var BaseValidator[] */
private ?array $validators = [];
/**
* @param ModelInterface $model
2023-12-18 15:20:58 +08:00
* @param array $fields
* @param array $rules
2023-07-07 17:24:03 +08:00
* @return $this
*/
2023-12-18 15:01:09 +08:00
public function make(ModelInterface $model, array $fields, array $rules): static
2023-07-07 17:24:03 +08:00
{
2023-12-18 15:01:09 +08:00
foreach ($fields as $field) {
2023-12-18 15:20:58 +08:00
if (!isset($this->validators[$field])) {
$this->validators[$field] = [];
}
foreach ($rules 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);
}
2023-07-07 17:24:03 +08:00
}
}
2023-12-18 15:20:58 +08:00
return $this;
2023-07-07 17:24:03 +08:00
}
2023-12-18 15:01:09 +08:00
2023-07-07 17:24:03 +08:00
/**
2023-12-18 15:20:58 +08:00
* @param ModelInterface $model
2023-12-18 15:01:09 +08:00
* @param $key
* @param $val
* @return array
2023-12-18 15:20:58 +08:00
* @throws
2023-07-07 17:24:03 +08:00
*/
2023-12-18 15:01:09 +08:00
protected function mapGen(ModelInterface $model, $key, $val): array
2023-07-07 17:24:03 +08:00
{
2023-12-18 15:01:09 +08:00
if (is_numeric($key)) {
$defined = self::classMap[$val];
} else {
$defined = self::classMap[$key];
$defined['value'] = $val;
2023-07-07 17:24:03 +08:00
}
2023-12-18 15:01:09 +08:00
if ($defined['class'] == UniqueValidator::class) {
$defined['model'] = $model;
2023-07-07 17:24:03 +08:00
}
2023-12-18 15:01:09 +08:00
return [Kiri::createObject($defined), 'trigger'];
2023-07-07 17:24:03 +08:00
}
/**
2023-12-18 15:01:09 +08:00
* @param ModelInterface $model
* @return bool
2023-07-07 17:24:03 +08:00
*/
2023-12-18 15:01:09 +08:00
public function validation(ModelInterface $model): bool
2023-07-07 17:24:03 +08:00
{
2023-12-18 15:01:09 +08:00
if (count($this->validators) < 1) {
return true;
2023-07-07 17:24:03 +08:00
}
2023-12-18 15:01:09 +08:00
$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;
2023-07-07 17:24:03 +08:00
}
2022-01-09 14:01:11 +08:00
}