laravel 中自动批量注册验证规则到验证器

laravel 中自动批量注册验证规则到验证器

创建抽象验证规则 App\Rules\RuleApp\Rules\RegexRule

passes($attribute, $value)) {
            $fail(static::message())->translate();
        }
    }

    /**
     * 规则名称
     */
    public static function name(): string
    {
        return Str::of(class_basename(static::class))->replaceLast('Rule', '')->snake();
    }

    /**
     * 错误消息
     */
    public static function message(): string
    {
        $transMessage = __($transKey = sprintf('validation.%s', static::name()));

        return $transMessage === $transKey ? static::fallbackMessage() : $transMessage;
    }

    /**
     * 扩展方法
     */
    public static function extendMethod(): string
    {
        $ruleReflectionClass = new \ReflectionClass(static::class);

        $isImplicit = $ruleReflectionClass->getDefaultProperties()['implicit'] ?? false;
        if ($isImplicit) {
            return 'extendImplicit';
        }

        // extendDependent todo

        return 'extend';
    }

    /**
     * 回退错误消息
     */
    protected static function fallbackMessage(): string
    {
        return __(
            app()->isLocale('zh_CN')
                ? ':Attribute [:input] 必须是有效的 :Name。'
                : 'The :attribute [:input] must be a valid :Name.',
            [
                'name' => value(static function () {
                    $name = __($key = sprintf('validation.attributes.%s', static::name()));

                    return $name === $key ? str(static::name())->replace('_', ' ') : $name;
                }),
            ]
        );
    }
}
pattern(), $value);
    }
}

创建验证规则特性 App\Rules\Concerns\DataAwareApp\Rules\Concerns\ValidatorAware

data = $data;

        return $this;
    }
}
validator = $validator;

        return $this;
    }
}

创建验证规则

普通验证规则

正则验证规则

隐式验证规则

validator->getData();
            $data[$attribute] = $this->default;
            $this->validator->setData($data);
        }

        return true;
    }
}

注册验证规则到验证器

创建类发现器 App\Support\Discover

注册验证规则

instanceOf(Rule::class)
            ->all()
            ->each(static function (\ReflectionClass $ruleReflectionClass, $ruleClass): void {
                /** @var Rule&class-string $ruleClass */
                Validator::{$ruleClass::extendMethod()}(
                    $ruleClass::name(),
                    static fn (
                        string $attribute,
                        mixed $value,
                        array $parameters,
                        \Illuminate\Validation\Validator $validator
                    ): bool => tap(new $ruleClass(...$parameters), static function (Rule $rule) use ($validator): void {
                        $rule instanceof ValidatorAwareRule and $rule->setValidator($validator);
                        $rule instanceof DataAwareRule and $rule->setData($validator->getData());
                    })->passes($attribute, $value),
                    $ruleClass::message()
                );
            });
    }
}

验证

 'foo',
        'phone' => 'bar',
    ],
    [
        // 'base64' => new Base64Rule(),
        'base64' => 'base64', // 验证规则名称
        'phone' => new PhoneRule(), // 验证规则实例
        // 'phone' => 'phone',
    ]
);
dump($validator->errors()->all());

$validator = Validator::make(
    [
        // 'default' => 'baz',
    ],
    [
        'default' => 'default:baz',
    ]
);
dump($validator->validate());
array:2 [
  0 => "Base64 [foo] 必须是有效的 Base64。"
  1 => "电话 [bar] 必须是有效的 电话。"
]

array:1 [
  "default" => "baz"
]

相关连接

原文连接

你可能感兴趣的:(laravel 中自动批量注册验证规则到验证器)