邹景立 34104dad22 Update plugin headers and add rewrite-model script
Replace header text "Payment Plugin for ThinkAdmin" with "ThinkAdmin Plugin for ThinkAdmin" across project files (configs, controllers, plugins, php-cs-fixer, etc.) to unify branding. Add a new composer script "rewrite-model" to regenerate models and run php-cs-fixer. Also apply a minor newline fix in .copilot-commit-message-instructions.md.
2026-02-01 14:24:36 +08:00

86 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin for ThinkAdmin
* +----------------------------------------------------------------------
* | 版权所有 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | 官方网站: https://thinkadmin.top
* +----------------------------------------------------------------------
* | 开源协议 ( https://mit-license.org )
* | 免责声明 ( https://thinkadmin.top/disclaimer )
* | 会员特权 ( https://thinkadmin.top/vip-introduce )
* +----------------------------------------------------------------------
* | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
* | github 代码仓库https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
namespace think\admin\helper;
use think\admin\Helper;
use think\Validate;
/**
* 快捷输入验证器.
* @class ValidateHelper
*/
class ValidateHelper extends Helper
{
/**
* 快捷输入并验证( 支持 规则 # 别名 .
* @param array $rules 验证规则( 验证信息数组
* @param array|string $input 输入内容 ( post. 或 get. )
* @param null|callable $callable 异常处理操作
* @return array|void
*
* age.require => message // 最大值限定
* age.between:1,120 => message // 范围限定
* name.require => message // 必填内容
* name.default => 100 // 获取并设置默认值
* region.value => value // 固定字段数值内容
*
* 更多规则参照 ThinkPHP 官方的验证类
*/
public function init(array $rules, $input = '', ?callable $callable = null)
{
if (is_string($input)) {
$type = trim($input, '.') ?: 'param';
$input = $this->app->request->{$type}();
}
[$data, $rule, $info] = [[], [], []];
foreach ($rules as $key => $value) {
if (is_numeric($key)) {
[$key, $alias] = explode('#', "{$value}#");
$data[$key] = $input[$alias ?: $key] ?? null;
} elseif (strpos($key, '.') === false) {
$data[$key] = $value;
} elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', "{$key}#", $matches)) {
[, $_key, $_rule, $alias] = $matches;
if (in_array($_rule, ['value', 'default'])) {
if ($_rule === 'value') {
$data[$_key] = $value;
}
if ($_rule === 'default') {
$data[$_key] = $input[$alias ?: $_key] ?? $value;
}
} else {
$info[explode(':', "{$_key}.{$_rule}")[0]] = $value;
$data[$_key] = $data[$_key] ?? ($input[$alias ?: $_key] ?? null);
$rule[$_key] = isset($rule[$_key]) ? "{$rule[$_key]}|{$_rule}" : $_rule;
}
}
}
$validate = new Validate();
if ($validate->rule($rule)->message($info)->check($data)) {
return $data;
}
if (is_callable($callable)) {
return call_user_func($callable, lang($validate->getError()), $data);
}
$this->class->error(lang($validate->getError()));
}
}