1537 lines
43 KiB
PHP
Executable File
1537 lines
43 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | Author: liu21st <liu21st@gmail.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace think;
|
||
|
||
use think\exception\ClassNotFoundException;
|
||
use think\validate\ValidateRule;
|
||
|
||
class Validate
|
||
{
|
||
|
||
/**
|
||
* 自定义验证类型
|
||
* @var array
|
||
*/
|
||
protected static $type = [];
|
||
|
||
/**
|
||
* 验证类型别名
|
||
* @var array
|
||
*/
|
||
protected $alias = [
|
||
'>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',
|
||
];
|
||
|
||
/**
|
||
* 当前验证规则
|
||
* @var array
|
||
*/
|
||
protected $rule = [];
|
||
|
||
/**
|
||
* 验证提示信息
|
||
* @var array
|
||
*/
|
||
protected $message = [];
|
||
|
||
/**
|
||
* 验证字段描述
|
||
* @var array
|
||
*/
|
||
protected $field = [];
|
||
|
||
/**
|
||
* 默认规则提示
|
||
* @var array
|
||
*/
|
||
protected static $typeMsg = [
|
||
'require' => ':attribute require',
|
||
'must' => ':attribute must',
|
||
'number' => ':attribute must be numeric',
|
||
'integer' => ':attribute must be integer',
|
||
'float' => ':attribute must be float',
|
||
'boolean' => ':attribute must be bool',
|
||
'email' => ':attribute not a valid email address',
|
||
'mobile' => ':attribute not a valid mobile',
|
||
'array' => ':attribute must be a array',
|
||
'accepted' => ':attribute must be yes,on or 1',
|
||
'date' => ':attribute not a valid datetime',
|
||
'file' => ':attribute not a valid file',
|
||
'image' => ':attribute not a valid image',
|
||
'alpha' => ':attribute must be alpha',
|
||
'alphaNum' => ':attribute must be alpha-numeric',
|
||
'alphaDash' => ':attribute must be alpha-numeric, dash, underscore',
|
||
'activeUrl' => ':attribute not a valid domain or ip',
|
||
'chs' => ':attribute must be chinese',
|
||
'chsAlpha' => ':attribute must be chinese or alpha',
|
||
'chsAlphaNum' => ':attribute must be chinese,alpha-numeric',
|
||
'chsDash' => ':attribute must be chinese,alpha-numeric,underscore, dash',
|
||
'url' => ':attribute not a valid url',
|
||
'ip' => ':attribute not a valid ip',
|
||
'dateFormat' => ':attribute must be dateFormat of :rule',
|
||
'in' => ':attribute must be in :rule',
|
||
'notIn' => ':attribute be notin :rule',
|
||
'between' => ':attribute must between :1 - :2',
|
||
'notBetween' => ':attribute not between :1 - :2',
|
||
'length' => 'size of :attribute must be :rule',
|
||
'max' => 'max size of :attribute must be :rule',
|
||
'min' => 'min size of :attribute must be :rule',
|
||
'after' => ':attribute cannot be less than :rule',
|
||
'before' => ':attribute cannot exceed :rule',
|
||
'afterWith' => ':attribute cannot be less than :rule',
|
||
'beforeWith' => ':attribute cannot exceed :rule',
|
||
'expire' => ':attribute not within :rule',
|
||
'allowIp' => 'access IP is not allowed',
|
||
'denyIp' => 'access IP denied',
|
||
'confirm' => ':attribute out of accord with :2',
|
||
'different' => ':attribute cannot be same with :2',
|
||
'egt' => ':attribute must greater than or equal :rule',
|
||
'gt' => ':attribute must greater than :rule',
|
||
'elt' => ':attribute must less than or equal :rule',
|
||
'lt' => ':attribute must less than :rule',
|
||
'eq' => ':attribute must equal :rule',
|
||
'unique' => ':attribute has exists',
|
||
'regex' => ':attribute not conform to the rules',
|
||
'method' => 'invalid Request method',
|
||
'token' => 'invalid token',
|
||
'fileSize' => 'filesize not match',
|
||
'fileExt' => 'extensions to upload is not allowed',
|
||
'fileMime' => 'mimetype to upload is not allowed',
|
||
];
|
||
|
||
/**
|
||
* 当前验证场景
|
||
* @var array
|
||
*/
|
||
protected $currentScene = null;
|
||
|
||
/**
|
||
* Filter_var 规则
|
||
* @var array
|
||
*/
|
||
protected $filter = [
|
||
'email' => FILTER_VALIDATE_EMAIL,
|
||
'ip' => [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6],
|
||
'integer' => FILTER_VALIDATE_INT,
|
||
'url' => FILTER_VALIDATE_URL,
|
||
'macAddr' => FILTER_VALIDATE_MAC,
|
||
'float' => FILTER_VALIDATE_FLOAT,
|
||
];
|
||
|
||
/**
|
||
* 内置正则验证规则
|
||
* @var array
|
||
*/
|
||
protected $regex = [
|
||
'alphaDash' => '/^[A-Za-z0-9\-\_]+$/',
|
||
'chs' => '/^[\x{4e00}-\x{9fa5}]+$/u',
|
||
'chsAlpha' => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u',
|
||
'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u',
|
||
'chsDash' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\-]+$/u',
|
||
'mobile' => '/^1[3-9][0-9]\d{8}$/',
|
||
'idCard' => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/',
|
||
'zip' => '/\d{6}/',
|
||
];
|
||
|
||
/**
|
||
* 验证场景定义
|
||
* @var array
|
||
*/
|
||
protected $scene = [];
|
||
|
||
/**
|
||
* 验证失败错误信息
|
||
* @var array
|
||
*/
|
||
protected $error = [];
|
||
|
||
/**
|
||
* 是否批量验证
|
||
* @var bool
|
||
*/
|
||
protected $batch = false;
|
||
|
||
/**
|
||
* 场景需要验证的规则
|
||
* @var array
|
||
*/
|
||
protected $only = [];
|
||
|
||
/**
|
||
* 场景需要移除的验证规则
|
||
* @var array
|
||
*/
|
||
protected $remove = [];
|
||
|
||
/**
|
||
* 场景需要追加的验证规则
|
||
* @var array
|
||
*/
|
||
protected $append = [];
|
||
|
||
/**
|
||
* 架构函数
|
||
* @access public
|
||
* @param array $rules 验证规则
|
||
* @param array $message 验证提示信息
|
||
* @param array $field 验证字段描述信息
|
||
*/
|
||
public function __construct(array $rules = [], array $message = [], array $field = [])
|
||
{
|
||
$this->rule = $rules + $this->rule;
|
||
$this->message = array_merge($this->message, $message);
|
||
$this->field = array_merge($this->field, $field);
|
||
}
|
||
|
||
/**
|
||
* 创建一个验证器类
|
||
* @access public
|
||
* @param array $rules 验证规则
|
||
* @param array $message 验证提示信息
|
||
* @param array $field 验证字段描述信息
|
||
* @return Validate
|
||
*/
|
||
public static function make(array $rules = [], array $message = [], array $field = [])
|
||
{
|
||
return new self($rules, $message, $field);
|
||
}
|
||
|
||
/**
|
||
* 添加字段验证规则
|
||
* @access protected
|
||
* @param string|array $name 字段名称或者规则数组
|
||
* @param mixed $rule 验证规则或者字段描述信息
|
||
* @return $this
|
||
*/
|
||
public function rule($name, $rule = '')
|
||
{
|
||
if (is_array($name)) {
|
||
$this->rule = $name + $this->rule;
|
||
if (is_array($rule)) {
|
||
$this->field = array_merge($this->field, $rule);
|
||
}
|
||
} else {
|
||
$this->rule[$name] = $rule;
|
||
}
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 注册扩展验证(类型)规则
|
||
* @access public
|
||
* @param string $type 验证规则类型
|
||
* @param mixed $callback callback方法(或闭包)
|
||
* @return void
|
||
*/
|
||
public static function extend($type, $callback = null)
|
||
{
|
||
if (is_array($type)) {
|
||
self::$type = array_merge(self::$type, $type);
|
||
} else {
|
||
self::$type[$type] = $callback;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 设置验证规则的默认提示信息
|
||
* @access public
|
||
* @param string|array $type 验证规则类型名称或者数组
|
||
* @param string $msg 验证提示信息
|
||
* @return void
|
||
*/
|
||
public static function setTypeMsg($type, $msg = null)
|
||
{
|
||
if (is_array($type)) {
|
||
self::$typeMsg = array_merge(self::$typeMsg, $type);
|
||
} else {
|
||
self::$typeMsg[$type] = $msg;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 设置提示信息
|
||
* @access public
|
||
* @param string|array $name 字段名称
|
||
* @param string $message 提示信息
|
||
* @return Validate
|
||
*/
|
||
public function message($name, $message = '')
|
||
{
|
||
if (is_array($name)) {
|
||
$this->message = array_merge($this->message, $name);
|
||
} else {
|
||
$this->message[$name] = $message;
|
||
}
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 设置验证场景
|
||
* @access public
|
||
* @param string $name 场景名
|
||
* @return $this
|
||
*/
|
||
public function scene($name)
|
||
{
|
||
// 设置当前场景
|
||
$this->currentScene = $name;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 判断是否存在某个验证场景
|
||
* @access public
|
||
* @param string $name 场景名
|
||
* @return bool
|
||
*/
|
||
public function hasScene($name)
|
||
{
|
||
return isset($this->scene[$name]) || method_exists($this, 'scene' . $name);
|
||
}
|
||
|
||
/**
|
||
* 设置批量验证
|
||
* @access public
|
||
* @param bool $batch 是否批量验证
|
||
* @return $this
|
||
*/
|
||
public function batch($batch = true)
|
||
{
|
||
$this->batch = $batch;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 指定需要验证的字段列表
|
||
* @access public
|
||
* @param array $fields 字段名
|
||
* @return $this
|
||
*/
|
||
public function only($fields)
|
||
{
|
||
$this->only = $fields;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 移除某个字段的验证规则
|
||
* @access public
|
||
* @param string|array $field 字段名
|
||
* @param mixed $rule 验证规则 null 移除所有规则
|
||
* @return $this
|
||
*/
|
||
public function remove($field, $rule = null)
|
||
{
|
||
if (is_array($field)) {
|
||
foreach ($field as $key => $rule) {
|
||
if (is_int($key)) {
|
||
$this->remove($rule);
|
||
} else {
|
||
$this->remove($key, $rule);
|
||
}
|
||
}
|
||
} else {
|
||
if (is_string($rule)) {
|
||
$rule = explode('|', $rule);
|
||
}
|
||
|
||
$this->remove[$field] = $rule;
|
||
}
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 追加某个字段的验证规则
|
||
* @access public
|
||
* @param string|array $field 字段名
|
||
* @param mixed $rule 验证规则
|
||
* @return $this
|
||
*/
|
||
public function append($field, $rule = null)
|
||
{
|
||
if (is_array($field)) {
|
||
foreach ($field as $key => $rule) {
|
||
$this->append($key, $rule);
|
||
}
|
||
} else {
|
||
if (is_string($rule)) {
|
||
$rule = explode('|', $rule);
|
||
}
|
||
|
||
$this->append[$field] = $rule;
|
||
}
|