From 828ae2d18ccee225df0b0536104655f387a9d189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Fri, 19 Apr 2013 13:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E7=B1=BB=E5=8F=8A=E7=9B=B8=E5=85=B3=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Transform.php | 47 +++++++++++++++ Library/Think/Transform/Driver/Json.php | 22 +++++++ Library/Think/Transform/Driver/Xml.php | 80 +++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 Library/Think/Transform.php create mode 100644 Library/Think/Transform/Driver/Json.php create mode 100644 Library/Think/Transform/Driver/Xml.php diff --git a/Library/Think/Transform.php b/Library/Think/Transform.php new file mode 100644 index 00000000..7fcf9bb9 --- /dev/null +++ b/Library/Think/Transform.php @@ -0,0 +1,47 @@ + +// +---------------------------------------------------------------------- + +namespace Think; + +// 内容解析类 +class Transform { + + static private $handler = []; + + static private init($type){ + if(!isset(self::$handler[$type])) { + $class = '\\Think\\Transform\\Driver\\' . ucwords($type); + self::$handler[$type] = new $class(); + } + } + + // 编码内容 + static public function encode($content, $type){ + self::init($type); + return self::$handler[$type]->encode($content); + } + + // 解码内容 + static public function decode($content, $type, $assoc = true){ + self::init($type); + return self::$handler[$type]->decode($content, $assoc); + } + + // 调用驱动类的方法 + // Transform::xmlEncode('abc') + // Transform::jsonDecode('abc', true); + static public function __callStatic($method, $params){ + $type = substr($method, 0, strlen($method) - 6); + $method = substr($method, -6); + $assoc = empty($params[2]) ? true : false; + return self::$method($params[0], $type, $assoc); + } +} diff --git a/Library/Think/Transform/Driver/Json.php b/Library/Think/Transform/Driver/Json.php new file mode 100644 index 00000000..6a4f0b84 --- /dev/null +++ b/Library/Think/Transform/Driver/Json.php @@ -0,0 +1,22 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Transform\Driver; + +class Json{ + public function encode($data){ + return json_encode($data); + } + + public function decode($data, $assoc = true){ + return json_decode($data, $assoc); + } +} \ No newline at end of file diff --git a/Library/Think/Transform/Driver/Xml.php b/Library/Think/Transform/Driver/Xml.php new file mode 100644 index 00000000..555359a7 --- /dev/null +++ b/Library/Think/Transform/Driver/Xml.php @@ -0,0 +1,80 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Transform\Driver; + +class Xml{ + /** + * XML数据默认配置项 + * @var array + */ + private $config = [ + 'root_name' => 'think', //根节点名称 + 'root_attr' => [], //根节点属性 + 'item_name' => 'item', //数字节点转换的名称 + 'item_key' => 'id', //数字节点转换的属性名 + 'encoding' => 'utf-8', //字符集编码 + ]; + + /** + * 编码XML数据 + * @param mixed $data 被编码的数据 + * @param array $config 数据配置项 + * @return string 编码后的XML数据 + */ + public function encode($data, array $config = []) { + //初始化配置 + $config = array_merge($this->config, $config); + + if(is_array($config['root_attr'] && !empty($config['root_attr']))){ + $attr = []; + foreach ($config['root_attr'] as $key => $value) { + $attr[] = "{$key}=\"{$value}\""; + } + $attr = implode(' ', $attr); + } + $attr = empty($attr) ? '' : " {$attr}"; + $xml = ""; + $xml .= "<{$config['root_name']}{$attr}>"; + $xml .= self::data2xml($data, $config['item_name'], $config['item_key']); + $xml .= ""; + return $xml; + } + + public function decode($data, $assoc = true){ + + } + + + + /** + * 数据XML编码 + * @static + * @access public + * @param mixed $data 数据 + * @param string $item 数字索引时的节点名称 + * @param string $id 数字索引key转换为的属性名 + * @return string + */ + static public function data2xml($data, $item = 'item', $id = 'id') { + $xml = $attr = ''; + foreach ($data as $key => $val) { + if(is_numeric($key)){ + $id && $attr = " {$id}=\"{$key}\""; + $key = $item; + } + $xml .= "<{$key}{$attr}>"; + $xml .= (is_array($val) || is_object($val)) ? self::data2xml($val, $item, $id) : $val; + $xml .= ""; + } + return $xml; + } +} \ No newline at end of file