调整 __callStatic 方法

This commit is contained in:
麦当苗儿
2013-04-20 12:47:50 +08:00
parent 1b3f366a59
commit c97e18c377

View File

@@ -22,7 +22,7 @@ class Transform {
* @param string $type 驱动类型 * @param string $type 驱动类型
*/ */
static private function init($type){ static private function init($type){
if(!isset(self::$handler[$type])) { if(!isset(self::$handler[$type])) {
$class = '\\Think\\Transform\\Driver\\' . ucwords($type); $class = '\\Think\\Transform\\Driver\\' . ucwords($type);
self::$handler[$type] = new $class(); self::$handler[$type] = new $class();
} }
@@ -34,11 +34,12 @@ class Transform {
* @access public * @access public
* @param mixed $content 要编码的数据 * @param mixed $content 要编码的数据
* @param string $type 数据类型 * @param string $type 数据类型
* @param array $config XML配置参数JSON格式生成无此参数
* @return string 编码后的数据 * @return string 编码后的数据
*/ */
static public function encode($content, $type){ static public function encode($content, $type, array $config = []){
self::init($type); self::init($type);
return self::$handler[$type]->encode($content); return self::$handler[$type]->encode($content, $config);
} }
/** /**
@@ -46,23 +47,35 @@ class Transform {
* @param string $content 要解码的数据 * @param string $content 要解码的数据
* @param string $type 数据类型 * @param string $type 数据类型
* @param boolean $assoc 是否返回数组 * @param boolean $assoc 是否返回数组
* @param array $config XML配置参数JSON格式解码无此参数
* @return mixed 解码后的数据 * @return mixed 解码后的数据
*/ */
static public function decode($content, $type, $assoc = true){ static public function decode($content, $type, $assoc = true, array $config = []){
self::init($type); self::init($type);
return self::$handler[$type]->decode($content, $assoc); return self::$handler[$type]->decode($content, $assoc, $config);
} }
// 调用驱动类的方法 // 调用驱动类的方法
// Transform::xmlEncode('abc') // Transform::xmlEncode('abc')
// Transform::jsonDecode('abc', true); // Transform::jsonDecode('abc', true);
static public function __callStatic($method, $params){ static public function __callStatic($method, $params){
$type = substr($method, 0, strlen($method) - 6); if(empty($params[0])){
$method = strtolower(substr($method, -6)); return '';
$assoc = empty($params[2]) ? true : false;
if(!in_array($method, ['encode', 'decode'])){
throw new Think\Exception("call to undefined method {$method}");
} }
return self::$method($params[0], $type, $assoc);
} //获取类型
$type = substr($method, 0, strlen($method) - 6);
switch (strtolower(substr($method, -6))) {
case 'encode':
$config = empty($params[1]) ? [] : $params[1];
return self::encode($params[0], $type, $config);
case 'decode':
$assoc = empty($params[1]) ? true : $params[1];
$config = empty($params[2]) ? [] : $params[2];
return self::decode($params[0], $type, $assoc, $config);
default:
throw new Exception("call to undefined method {$method}");
}
}
} }