Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 00a53ba0f3 | |||
| 011e0cd9b8 | |||
| a7531432d4 | |||
| 76bd050ada | |||
| 0e6a1b5ba2 | |||
| 10aa70eb03 | |||
| b80833691e | |||
| 4ac84b3edf | |||
| 8e8bd0bd43 | |||
| a1c7907146 | |||
| 3b15bc5e27 | |||
| 5c30ffd40f | |||
| 97c07dabeb | |||
| 2c6d216953 |
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "game-worker/validator",
|
"name": "game-worker/kiri-validator",
|
||||||
"description": "db",
|
"description": "db",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,14 +24,13 @@ class ArrayValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params)) {
|
||||||
if (empty($param) || !is_array($param)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!isset($param[$this->field])) {
|
if (!isset($this->params[$this->field])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!is_array($param[$this->field])) {
|
if (!is_array($this->params[$this->field])) {
|
||||||
return $this->addError("The param :attribute must a array");
|
return $this->addError("The param :attribute must a array");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+11
-8
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
namespace validator;
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveRecord;
|
use Database\Model;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
abstract class BaseValidator
|
abstract class BaseValidator
|
||||||
@@ -22,7 +22,7 @@ abstract class BaseValidator
|
|||||||
|
|
||||||
protected array $params = [];
|
protected array $params = [];
|
||||||
|
|
||||||
protected ?ActiveRecord $model = null;
|
protected ?Model $model = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,9 +34,9 @@ abstract class BaseValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ActiveRecord|null
|
* @return Model|null
|
||||||
*/
|
*/
|
||||||
public function getModel(): ?ActiveRecord
|
public function getModel(): ?Model
|
||||||
{
|
{
|
||||||
return $this->model;
|
return $this->model;
|
||||||
}
|
}
|
||||||
@@ -66,12 +66,12 @@ abstract class BaseValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
throw new Exception('Child Class must define method of trigger');
|
throw new Exception('Child Class must define method of trigger');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,11 +83,14 @@ abstract class BaseValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array|null $data
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setParams(array $data): static
|
public function setParams(?array $data): static
|
||||||
{
|
{
|
||||||
|
if (is_null($data)) {
|
||||||
|
$data = [];
|
||||||
|
}
|
||||||
$this->params = $data;
|
$this->params = $data;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,26 +25,19 @@ class DateTimeValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params)) {
|
||||||
if (empty($param) || !is_array($param)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!isset($param[$this->field]) || empty($param[$this->field])) {
|
if (!isset($this->params[$this->field]) || empty($this->params[$this->field])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$value = $param[$this->field];
|
return match (strtolower($this->method)) {
|
||||||
switch (strtolower($this->method)) {
|
self::DATE => $this->validatorDate($this->params[$this->field]),
|
||||||
case self::DATE:
|
self::DATE_TIME => $this->validateDatetime($this->params[$this->field]),
|
||||||
return $this->validatorDate($value);
|
self::TIME => $this->validatorTime($this->params[$this->field]),
|
||||||
case self::DATE_TIME:
|
self::STR_TO_TIME => $this->validatorTimestamp($this->params[$this->field]),
|
||||||
return $this->validateDatetime($value);
|
default => true,
|
||||||
case self::TIME:
|
};
|
||||||
return $this->validatorTime($value);
|
|
||||||
case self::STR_TO_TIME:
|
|
||||||
return $this->validatorTimestamp($value);
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,16 +19,13 @@ class EmailValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
return true;
|
||||||
|
}
|
||||||
|
if (preg_match('/^[a-zA-Z0-9]+([\.\_]{1,})[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $this->params[$this->field])) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
$value = $param[$this->field];
|
return $this->addError('The param :attribute format error');
|
||||||
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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,26 +30,21 @@ class EmptyValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError(':attribute not exists');
|
return $this->addError(':attribute not exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $param[$this->field];
|
|
||||||
|
|
||||||
switch (strtolower($this->method)) {
|
switch (strtolower($this->method)) {
|
||||||
case self::CAN_NOT_EMPTY:
|
case self::CAN_NOT_EMPTY:
|
||||||
if (strlen($value) < 1) {
|
if (strlen($this->params[$this->field]) < 1) {
|
||||||
return $this->addError('The :attribute can not empty.');
|
return $this->addError('The :attribute can not empty.');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case self::CAN_NOT_NULL:
|
case self::CAN_NOT_NULL:
|
||||||
if ($value === null) {
|
if ($this->params[$this->field] === null) {
|
||||||
return $this->addError('The :attribute can not is null.');
|
return $this->addError('The :attribute can not is null.');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-10
@@ -20,22 +20,15 @@ class EnumValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
return $this->addError('The param :attribute is null');
|
||||||
}
|
}
|
||||||
$value = $param[$this->field];
|
if (is_null($this->params[$this->field])) {
|
||||||
if (is_null($value)) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
return $this->addError('The param :attribute is null');
|
||||||
}
|
}
|
||||||
|
if (!in_array($this->params[$this->field], $this->value)) {
|
||||||
if (!is_array($this->value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!in_array($value, $this->value)) {
|
|
||||||
return $this->addError($this->i());
|
return $this->addError($this->i());
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,20 +24,13 @@ class IntegerValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if ($this->type !== self::MIN && $this->params[$this->field] < $this->value) {
|
||||||
$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.');
|
return $this->addError('The ' . $this->field . ' cannot be less than the default value.');
|
||||||
}
|
}
|
||||||
|
if ($this->type !== self::MAX && $this->params[$this->field] > $this->value) {
|
||||||
if ($this->type !== self::MAX && $value > $this->value) {
|
|
||||||
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
|
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -25,22 +25,17 @@ class LengthValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
if ($this->method != self::MAX_LENGTH) {
|
if ($this->method != self::MAX_LENGTH) {
|
||||||
return $this->addError('The param :attribute not exists');
|
return $this->addError('The param :attribute not exists');
|
||||||
} else {
|
} else {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$value = $param[$this->field];
|
|
||||||
if (is_null($value)) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
|
||||||
}
|
|
||||||
return match (strtolower($this->method)) {
|
return match (strtolower($this->method)) {
|
||||||
self::MAX_LENGTH => $this->maxLength($value),
|
self::MAX_LENGTH => $this->maxLength($this->params[$this->field]),
|
||||||
self::MIN_LENGTH => $this->minLength($value),
|
self::MIN_LENGTH => $this->minLength($this->params[$this->field]),
|
||||||
default => $this->defaultLength($value),
|
default => $this->defaultLength($this->params[$this->field]),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,11 +19,7 @@ class RequiredValidator extends BaseValidator
|
|||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
if (!isset($this->params[$this->field])) {
|
||||||
if (is_numeric($param)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError('The param :attribute not exists');
|
return $this->addError('The param :attribute not exists');
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -46,21 +46,13 @@ class TypesOfValidator extends BaseValidator
|
|||||||
if (!in_array($this->method, $this->types)) {
|
if (!in_array($this->method, $this->types)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (empty($this->params) || !isset($this->params[$this->field])) {
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if ($this->params[$this->field] === null) {
|
||||||
$value = $param[$this->field];
|
|
||||||
|
|
||||||
$method = $this->method . 'Format';
|
|
||||||
|
|
||||||
if ($value === null) {
|
|
||||||
return $this->addError('This ' . $this->field . ' is not an empty data.');
|
return $this->addError('This ' . $this->field . ' is not an empty data.');
|
||||||
}
|
}
|
||||||
|
return $this->{$this->method . 'Format'}($this->params[$this->field]);
|
||||||
return $this->{$method}($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,12 +28,10 @@ class UniqueValidator extends BaseValidator
|
|||||||
if (empty($this->model)) {
|
if (empty($this->model)) {
|
||||||
return $this->addError('Model error.');
|
return $this->addError('Model error.');
|
||||||
}
|
}
|
||||||
|
if (!$this->model->getIsNowExample()) {
|
||||||
$model = $this->model;
|
|
||||||
if (!$this->model->getIsCreate()) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($model::find()->where([$this->field => $param[$this->field]])->exists()) {
|
if ($this->model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
||||||
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
||||||
}
|
}
|
||||||
return $this->isFail = TRUE;
|
return $this->isFail = TRUE;
|
||||||
|
|||||||
+22
-22
@@ -34,7 +34,7 @@ class Validator extends BaseValidator
|
|||||||
'required' => [
|
'required' => [
|
||||||
'class' => 'validator\RequiredValidator',
|
'class' => 'validator\RequiredValidator',
|
||||||
],
|
],
|
||||||
'enums' => [
|
'enum' => [
|
||||||
'class' => 'validator\EnumValidator',
|
'class' => 'validator\EnumValidator',
|
||||||
],
|
],
|
||||||
'unique' => [
|
'unique' => [
|
||||||
@@ -102,8 +102,8 @@ class Validator extends BaseValidator
|
|||||||
'class' => 'validator\LengthValidator',
|
'class' => 'validator\LengthValidator',
|
||||||
'method' => 'default',
|
'method' => 'default',
|
||||||
],
|
],
|
||||||
'round' => [
|
'round' => [
|
||||||
'class' => 'validator\RoundValidator',
|
'class' => 'validator\RoundValidator',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -150,33 +150,28 @@ class Validator extends BaseValidator
|
|||||||
public function createRule($field, $rule, $model, $param)
|
public function createRule($field, $rule, $model, $param)
|
||||||
{
|
{
|
||||||
$define = ['field' => $field];
|
$define = ['field' => $field];
|
||||||
foreach ($rule as $key => $val) {
|
|
||||||
|
|
||||||
if (!is_null($model)) {
|
$is_model = is_null($model);
|
||||||
|
foreach ($rule as $key => $val) {
|
||||||
|
if (!$is_model) {
|
||||||
if (is_string($val) && method_exists($model, $val)) {
|
if (is_string($val) && method_exists($model, $val)) {
|
||||||
$this->validators[] = [$model, $val];
|
$this->validators[] = [$model, $val];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($key)) {
|
if (is_string($key)) {
|
||||||
$type = strtolower($key);
|
$type = strtolower($key);
|
||||||
$define['value'] = $val;
|
$define['value'] = $val;
|
||||||
} else {
|
} else {
|
||||||
$type = strtolower($val);
|
$type = strtolower($val);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($this->classMap[$type])) {
|
if (!isset($this->classMap[$type])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$constr = array_merge($this->classMap[$type], $define);
|
$this->validators[] = array_merge($this->classMap[$type], $define, [
|
||||||
|
'params' => $param,
|
||||||
/** @var BaseValidator $class */
|
'model' => $model
|
||||||
$class = Kiri::createObject($constr);
|
]);
|
||||||
$class->setParams($param);
|
|
||||||
$class->setModel($model);
|
|
||||||
|
|
||||||
$this->validators[] = $class;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,18 +184,17 @@ class Validator extends BaseValidator
|
|||||||
if (count($this->validators) < 1) {
|
if (count($this->validators) < 1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->validators as $val) {
|
foreach ($this->validators as $val) {
|
||||||
if ($this->check($val)) {
|
[$result, $validator] = $this->check($val);
|
||||||
|
if ($result === true) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$isTrue = false;
|
$isTrue = false;
|
||||||
if ($val instanceof BaseValidator) {
|
if ($validator instanceof BaseValidator) {
|
||||||
$this->addError($val->getError());
|
$this->addError($validator->getError());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$this->validators = null;
|
|
||||||
$this->validators = [];
|
$this->validators = [];
|
||||||
return !isset($isTrue);
|
return !isset($isTrue);
|
||||||
}
|
}
|
||||||
@@ -213,9 +207,15 @@ class Validator extends BaseValidator
|
|||||||
private function check(BaseValidator|array|Closure $val): mixed
|
private function check(BaseValidator|array|Closure $val): mixed
|
||||||
{
|
{
|
||||||
if (is_callable($val, true)) {
|
if (is_callable($val, true)) {
|
||||||
return call_user_func($val, $this);
|
return [call_user_func($val, $this), $val];
|
||||||
}
|
}
|
||||||
return $val->trigger();
|
|
||||||
|
$class = Kiri::getDi()->get($val['class']);
|
||||||
|
unset($val['class']);
|
||||||
|
|
||||||
|
Kiri::configure($class, $val);
|
||||||
|
|
||||||
|
return [$class->trigger(), $class];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user