From 939c3fdfde590fa430a516f9a4cfbe2f00b81258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Sat, 20 Apr 2013 12:47:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20=5F=5FcallStatic=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Transform.php | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Library/Think/Transform.php b/Library/Think/Transform.php index e41738ff..4527f1f1 100644 --- a/Library/Think/Transform.php +++ b/Library/Think/Transform.php @@ -22,7 +22,7 @@ class Transform { * @param string $type 驱动类型 */ static private function init($type){ - if(!isset(self::$handler[$type])) { + if(!isset(self::$handler[$type])) { $class = '\\Think\\Transform\\Driver\\' . ucwords($type); self::$handler[$type] = new $class(); } @@ -34,11 +34,12 @@ class Transform { * @access public * @param mixed $content 要编码的数据 * @param string $type 数据类型 + * @param array $config XML配置参数,JSON格式生成无此参数 * @return string 编码后的数据 */ - static public function encode($content, $type){ + static public function encode($content, $type, array $config = []){ self::init($type); - return self::$handler[$type]->encode($content); + return self::$handler[$type]->encode($content, $config); } /** @@ -46,23 +47,31 @@ class Transform { * @param string $content 要解码的数据 * @param string $type 数据类型 * @param boolean $assoc 是否返回数组 + * @param array $config XML配置参数,JSON格式解码无此参数 * @return mixed 解码后的数据 */ - static public function decode($content, $type, $assoc = true){ - self::init($type); - return self::$handler[$type]->decode($content, $assoc); + static public function decode($content, $type, $assoc = true, array $config = []){ + self::init($type); + return self::$handler[$type]->decode($content, $assoc, $config); } // 调用驱动类的方法 // Transform::xmlEncode('abc') // Transform::jsonDecode('abc', true); - static public function __callStatic($method, $params){ - $type = substr($method, 0, strlen($method) - 6); - $method = strtolower(substr($method, -6)); - $assoc = empty($params[2]) ? true : false; - if(!in_array($method, ['encode', 'decode'])){ - throw new Think\Exception("call to undefined method {$method}"); + static public function __callStatic($method, $params){ + $type = substr($method, 0, strlen($method) - 6); + $method = strtolower(substr($method, -6)); + + switch ($method) { + 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}"); } - return self::$method($params[0], $type, $assoc); - } + } }