This commit is contained in:
2020-08-31 12:38:32 +08:00
parent 683a39dded
commit 65c443ec87
126 changed files with 7369 additions and 228 deletions
+65
View File
@@ -0,0 +1,65 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 15:28
*/
namespace validator;
class ArrayValidator extends BaseValidator
{
/**
* @return array|bool
*
* 检查
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !is_array($param)) {
return true;
}
if (!isset($param[$this->field])) {
return true;
}
if (!is_array($param[$this->field])) {
return $this->addError("The param :attribute must a array");
}
return $this->toArray($param[$this->field]);
}
/**
* @param $data
* @return array
*
* 转成数组
*/
private function toArray($data)
{
if (is_numeric($data)) {
return [];
} else if (is_null(json_decode($data, true))) {
return [];
} elseif (is_object($data)) {
$data = get_object_vars($data);
}
$_tmp = [];
foreach ($data as $key => $val) {
if (is_object($val)) {
$_tmp[$key] = $this->toArray($val);
} else if (is_array($val)) {
$_tmp[$key] = $this->toArray($val);
} else {
$_tmp[$key] = $val;
}
}
return $_tmp;
}
}
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace validator;
use Database\ActiveRecord;
abstract class BaseValidator
{
public $field;
public $rules;
public $method;
protected $isFail = TRUE;
protected $message = '';
protected $params = [];
/** @var ActiveRecord */
protected $model;
public function setModel($model)
{
$this->model = $model;
}
/**
* @return ActiveRecord
*/
public function getModel()
{
return $this->model;
}
public function __construct($config = [])
{
$this->regConfig($config);
}
private function regConfig($config)
{
if (empty($config) || !is_array($config)) {
return;
}
foreach ($config as $key => $val) {
$this->$key = $val;
}
}
/**
* @throws \Exception
* @return bool
*/
public function trigger()
{
throw new \Exception('Child Class must define method of trigger');
}
/**
* @return mixed
*/
protected function getParams()
{
return $this->params;
}
/**
* @param $data
* @return $this
*/
public function setParams($data)
{
$this->params = $data;
return $this;
}
/**
* @param $message
* @return bool
*/
public function addError($message)
{
$this->isFail = FALSE;
$message = str_replace(':attribute', $this->field, $message);
$this->message = $message;
return $this->isFail;
}
/**
* @return string
*/
public function getError()
{
return $this->message;
}
/**
* @param $name
* @param $value
* @throws \Exception
*/
public function __set($name, $value)
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->$method($value);
} else if (property_exists($this, $name)) {
$this->$name = $value;
} else {
throw new \Exception('unknown property ' . $name . ' in class ' . get_called_class());
}
}
}
+131
View File
@@ -0,0 +1,131 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 15:42
*/
namespace validator;
class DateTimeValidator extends BaseValidator
{
const DATE = 'date';
const DATE_TIME = 'datetime';
const TIME = 'time';
const STR_TO_TIME = 'timestamp';
public $method;
/**
* @return bool
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !is_array($param)) {
return true;
}
if (!isset($param[$this->field]) || empty($param[$this->field])) {
return true;
}
$value = $param[$this->field];
switch (strtolower($this->method)) {
case self::DATE:
return $this->validatorDate($value);
break;
case self::DATE_TIME:
return $this->validateDatetime($value);
break;
case self::TIME:
return $this->validatorTime($value);
break;
case self::STR_TO_TIME:
return $this->validatorTimestamp($value);
break;
default:
return true;
}
}
/**
* @param $value
* @return bool
*
* 效验分秒 格式如 01:02 or 01-02
*/
public function validatorTime($value)
{
if (empty($value) || !is_string($value)) {
return $this->addError('The param :attribute not is a date value');
}
$match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result);
if ($match && $result[0] == $value) {
return true;
} else {
return $this->addError('The param :attribute format error');
}
}
/**
* @param $value
* @return bool
*
* 效验分秒 格式如 2017-12-22 01:02
*/
public function validateDatetime($value)
{
if (empty($value) || !is_string($value)) {
return $this->addError('The param :attribute not is a date value');
}
$match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/';
$match = preg_match($match, $value, $result);
if ($match && $result[0] == $value) {
return true;
} else {
return $this->addError('The param :attribute format error');
}
}
/**
* @param $value
* @return bool
*
* 效验分秒 格式如 2017-12-22
*/
public function validatorDate($value)
{
if (empty($value) || !is_string($value)) {
return $this->addError('The param :attribute not is a date value');
}
$match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result);
if ($match && $result[0] == $value) {
return true;
} else {
return $this->addError('The param :attribute format error');
}
}
/**
* @param $value
* @return bool
*
* 效验时间戳 格式如 1521452254
*/
public function validatorTimestamp($value)
{
if (empty($value) || !is_numeric($value)) {
return $this->addError('The param :attribute not is a timestamp value');
}
if (strlen((string)$value) != 10) {
return $this->addError('The param :attribute not is a timestamp value');
}
if (!date('YmdHis', $value)) {
return $this->addError('The param :attribute format error');
}
return true;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/20 0020
* Time: 17:32
*/
namespace validator;
class EmailValidator extends BaseValidator
{
/**
* @return bool
* 检查是否存在
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return true;
} else {
$value = $param[$this->field];
if (preg_match('/^[a-zA-Z0-9]+([\.\_]{1,})[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $value)) {
return true;
} else {
return $this->addError('The param :attribute format error');
}
}
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 15:46
*/
namespace validator;
class EmptyValidator extends BaseValidator
{
/** @var string [不能为空] */
const CAN_NOT_EMPTY = 'not empty';
/** @var string [可为空, 不能为null] */
const CAN_NOT_NULL = 'not null';
public $method;
/**
* @return bool
*
* 检查参数是否为NULL
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return $this->addError(':attribute not exists');
}
$value = $param[$this->field];
switch (strtolower($this->method)) {
case self::CAN_NOT_EMPTY:
if (strlen($value) < 1) {
return $this->addError('The :attribute can not empty.');
}
break;
case self::CAN_NOT_NULL:
if ($value === null) {
return $this->addError('The :attribute can not is null.');
}
break;
}
return true;
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace validator;
/**
* Class EnumValidator
* @package validator
*/
class EnumValidator extends BaseValidator
{
public $value = [];
/**
* @return bool
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return $this->addError('The param :attribute is null');
}
$value = $param[$this->field];
if (is_null($value)) {
return $this->addError('The param :attribute is null');
}
if (!is_array($this->value)) {
return true;
}
if (!in_array($value, $this->value)) {
return $this->addError($this->i());
}
return true;
}
/**
* @return string
*/
private function i()
{
return 'The param :attribute value only in ' . implode(',', $this->value);
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 18:44
*/
namespace validator;
class IntegerValidator extends BaseValidator
{
const MIN = 'min';
const MAX = 'max';
public $value = null;
private $type = '';
/**
* @return bool
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return true;
}
$value = $param[$this->field] ?? null;
if ($value === null) {
return $this->addError('The :attribute can not is null.');
}
if ($this->type !== self::MIN && $value < $this->value) {
return $this->addError('The ' . $this->field . ' cannot be less than the default value.');
}
if ($this->type !== self::MAX && $value > $this->value) {
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
}
return true;
}
}
+119
View File
@@ -0,0 +1,119 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 17:04
*/
namespace validator;
class LengthValidator extends BaseValidator
{
const MAX_LENGTH = 'max';
const MIN_LENGTH = 'min';
public $method;
public $value;
/**
* @return bool
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
if ($this->method != self::MAX_LENGTH) {
return $this->addError('The param :attribute not exists');
} else {
return TRUE;
}
}
$value = $param[$this->field];
if (is_null($value)) {
return $this->addError('The param :attribute is null');
}
switch (strtolower($this->method)) {
case self::MAX_LENGTH:
return $this->maxLength($value);
break;
case self::MIN_LENGTH:
return $this->minLength($value);
break;
default:
return $this->defaultLength($value);
}
}
/**
* @param $value
* @return bool
*
* 效验长度是否大于最大长度
*/
private function maxLength($value)
{
if (is_array($value)) {
if (count($value) > $value) {
return $this->addError('The param :attribute length overflow');
}
} else {
if (is_numeric($value) && strlen(floatval($value)) > $this->value) {
return $this->addError('The param :attribute length overflow');
}
if (strlen($value) > $this->value) {
return $this->addError('The param :attribute length overflow');
}
}
return TRUE;
}
/**
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
private function minLength($value)
{
if (is_array($value)) {
if (count($value) < $value) {
return $this->addError('The param :attribute length error');
}
} else {
if (is_numeric($value) && strlen(floatval($value)) < $this->value) {
return $this->addError('The param :attribute length overflow');
}
if (strlen($value) < $this->value) {
return $this->addError('The param :attribute length error');
}
}
return TRUE;
}
/**
* @param $value
* @return bool
*
* 效验长度是否小于最小长度
*/
private function defaultLength($value)
{
if (is_array($value)) {
if (count($value) !== $value) {
return $this->addError('The param :attribute length error');
}
} else {
if (is_numeric($value) && mb_strlen(floatval($value)) !== $this->value) {
return $this->addError('The param :attribute length overflow');
}
if (mb_strlen($value) !== $this->value) {
return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value);
}
}
return TRUE;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/3 0003
* Time: 15:47
*/
namespace validator;
class RequiredValidator extends BaseValidator
{
/**
* @return bool
* 检查是否存在
*/
public function trigger()
{
$param = $this->getParams();
if (is_numeric($param)) {
return true;
}
if (empty($param) || !isset($param[$this->field])) {
return $this->addError('The param :attribute not exists');
} else {
return true;
}
}
}
+149
View File
@@ -0,0 +1,149 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 18:44
*/
namespace validator;
class TypesOfValidator extends BaseValidator
{
const JSON = 'json';
const FLOAT = 'float';
const ARRAY = 'array';
const STRING = 'string';
const INTEGER = 'integer';
const SERIALIZE = 'serialize';
private $min = null;
private $max = null;
/** @var array */
public $types = [
self::JSON => 'json',
self::FLOAT => 'float',
self::ARRAY => 'array',
self::STRING => 'string',
self::INTEGER => 'integer',
self::SERIALIZE => 'serialize',
];
/** @var string */
public $method;
/**
* @return bool
*/
public function trigger()
{
if (!in_array($this->method, $this->types)) {
return true;
}
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return true;
}
$value = $param[$this->field];
$method = $this->method . 'Format';
if ($value === null) {
return $this->addError('This ' . $this->field . ' is not an empty data.');
}
return $this->{$method}($value);
}
/**
* @param $value
* @return bool
*/
public function jsonFormat($value)
{
if (!is_string($value) || is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is JSON data.');
}
if (is_null(json_decode($value))) {
return $this->addError('The ' . $this->field . ' not is JSON data.');
}
return true;
}
/**
* @param $value
* @return bool
*/
public function serializeFormat($value)
{
if (!is_string($value) || is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.');
}
if (false === unserialize($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.');
}
return true;
}
/**
* @param $value
* @return bool
*/
public function arrayFormat($value)
{
if (!is_array($value)) {
return $this->addError('The ' . $this->field . ' not is array data.');
}
return true;
}
/**
* @param $value
* @return bool
*/
public function stringFormat($value)
{
if (is_array($value) || is_object($value) || is_bool($value)) {
return $this->addError('The ' . $this->field . ' not is string data.');
}
return true;
}
/**
* @param $value
* @return bool
*/
public function integerFormat($value)
{
$trim = trim($value);
if (!is_numeric($trim)) {
return $this->addError('The ' . $this->field . ' not is number data.');
}
if ($trim != $value || intval($trim) != $value) {
return $this->addError('The ' . $this->field . ' not is number data.');
}
return true;
}
/**
* @param $value
* @return bool
*/
public function floatFormat($value)
{
$trim = floatval(trim($value));
if ($trim != $value || !is_float($trim)) {
return $this->addError('The ' . $this->field . ' not is float data.');
}
return true;
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: qv
* Date: 2018/10/16 0016
* Time: 10:24
*/
namespace validator;
class UniqueValidator extends BaseValidator
{
/**
* @return bool
* @throws
* 检查是否存在
*/
public function trigger()
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return TRUE;
}
if (empty($this->model)) {
return $this->addError('Model error.');
}
$model = $this->model;
if (!$this->model->getIsCreate()) {
return true;
}
if ($model::find()->where([$this->field => $param[$this->field]])->exists()) {
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
}
return $this->isFail = TRUE;
}
}
+208
View File
@@ -0,0 +1,208 @@
<?php
namespace validator;
/**
* Class Validator
* @package validator
*/
class Validator extends BaseValidator
{
/** @var BaseValidator[] */
private $validators = [];
/** @var Validator */
private static $instance = null;
protected $classMap = [
'not empty' => [
'class' => 'validator\EmptyValidator',
'method' => EmptyValidator::CAN_NOT_EMPTY,
],
'not null' => [
'class' => 'validator\EmptyValidator',
'method' => EmptyValidator::CAN_NOT_NULL,
],
'required' => [
'class' => 'validator\RequiredValidator',
],
'enums' => [
'class' => 'validator\EnumValidator',
],
'unique' => [
'class' => 'validator\UniqueValidator',
],
'datetime' => [
'class' => 'validator\DatetimeValidator',
'method' => DateTimeValidator::DATE_TIME,
],
'date' => [
'class' => 'validator\DatetimeValidator',
'method' => DateTimeValidator::DATE,
],
'time' => [
'class' => 'validator\DatetimeValidator',
'method' => DateTimeValidator::TIME,
],
'timestamp' => [
'class' => 'validator\DatetimeValidator',
'method' => DateTimeValidator::STR_TO_TIME,
],
'string' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::STRING,
],
'int' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::INTEGER,
],
'min' => [
'class' => IntegerValidator::class
],
'max' => [
'class' => IntegerValidator::class
],
'json' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::JSON,
],
'float' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::FLOAT,
],
'array' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::ARRAY,
],
'serialize' => [
'class' => 'validator\TypesOfValidator',
'method' => TypesOfValidator::SERIALIZE,
],
'maxLength' => [
'class' => 'validator\LengthValidator',
'method' => 'max',
],
'minLength' => [
'class' => 'validator\LengthValidator',
'method' => 'min',
],
'email' => [
'class' => 'validator\EmailValidator',
'method' => 'email',
],
'length' => [
'class' => 'validator\LengthValidator',
'method' => 'default',
],
];
/**
* @return Validator
*/
public static function getInstance()
{
if (static::$instance == null) {
static::$instance = new Validator();
}
return static::$instance;
}
/**
* @param $field
* @param $rules
* @return $this
* @throws \Exception
*/
public function make($field, $rules)
{
if (!is_array($field)) {
$field = [$field];
}
$param = $this->getParams();
$model = $this->getModel();
foreach ($field as $val) {
$this->createRule($val, $rules, $model, $param);
}
return $this;
}
/**
* @param $field
* @param $rule
* @param $model
* @param $param
* @throws \Exception
* ['maxLength'=>150, 'required', 'minLength' => 100]
*/
public function createRule($field, $rule, $model, $param)
{
$define = ['field' => $field];
foreach ($rule as $key => $val) {
if (is_string($val) && method_exists($model, $val)) {
$this->validators[] = [$model, $val];
continue;
}
if (is_string($key)) {
$type = strtolower($key);
$define['value'] = $val;
} else {
$type = strtolower($val);
}
if (!isset($this->classMap[$type])) {
continue;
}
$constr = array_merge($this->classMap[$type], $define);
/** @var BaseValidator $class */
$class = Snowflake::createObject($constr);
$class->setParams($param);
$class->setModel($model);
$this->validators[] = $class;
}
}
/**
* @return bool
* @throws \Exception
*/
public function validation()
{
if (count($this->validators) < 1) {
return true;
}
foreach ($this->validators as $val) {
if ($this->check($val)) {
continue;
}
$isTrue = false;
if ($val instanceof BaseValidator) {
$this->addError($val->getError());
}
break;
}
$this->validators = [];
return !isset($isTrue);
}
/**
* @param BaseValidator $val
* @return mixed
* @throws
*/
private function check($val)
{
if (is_callable($val, true)) {
return call_user_func($val, $this);
}
return $val->trigger();
}
}