This commit is contained in:
2023-12-18 15:20:58 +08:00
parent 49cefbd1f9
commit 8684ab7045
+14 -25
View File
@@ -49,45 +49,34 @@ class Validator extends BaseValidator
/** /**
* @param ModelInterface $model * @param ModelInterface $model
* @param $field * @param array $fields
* @param $rules * @param array $rules
* @return $this * @return $this
*/ */
public function make(ModelInterface $model, array $fields, array $rules): static public function make(ModelInterface $model, array $fields, array $rules): static
{ {
foreach ($fields as $field) { foreach ($fields as $field) {
$this->createRule($field, $rules, $model); 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);
}
}
} }
return $this; return $this;
} }
/** /**
* @param string $field
* @param array $rule
* @param ModelInterface $model * @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 $key
* @param $val * @param $val
* @return array * @return array
* @throws \Exception * @throws
*/ */
protected function mapGen(ModelInterface $model, $key, $val): array protected function mapGen(ModelInterface $model, $key, $val): array
{ {