7 Commits

Author SHA1 Message Date
as2252258 8e8bd0bd43 改名 2021-11-10 11:27:38 +08:00
as2252258 a1c7907146 改名 2021-11-10 11:25:18 +08:00
as2252258 3b15bc5e27 update composer.json. 2021-11-05 07:21:51 +00:00
as2252258 5c30ffd40f 改名 2021-10-25 16:39:56 +08:00
as2252258 97c07dabeb 改名 2021-10-09 16:48:19 +08:00
as2252258 2c6d216953 validator 2021-09-29 11:24:46 +08:00
as2252258 eb7f65f844 validator 2021-08-26 16:17:21 +08:00
4 changed files with 25 additions and 26 deletions
+2 -4
View File
@@ -1,5 +1,5 @@
{ {
"name": "game-worker/validator", "name": "game-worker/kiri-validator",
"description": "db", "description": "db",
"authors": [ "authors": [
{ {
@@ -11,8 +11,7 @@
"require": { "require": {
"php": ">=8.0", "php": ">=8.0",
"ext-json": "*", "ext-json": "*",
"ext-pdo": "*", "ext-pdo": "*"
"game-worker/snowflake": "dev-master"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@@ -20,6 +19,5 @@
} }
}, },
"require-dev": { "require-dev": {
"kwn/php-rdkafka-stubs": "^2.0"
} }
} }
+4 -4
View File
@@ -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;
} }
+1 -1
View File
@@ -33,7 +33,7 @@ class UniqueValidator extends BaseValidator
if (!$this->model->getIsCreate()) { if (!$this->model->getIsCreate()) {
return true; 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->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
} }
return $this->isFail = TRUE; return $this->isFail = TRUE;
+18 -17
View File
@@ -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,29 @@ 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); $constr = array_merge($this->classMap[$type], $define, [
'params' => $param,
/** @var BaseValidator $class */ 'model' => $model
$class = Kiri::createObject($constr); ]);
$class->setParams($param); $this->validators[] = $constr;
$class->setModel($model);
$this->validators[] = $class;
} }
} }
@@ -189,7 +185,6 @@ 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)) { if ($this->check($val)) {
continue; continue;
@@ -215,7 +210,13 @@ class Validator extends BaseValidator
if (is_callable($val, true)) { if (is_callable($val, true)) {
return call_user_func($val, $this); return call_user_func($val, $this);
} }
return $val->trigger();
$class = Kiri::getDi()->get($val['class']);
unset($val['class']);
Kiri::configure($class, $val);
return $class->trigger();
} }
} }