Skip to content

Commit

Permalink
增加全局验证规则注入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Dec 17, 2024
1 parent e162f3d commit 337e34c
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,35 @@ class Validate
*/
protected $regex = [];

/**
* @var Closure[]
*/
protected static $maker = [];

/**
* 构造方法
* @access public
*/
public function __construct()
{
if (!empty(static::$maker)) {
foreach (static::$maker as $maker) {
call_user_func($maker, $this);
}
}
}

/**
* 设置服务注入
* @access public
* @param Closure $maker
* @return void
*/
public static function maker(Closure $maker)
{
static::$maker[] = $maker;
}

/**
* 添加字段验证规则
* @access protected
Expand Down Expand Up @@ -555,10 +584,6 @@ public function check(array $data, array | string $rules = []): bool
{
$this->error = [];

if ($this->currentScene) {
$this->getScene($this->currentScene);
}

if (empty($rules)) {
// 读取验证规则
$rules = $this->rules();
Expand All @@ -578,6 +603,10 @@ public function check(array $data, array | string $rules = []): bool
$rules = $rules->getRules();
}

if ($this->currentScene) {
$this->getScene($this->currentScene);
}

foreach ($this->append as $key => $rule) {
if (!isset($rules[$key])) {
$rules[$key] = $rule;
Expand Down Expand Up @@ -1943,7 +1972,7 @@ protected function getScene(string $scene): void
*/
protected function getGroupRules(string $group)
{
$method = 'group' . Str::studly($group);
$method = 'rules' . Str::studly($group);
if (method_exists($this, $method)) {
$validate = call_user_func_array([$this, $method], [new self]);
return $validate->alias($this->alias)
Expand Down

0 comments on commit 337e34c

Please sign in to comment.