From 7eccee5d1626d604c179039c50a8bc3e2182db27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 7 Jul 2021 18:36:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gii/GiiModel.php | 26 +++++++++++++++----------- Validator/BaseValidator.php | 2 +- Validator/LengthValidator.php | 13 +++++-------- Validator/RoundValidator.php | 34 ++++++++++++++++++++++++++++++++++ Validator/Validator.php | 3 +++ 5 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 Validator/RoundValidator.php diff --git a/Gii/GiiModel.php b/Gii/GiiModel.php index d60b8b8e..10f339d1 100644 --- a/Gii/GiiModel.php +++ b/Gii/GiiModel.php @@ -22,12 +22,12 @@ class GiiModel extends GiiBase public ?array $fields; /** - * ModelFile constructor. - * @param $classFileName - * @param $tableName - * @param $visible - * @param $res - * @param $fields + * GiiModel constructor. + * @param string $classFileName + * @param string $tableName + * @param array $visible + * @param array $res + * @param array $fields */ public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields) { @@ -308,16 +308,20 @@ use Database\ActiveRecord; if (empty($data)) return ''; $string = []; foreach ($data as $key => $_val) { - if (is_string($key) && str_contains($key, ',')) { - $key = '[' . $key . ']'; + if (in_array($_val[0][1], $this->type['float'])) { + $e_x = explode(',', $key); + $key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1); + } else if (is_string($key) && str_contains($key, ',')) { + $key = '\'between\' => [' . $key . ']'; + } else { + $key = '\'maxLength\' => ' . $key; } if (count($_val) == 1) { - [$typeRule, $type, $rule, $field] = current($_val); $_tmp = ' - [\'' . $field . '\', \'' . ($type == 'enum' ? 'enum' : 'maxLength') . '\' => ' . $key . ']'; + [\'' . $_val[3] . '\', ' . ($_val[1] == 'enum' ? '\'enum\' => ' . $key : $key) . ']'; } else { $_tmp = ' - [[\'' . implode('\', \'', array_column($_val, 3)) . '\'], \'maxLength\' => ' . $key . ']'; + [[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . $key . ']'; } $string[] = $_tmp; } diff --git a/Validator/BaseValidator.php b/Validator/BaseValidator.php index ebcb04b8..3121fa1f 100644 --- a/Validator/BaseValidator.php +++ b/Validator/BaseValidator.php @@ -46,7 +46,7 @@ abstract class BaseValidator * BaseValidator constructor. * @param array $config */ - public function __construct($config = []) + public function __construct(array $config = []) { $this->regConfig($config); } diff --git a/Validator/LengthValidator.php b/Validator/LengthValidator.php index 616e0a19..9b5ef67a 100644 --- a/Validator/LengthValidator.php +++ b/Validator/LengthValidator.php @@ -37,14 +37,11 @@ class LengthValidator extends BaseValidator if (is_null($value)) { return $this->addError('The param :attribute is null'); } - switch (strtolower($this->method)) { - case self::MAX_LENGTH: - return $this->maxLength($value); - case self::MIN_LENGTH: - return $this->minLength($value); - default: - return $this->defaultLength($value); - } + return match (strtolower($this->method)) { + self::MAX_LENGTH => $this->maxLength($value), + self::MIN_LENGTH => $this->minLength($value), + default => $this->defaultLength($value), + }; } /** diff --git a/Validator/RoundValidator.php b/Validator/RoundValidator.php new file mode 100644 index 00000000..66fa63f8 --- /dev/null +++ b/Validator/RoundValidator.php @@ -0,0 +1,34 @@ +model->getAttribute($this->field); + if ($value == null || round($value, $this->value) != $value) { + return $this->addError('The param :attribute length error'); + } + return true; + } + + +} diff --git a/Validator/Validator.php b/Validator/Validator.php index 027b1cd0..dba43ff8 100644 --- a/Validator/Validator.php +++ b/Validator/Validator.php @@ -102,6 +102,9 @@ class Validator extends BaseValidator 'class' => 'validator\LengthValidator', 'method' => 'default', ], + 'round' => [ + 'class' => 'validator\RoundValidator', + ], ]; /**