Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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",
|
||||
"authors": [
|
||||
{
|
||||
|
||||
+10
-7
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
namespace validator;
|
||||
|
||||
|
||||
use Database\ActiveRecord;
|
||||
use Database\Model;
|
||||
use Exception;
|
||||
|
||||
abstract class BaseValidator
|
||||
@@ -22,7 +22,7 @@ abstract class BaseValidator
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -66,8 +66,8 @@ abstract class BaseValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger(): bool
|
||||
{
|
||||
@@ -83,11 +83,14 @@ abstract class BaseValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array|null $data
|
||||
* @return $this
|
||||
*/
|
||||
public function setParams(array $data): static
|
||||
public function setParams(?array $data): static
|
||||
{
|
||||
if (is_null($data)) {
|
||||
$data = [];
|
||||
}
|
||||
$this->params = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -20,10 +20,7 @@ class RequiredValidator extends BaseValidator
|
||||
public function trigger(): bool
|
||||
{
|
||||
$param = $this->getParams();
|
||||
if (is_numeric($param)) {
|
||||
return true;
|
||||
}
|
||||
if (empty($param) || !isset($param[$this->field])) {
|
||||
if (!is_array($param) || !isset($param[$this->field])) {
|
||||
return $this->addError('The param :attribute not exists');
|
||||
} else {
|
||||
return true;
|
||||
|
||||
@@ -30,10 +30,10 @@ class UniqueValidator extends BaseValidator
|
||||
}
|
||||
|
||||
$model = $this->model;
|
||||
if (!$this->model->getIsCreate()) {
|
||||
if (!$this->model->getIsNowExample()) {
|
||||
return true;
|
||||
}
|
||||
if ($model::find()->where([$this->field => $param[$this->field]])->exists()) {
|
||||
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
||||
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
||||
}
|
||||
return $this->isFail = TRUE;
|
||||
|
||||
+21
-19
@@ -34,7 +34,7 @@ class Validator extends BaseValidator
|
||||
'required' => [
|
||||
'class' => 'validator\RequiredValidator',
|
||||
],
|
||||
'enums' => [
|
||||
'enum' => [
|
||||
'class' => 'validator\EnumValidator',
|
||||
],
|
||||
'unique' => [
|
||||
@@ -150,33 +150,29 @@ class Validator extends BaseValidator
|
||||
public function createRule($field, $rule, $model, $param)
|
||||
{
|
||||
$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)) {
|
||||
$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 = Kiri::createObject($constr);
|
||||
$class->setParams($param);
|
||||
$class->setModel($model);
|
||||
|
||||
$this->validators[] = $class;
|
||||
$constr = array_merge($this->classMap[$type], $define, [
|
||||
'params' => $param,
|
||||
'model' => $model
|
||||
]);
|
||||
$this->validators[] = $constr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,14 +185,14 @@ class Validator extends BaseValidator
|
||||
if (count($this->validators) < 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($this->validators as $val) {
|
||||
if ($this->check($val)) {
|
||||
[$result, $validator] = $this->check($val);
|
||||
if ($result === true) {
|
||||
continue;
|
||||
}
|
||||
$isTrue = false;
|
||||
if ($val instanceof BaseValidator) {
|
||||
$this->addError($val->getError());
|
||||
if ($validator instanceof BaseValidator) {
|
||||
$this->addError($validator->getError());
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -213,9 +209,15 @@ class Validator extends BaseValidator
|
||||
private function check(BaseValidator|array|Closure $val): mixed
|
||||
{
|
||||
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