|
|
<?php
|
|
|
|
|
|
use Exception\CheckClientException;
|
|
|
|
|
|
/**
|
|
|
* 校验类
|
|
|
* Class CheckClient
|
|
|
*/
|
|
|
class CheckClient {
|
|
|
public static function checkEmpty($value, $fieldName, $customMessage = null) {
|
|
|
if (empty($value)) {
|
|
|
$message = $customMessage ?: "{$fieldName}必填";
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkAnyEmpty($message) {
|
|
|
$args = func_get_args();
|
|
|
unset($args[0]);
|
|
|
foreach ($args as $arg) {
|
|
|
if (empty($arg)) {
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkAllEmpty($message, ...$fields) {
|
|
|
foreach ($fields as $field) {
|
|
|
if (!empty($field)) {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
|
|
|
public static function checkNumeric($value, $fieldName) {
|
|
|
if (!is_numeric($value)) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 不是纯数字");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkBoolean($value, $fieldName) {
|
|
|
if (!is_bool($value)) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 必须是Boolean类型");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkMinValue($value, $minValue, $fieldName) {
|
|
|
self::checkNumeric($value, $fieldName);
|
|
|
if ($value < $minValue) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 不能小于:{$minValue}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkMaxValue($value, $maxValue, $fieldName) {
|
|
|
self::checkNumeric($value, $fieldName);
|
|
|
if ($value > $maxValue) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 不能大于:{$maxValue}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkIntegerDigit($value, $fieldName) {
|
|
|
if (!is_numeric($value) || ((string)(int)$value) !== (string)$value) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 必须是整数");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkMinLength($value, $minLen, $fieldName) {
|
|
|
$value = (string)$value;
|
|
|
if (mb_strlen($value, 'UTF-8') < $minLen) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 长度不能小于{$minLen}个字符.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkMaxLength($value, $maxLen, $fieldName) {
|
|
|
$value = (string)$value;
|
|
|
if (mb_strlen($value, 'UTF-8') > $maxLen) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 长度不能大于{$maxLen}个字符.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkSimplePassword($password, $fieldName = '密码') {
|
|
|
if (!preg_match('/^(?![\d]+$).{6,30}$/', $password)) {
|
|
|
throw new CheckClientException("{$fieldName}必须大于非纯数字,长度6到30个字符");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkPassword($password, $fieldName = '密码') {
|
|
|
if (!preg_match('/^(?=[^\s]*[0-9])(?=[^\s]*[a-zA-Z])(?=[^\s]*[^a-zA-Z0-9])[^\s]{8,30}$/', $password)) {
|
|
|
throw new CheckClientException("{$fieldName}必须含有数字、字母、特殊字符,8-30个字符");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkUserName($username, $fieldName = '用户名') {
|
|
|
if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]{5,31}$/', $username)) {
|
|
|
throw new CheckClientException("{$fieldName}不符合格式要求,仅支持字母,数字,下划线 字母开头,6-32个字符");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkArray($list, $fieldName) {
|
|
|
if (!is_array($list)) {
|
|
|
throw new CheckClientException("参数 {$fieldName} 必须为数组类型数据");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkEnum($value, $enumList, $fieldName, $message = null) {
|
|
|
if (!in_array($value, $enumList)) {
|
|
|
$message = $message ?: "{$fieldName}可选值不在预期范围之内,可选值:" . implode(',', $enumList);
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* check value is 0 or 1 digital
|
|
|
* @param $value
|
|
|
* @param $fieldName
|
|
|
*/
|
|
|
public static function checkNumberBool($value, $fieldName) {
|
|
|
self::checkNumberEnum($value, [0, 1], $fieldName);
|
|
|
}
|
|
|
|
|
|
public static function checkNumberEnum($value, $enumList, $fieldName) {
|
|
|
if (!is_numeric($value) || !in_array($value, $enumList)) {
|
|
|
throw new CheckClientException("{$fieldName}可选值不在预期范围之内,可选值:" . implode(',', $enumList));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static function checkUrl($value, $isMust = false, $fieldName) {
|
|
|
if (!$isMust && !$value) {
|
|
|
return ;
|
|
|
}
|
|
|
if (!$value) {
|
|
|
throw new CheckClientException("{$fieldName}不能为空");
|
|
|
}
|
|
|
if (false === filter_var($value, FILTER_VALIDATE_URL)) {
|
|
|
throw new CheckClientException("{$fieldName}不是一个合法的URL");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkMobile($value, $fieldName = '手机号码') {
|
|
|
if (!preg_match('/^1\d{10}$/', $value)) {
|
|
|
throw new CheckClientException("{$fieldName}不正确");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkQQ($value, $fieldName = 'QQ号码') {
|
|
|
if (!preg_match('/^[1-9]\d{4,10}$/', $value)) {
|
|
|
throw new CheckClientException("{$fieldName}不正确");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkEqual($value1, $value2, $message) {
|
|
|
if ($value1 != $value2) {
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkPositiveNumber($value, $fieldName) {
|
|
|
if (!is_numeric($value) || $value <= 0) {
|
|
|
throw new CheckClientException("{$fieldName} 必须大于0");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkIsFailRet($ret) {
|
|
|
if ($ret['result'] == 'fail') {
|
|
|
throw new CheckClientException($ret['reason']);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkIsFalse($value, $message) {
|
|
|
if ($value === false) {
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkNotInArray($needVal, $compareVals, $message) {
|
|
|
if (in_array($needVal, $compareVals)) {
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkInArray($needVal, $compareVals, $message) {
|
|
|
if (!in_array($needVal, $compareVals)) {
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function checkParamsAnyEmpty(array $params, array $fields, $message = null) {
|
|
|
foreach ($fields as $field) {
|
|
|
if (empty($params[$field])) {
|
|
|
$message = $message ?: '[' . $field . ']不能为空';
|
|
|
throw new CheckClientException($message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |