From 401d6d9d3f795a099eb180cc1041b3fcbdb0804d Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Wed, 22 Jan 2025 14:08:47 +0800 Subject: [PATCH] * [misc] move helper::convertType method to base. --- framework/base/helper.class.php | 45 +++++++++++++++++++++++++-------- framework/helper.class.php | 33 ------------------------ 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/framework/base/helper.class.php b/framework/base/helper.class.php index 42f8a7c702..cb9a38ead9 100644 --- a/framework/base/helper.class.php +++ b/framework/base/helper.class.php @@ -1134,6 +1134,39 @@ class baseHelper throw new Exception('Can not connect to Redis server. The error message is: ' . $e->getMessage()); } } + + /** + * 转换类型。 + * Convert the type. + * + * @param mixed $value + * @param string $type + * @static + * @access public + * @return array|bool|float|int|object|string + */ + public static function convertType($value, $type) + { + switch($type) + { + case 'int': + return (int)$value; + case 'float': + return (float)$value; + case 'bool': + return (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + case 'array': + return array_filter((array)$value, function($var){return ($var === '0' || !empty($var));}); + case 'object': + return (object)$value; + case 'datetime': + case 'date': + return $value ? trim((string)$value) : null; + case 'string': + default: + return trim((string)$value); + } + } } //------------------------------- 常用函数。Some tool functions.-------------------------------// @@ -1333,17 +1366,7 @@ function getEnvData($name, $default = '', $format = 'string') $value = getenv($name); if($value === false) $value = $default; - switch($format) - { - case 'int': - return (int)$value; - case 'float': - return (float)$value; - case 'bool': - return (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); - default: - return (string)$value; - } + return helper::convertType($value, $format); } if(!function_exists('array_column')) diff --git a/framework/helper.class.php b/framework/helper.class.php index 724067689b..adfd8ae665 100644 --- a/framework/helper.class.php +++ b/framework/helper.class.php @@ -345,39 +345,6 @@ class helper extends baseHelper return $dateInterval; } - /** - * 转换类型。 - * Convert the type. - * - * @param mixed $value - * @param string $type - * @static - * @access public - * @return array|bool|float|int|object|string - */ - public static function convertType($value, $type) - { - switch($type) - { - case 'int': - return (int)$value; - case 'float': - return (float)$value; - case 'bool': - return (bool)$value; - case 'array': - return array_filter((array)$value, function($var){return ($var === '0' || !empty($var));}); - case 'object': - return (object)$value; - case 'datetime': - case 'date': - return $value ? trim((string)$value) : null; - case 'string': - default: - return trim((string)$value); - } - } - /** * 是否是内网。 * Check is intranet.