提交的内容

This commit is contained in:
2025-05-12 15:45:02 +08:00
parent 629c4750da
commit b48c692775
3043 changed files with 34732 additions and 60810 deletions

View File

@ -0,0 +1 @@
.idea

201
vendor/topthink/think-validate/LICENSE vendored Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 thinkphp.cn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1,82 @@
# think-validate
基于PHP8.0+ 的Validate实现
## 主要特性
- 基于PHP8和强类型实现
- 内置丰富的验证规则
- 支持验证器类、数组和链式方法定义验证规则
- 支持验证场景和验证分组
- 支持独立数据验证
- 支持枚举验证
- 支持批量验证
- 支持抛出异常
## 安装
~~~
composer require topthink/think-validate
~~~
## 用法
~~~php
use think\facade\Validate;
$validate = Validate::rule([
'name' => 'require|max:25',
'email' => 'email'
]);
$data = [
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com'
];
if (!$validate->check($data)) {
var_dump($validate->getError());
}
~~~
支持创建验证器进行数据验证
~~~php
<?php
namespace app\index\validate;
use think\Validate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
}
~~~
验证器调用代码如下:
~~~php
$data = [
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com',
];
$validate = new \app\index\validate\User;
if (!$validate->check($data)) {
var_dump($validate->getError());
}
~~~
## 文档
详细使用请参考 [ThinkValidate开发指南](https://doc.thinkphp.cn/@think-validate)

View File

@ -0,0 +1,23 @@
{
"name": "topthink/think-validate",
"description": "think validate",
"license": "Apache-2.0",
"authors": [
{
"name": "liu21st",
"email": "liu21st@gmail.com"
}
],
"require": {
"php": ">=8.0",
"topthink/think-container":">=3.0"
},
"autoload": {
"psr-4": {
"think\\": "src"
},
"files": [
"src/helper.php"
]
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace think\contract;
/**
* 枚举类接口
*/
interface Enumable
{
// 返回枚举类的清单
public static function values(): array;
}

View File

@ -0,0 +1,45 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\exception;
/**
* 数据验证异常
*/
class ValidateException extends \RuntimeException
{
public function __construct(protected $error, protected $key = '')
{
$this->message = is_array($error) ? json_encode($error) : $error;
}
/**
* 获取验证错误信息
* @access public
* @return array|string
*/
public function getError()
{
return $this->error;
}
/**
* 获取验证错误字段
* @access public
* @return string
*/
public function getKey()
{
return $this->key;
}
}

View File

@ -0,0 +1,80 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\facade;
use think\Facade;
/**
* @see \think\Validate
* @mixin \think\Validate
* @method static \think\Validate rule(mixed $name, mixed $rule = '') 添加字段验证规则
* @method static void extend(string $type, callable $callback = null, string $message='') 注册扩展验证(类型)规则
* @method static void setTypeMsg(mixed $type, string $msg = null) 设置验证规则的默认提示信息
* @method static \think\Validate message(mixed $name, string $message = '') 设置提示信息
* @method static \think\Validate scene(string $name) 设置验证场景
* @method static bool hasScene(string $name) 判断是否存在某个验证场景
* @method static \think\Validate batch(bool $batch = true) 设置批量验证
* @method static \think\Validate only(array $fields) 指定需要验证的字段列表
* @method static \think\Validate remove(mixed $field, mixed $rule = true) 移除某个字段的验证规则
* @method static \think\Validate append(mixed $field, mixed $rule = null) 追加某个字段的验证规则
* @method static bool confirm(mixed $value, mixed $rule, array $data = [], string $field = '') 验证是否和某个字段的值一致
* @method static bool different(mixed $value, mixed $rule, array $data = []) 验证是否和某个字段的值是否不同
* @method static bool egt(mixed $value, mixed $rule, array $data = []) 验证是否大于等于某个值
* @method static bool gt(mixed $value, mixed $rule, array $data = []) 验证是否大于某个值
* @method static bool elt(mixed $value, mixed $rule, array $data = []) 验证是否小于等于某个值
* @method static bool lt(mixed $value, mixed $rule, array $data = []) 验证是否小于某个值
* @method static bool eq(mixed $value, mixed $rule) 验证是否等于某个值
* @method static bool must(mixed $value, mixed $rule) 必须验证
* @method static bool is(mixed $value, mixed $rule, array $data = []) 验证字段值是否为有效格式
* @method static bool ip(mixed $value, mixed $rule) 验证是否有效IP
* @method static bool requireIf(mixed $value, mixed $rule) 验证某个字段等于某个值的时候必须
* @method static bool requireCallback(mixed $value, mixed $rule,array $data) 通过回调方法验证某个字段是否必须
* @method static bool requireWith(mixed $value, mixed $rule, array $data) 验证某个字段有值的情况下必须
* @method static bool filter(mixed $value, mixed $rule) 使用 filter_var 方式验证
* @method static bool in(mixed $value, mixed $rule) 验证是否在范围内
* @method static bool notIn(mixed $value, mixed $rule) 验证是否不在范围内
* @method static bool between(mixed $value, mixed $rule) between 验证数据
* @method static bool notBetween(mixed $value, mixed $rule) 使用 notbetween 验证数据
* @method static bool length(mixed $value, mixed $rule) 验证数据长度
* @method static bool max(mixed $value, mixed $rule) 验证数据最大长度
* @method static bool min(mixed $value, mixed $rule) 验证数据最小长度
* @method static bool after(mixed $value, mixed $rule) 验证日期
* @method static bool before(mixed $value, mixed $rule) 验证日期
* @method static bool expire(mixed $value, mixed $rule) 验证有效期
* @method static bool allowIp(mixed $value, mixed $rule) 验证 IP 许可
* @method static bool denyIp(mixed $value, mixed $rule) 验证 IP 禁用
* @method static bool regex(mixed $value, mixed $rule) 使用正则验证数据
* @method static bool token(mixed $value, mixed $rule) 验证表单令牌
* @method static bool dateFormat(mixed $value, mixed $rule) 验证时间和日期是否符合指定格式
* @method static bool unique(mixed $value, mixed $rule, array $data = [], string $field = '') 验证是否唯一
* @method static bool check(array $data, mixed $rules = []) 数据自动验证
* @method static bool checkRule(mixed $data, mixed $rules = []) 数据手动验证
* @method static mixed getError() 获取错误信息
*/
class Validate extends Facade
{
/**
* 始终创建新的对象实例
* @var bool
*/
protected static $alwaysNewInstance = true;
/**
* 获取当前Facade对应类名或者已经绑定的容器对象标识
* @access protected
* @return string
*/
protected static function getFacadeClass()
{
return 'think\Validate';
}
}

View File

@ -0,0 +1,67 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
//------------------------
// ThinkPHP 助手函数
//-------------------------
use think\Validate;
use think\validate\ValidateRuleSet;
if (!function_exists('validate')) {
/**
* 生成验证对象
* @param string|array $validate 验证器类名或者验证规则数组
* @param array $message 错误提示信息
* @param bool $batch 是否批量验证
* @param bool $failException 是否抛出异常
* @return Validate
*/
function validate($validate = '', array $message = [], bool $batch = false, bool $failException = true): Validate
{
if (is_array($validate) || '' === $validate) {
$v = new Validate();
if (is_array($validate)) {
$v->rule($validate);
}
} else {
if (str_contains($validate, '.')) {
// 支持场景
[$validate, $scene] = explode('.', $validate);
}
if (class_exists($validate)) {
$v = new $validate();
if (!empty($scene)) {
$v->scene($scene);
}
} else {
$v = new Validate();
}
}
return $v->message($message)->batch($batch)->failException($failException);
}
}
if (!function_exists('rules')) {
/**
* 定义ValidateRuleSet规则集合
* @param array $rules 验证因子集
* @return ValidateRuleSet
*/
function rules(array $rules): ValidateRuleSet
{
return ValidateRuleSet::rules($rules);
}
}

View File

@ -0,0 +1,187 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\validate;
/**
* Class ValidateRule
* @package think\validate
* @method static ValidateRule confirm(mixed $rule, string $msg = '') 验证是否和某个字段的值一致
* @method static ValidateRule different(mixed $rule, string $msg = '') 验证是否和某个字段的值是否不同
* @method static ValidateRule egt(mixed $rule, string $msg = '') 验证是否大于等于某个值
* @method static ValidateRule gt(mixed $rule, string $msg = '') 验证是否大于某个值
* @method static ValidateRule elt(mixed $rule, string $msg = '') 验证是否小于等于某个值
* @method static ValidateRule lt(mixed $rule, string $msg = '') 验证是否小于某个值
* @method static ValidateRule eg(mixed $rule, string $msg = '') 验证是否等于某个值
* @method static ValidateRule in(mixed $rule, string $msg = '') 验证是否在范围内
* @method static ValidateRule notIn(mixed $rule, string $msg = '') 验证是否不在某个范围
* @method static ValidateRule between(mixed $rule, string $msg = '') 验证是否在某个区间
* @method static ValidateRule notBetween(mixed $rule, string $msg = '') 验证是否不在某个区间
* @method static ValidateRule length(mixed $rule, string $msg = '') 验证数据长度
* @method static ValidateRule max(mixed $rule, string $msg = '') 验证数据最大长度
* @method static ValidateRule min(mixed $rule, string $msg = '') 验证数据最小长度
* @method static ValidateRule after(mixed $rule, string $msg = '') 验证日期
* @method static ValidateRule before(mixed $rule, string $msg = '') 验证日期
* @method static ValidateRule expire(mixed $rule, string $msg = '') 验证有效期
* @method static ValidateRule allowIp(mixed $rule, string $msg = '') 验证IP许可
* @method static ValidateRule denyIp(mixed $rule, string $msg = '') 验证IP禁用
* @method static ValidateRule regex(mixed $rule, string $msg = '') 使用正则验证数据
* @method static ValidateRule token(mixed $rule='__token__', string $msg = '') 验证表单令牌
* @method static ValidateRule is(mixed $rule, string $msg = '') 验证字段值是否为有效格式
* @method static ValidateRule isRequire(mixed $rule = null, string $msg = '') 验证字段必须
* @method static ValidateRule isNumber(mixed $rule = null, string $msg = '') 验证字段值是否为数字
* @method static ValidateRule isArray(mixed $rule = null, string $msg = '') 验证字段值是否为数组
* @method static ValidateRule isInteger(mixed $rule = null, string $msg = '') 验证字段值是否为整形
* @method static ValidateRule isFloat(mixed $rule = null, string $msg = '') 验证字段值是否为浮点数
* @method static ValidateRule isMobile(mixed $rule = null, string $msg = '') 验证字段值是否为手机
* @method static ValidateRule isIdCard(mixed $rule = null, string $msg = '') 验证字段值是否为身份证号码
* @method static ValidateRule isChs(mixed $rule = null, string $msg = '') 验证字段值是否为中文
* @method static ValidateRule isChsDash(mixed $rule = null, string $msg = '') 验证字段值是否为中文字母及下划线
* @method static ValidateRule isChsAlpha(mixed $rule = null, string $msg = '') 验证字段值是否为中文和字母
* @method static ValidateRule isChsAlphaNum(mixed $rule = null, string $msg = '') 验证字段值是否为中文字母和数字
* @method static ValidateRule isDate(mixed $rule = null, string $msg = '') 验证字段值是否为有效格式
* @method static ValidateRule isBool(mixed $rule = null, string $msg = '') 验证字段值是否为布尔值
* @method static ValidateRule isAlpha(mixed $rule = null, string $msg = '') 验证字段值是否为字母
* @method static ValidateRule isAlphaDash(mixed $rule = null, string $msg = '') 验证字段值是否为字母和下划线
* @method static ValidateRule isAlphaNum(mixed $rule = null, string $msg = '') 验证字段值是否为字母和数字
* @method static ValidateRule isAccepted(mixed $rule = null, string $msg = '') 验证字段值是否为yes, on, true, 或是 1
* @method static ValidateRule isDeclined(mixed $rule = null, string $msg = '') 验证字段值是否为no, off, false, 或是 0
* @method static ValidateRule isEmail(mixed $rule = null, string $msg = '') 验证字段值是否为有效邮箱格式
* @method static ValidateRule isUrl(mixed $rule = null, string $msg = '') 验证字段值是否为有效URL地址
* @method static ValidateRule activeUrl(mixed $rule, string $msg = '') 验证是否为合格的域名或者IP
* @method static ValidateRule ip(mixed $rule, string $msg = '') 验证是否有效IP
* @method static ValidateRule fileExt(mixed $rule, string $msg = '') 验证文件后缀
* @method static ValidateRule fileMime(mixed $rule, string $msg = '') 验证文件类型
* @method static ValidateRule fileSize(mixed $rule, string $msg = '') 验证文件大小
* @method static ValidateRule image(mixed $rule, string $msg = '') 验证图像文件
* @method static ValidateRule method(mixed $rule, string $msg = '') 验证请求类型
* @method static ValidateRule dateFormat(mixed $rule, string $msg = '') 验证时间和日期是否符合指定格式
* @method static ValidateRule unique(mixed $rule, string $msg = '') 验证是否唯一
* @method static ValidateRule behavior(mixed $rule, string $msg = '') 使用行为类验证
* @method static ValidateRule filter(mixed $rule, string $msg = '') 使用filter_var方式验证
* @method static ValidateRule acceptedIf(mixed $rule, string $msg = '') 验证某个字段等于指定的值,则验证中的字段必须为 yes、on、1 或 true
* @method static ValidateRule declinedIf(mixed $rule, string $msg = '') 验证某个字段等于指定的值,则验证中的字段必须为 no、off、0 或 false
* @method static ValidateRule requireIf(mixed $rule, string $msg = '') 验证某个字段等于某个值的时候必须
* @method static ValidateRule requireCallback(mixed $rule, string $msg = '') 通过回调方法验证某个字段是否必须
* @method static ValidateRule requireWith(mixed $rule, string $msg = '') 验证某个字段有值的情况下必须
* @method static ValidateRule must(mixed $rule = null, string $msg = '') 必须验证
*/
class ValidateRule
{
// 验证字段的名称
protected $title;
// 当前验证规则
protected $rule = [];
// 验证提示信息
protected $message = [];
/**
* 添加验证因子
* @access protected
* @param string $name 验证名称
* @param mixed $rule 验证规则
* @param string $msg 提示信息
* @return $this
*/
protected function addItem(string $name, $rule = null, string $msg = '')
{
if ($rule || 0 === $rule) {
$this->rule[$name] = $rule;
} else {
$this->rule[] = $name;
}
$this->message[] = $msg;
return $this;
}
/**
* 添加验证因子集
* @access protected
* @param array $rules 验证规则
* @param array $msg 提示信息
* @return ValidateRuleSet
*/
public static function ruleSet(array $rules, array $msg = [])
{
return ValidateRuleSet::rules($rules, $msg);
}
/**
* 获取验证规则
* @access public
* @return array
*/
public function getRule(): array
{
return $this->rule;
}
/**
* 获取验证字段名称
* @access public
* @return string
*/
public function getTitle(): string
{
return $this->title ?: '';
}
/**
* 获取验证提示
* @access public
* @return array
*/
public function getMsg(): array
{
return $this->message;
}
/**
* 设置验证字段名称
* @access public
* @return $this
*/
public function title(string $title)
{
$this->title = $title;
return $this;
}
public function __call($method, $args)
{
if ('is' == strtolower(substr($method, 0, 2))) {
$method = substr($method, 2);
}
array_unshift($args, lcfirst($method));
return call_user_func_array([$this, 'addItem'], $args);
}
public static function __callStatic($method, $args)
{
$rule = new static();
if ('is' == strtolower(substr($method, 0, 2))) {
$method = substr($method, 2);
}
array_unshift($args, lcfirst($method));
return call_user_func_array([$rule, 'addItem'], $args);
}
}

View File

@ -0,0 +1,74 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\validate;
use Closure;
/**
* Class ValidateRuleSet
* @package think\validate
*/
class ValidateRuleSet
{
/**
* 构造方法
* @access public
*/
public function __construct(protected array|Closure $rules = [], protected array $message = [])
{
}
/**
* 添加验证因子
* @access public
* @param array $rules 验证因子
* @param array $message 错误信息
* @return static
*/
public static function rules(array|Closure $rules, array $message = [])
{
return new static($rules, $message);
}
/**
* 设置错误信息
* @access public
* @param array $message 错误信息
* @return $this
*/
public function message(array $message)
{
$this->message = $message;
return $this;
}
/**
* 获取验证因子
* @access public
* @return array
*/
public function getRules()
{
return $this->rules;
}
/**
* 获取错误信息
* @access public
* @return array
*/
public function getMessage(): array
{
return $this->message;
}
}