From 0547948b3bf5f8d6a5e5d4166db22f92fd93131d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 12 Sep 2023 20:52:18 +0800 Subject: [PATCH] eee --- TypesOfValidator.php | 158 +++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 87 deletions(-) diff --git a/TypesOfValidator.php b/TypesOfValidator.php index 93ddf13..5e4cd55 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -14,128 +14,112 @@ class TypesOfValidator extends BaseValidator { - const JSON = 'json'; - const FLOAT = 'float'; - const ARRAY = 'array'; - const STRING = 'string'; - const INTEGER = 'integer'; - const SERIALIZE = 'serialize'; + const JSON = 'json'; + const FLOAT = 'float'; + const ARRAY = 'array'; + const STRING = 'string'; + const INTEGER = 'integer'; - private ?int $min = null; - private ?int $max = null; + private ?int $min = null; + private ?int $max = null; - /** @var array */ - public array $types = [ - self::JSON => 'json', - self::FLOAT => 'float', - self::ARRAY => 'array', - self::STRING => 'string', - self::INTEGER => 'integer', - self::SERIALIZE => 'serialize', - ]; + /** @var array */ + public array $types = [ + self::JSON => 'json', + self::FLOAT => 'float', + self::ARRAY => 'array', + self::STRING => 'string', + self::INTEGER => 'integer' + ]; - /** @var string */ - public string $method; + /** @var string */ + public string $method; - /** - * @return bool - */ - public function trigger(): bool - { - return $this->_validator($this->field, function ($field, $params, $method, $types) { - if (!in_array($method, $types)) { - return true; - } - $value = $params[$field] ?? null; - - return $this->{$method . 'Format'}($field, $value); - }, $this->params, $this->method, $this->types); - } + /** + * @return bool + */ + public function trigger(): bool + { + return $this->_validator($this->field, function ($field, $params, $method, $types) { + if (!in_array($method, $types)) { + return true; + } + $value = $params[$field] ?? null; + return match ($method) { + self::INTEGER => $this->integerFormat($field, $value), + self::FLOAT => $this->floatFormat($field, $value), + self::JSON => $this->jsonFormat($field, $value), + self::STRING => $this->stringFormat($field, $value), + self::ARRAY => $this->arrayFormat($field, $value), + }; + }, $this->params, $this->method, $this->types); + } /** * @param $field * @param $value * @return bool - * @throws \ReflectionException */ - public function jsonFormat($field, $value): bool - { - if (is_null(json_decode($value))) { - return $this->addError($field, 'The ' . $field . ' not is JSON data.'); - } - return true; - } + public function jsonFormat($field, $value): bool + { + if (is_null(json_decode($value))) { + return $this->addError($field, 'The ' . $field . ' not is JSON data.'); + } + return true; + } /** * @param $field * @param $value * @return bool - * @throws \ReflectionException */ - public function serializeFormat($field, $value): bool - { - if (false === unserialize($value)) { - return $this->addError($field, 'The ' . $field . ' not is serialize data.'); - } - return true; - } + public function arrayFormat($field, $value): bool + { + if (!is_array($value)) { + return $this->addError($field, 'The ' . $field . ' not is array data.'); + } + return true; + } /** * @param $field * @param $value * @return bool - * @throws \ReflectionException */ - public function arrayFormat($field, $value): bool - { - if (!is_array($value)) { - return $this->addError($field, 'The ' . $field . ' not is array data.'); - } - return true; - } - - /** - * @param $field - * @param $value - * @return bool - * @throws \ReflectionException - */ - public function stringFormat($field, $value): bool - { + public function stringFormat($field, $value): bool + { if (!is_string($value)) { - return $this->addError($field, 'The ' . $field . ' not is string data.'); - } - return true; - } + return $this->addError($field, 'The ' . $field . ' not is string data.'); + } + return true; + } /** * @param $field * @param $value * @return bool - * @throws \ReflectionException */ - public function integerFormat($field, $value): bool - { - if ((int)$value != $value) { - return $this->addError($field, 'The ' . $field . ' not is number data.'); - } - return true; - } + public function integerFormat($field, $value): bool + { + if ((int)$value != $value) { + return $this->addError($field, 'The ' . $field . ' not is number data.'); + } + return true; + } /** * @param $field * @param $value * @return bool - * @throws \ReflectionException */ - public function floatFormat($field, $value): bool - { - $trim = (float)$value; - if ($trim != $value) { - return $this->addError($field, 'The ' . $field . ' not is float data.'); - } - return true; - } + public function floatFormat($field, $value): bool + { + $trim = (float)$value; + if ($trim != $value) { + return $this->addError($field, 'The ' . $field . ' not is float data.'); + } + return true; + } }