mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
添加数据转换类及相关驱动
This commit is contained in:
47
Library/Think/Transform.php
Normal file
47
Library/Think/Transform.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
22
Library/Think/Transform/Driver/Json.php
Normal file
22
Library/Think/Transform/Driver/Json.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
80
Library/Think/Transform/Driver/Xml.php
Normal file
80
Library/Think/Transform/Driver/Xml.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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 version=\"1.0\" encoding=\"{$config['encoding']}\"?>";
|
||||
$xml .= "<{$config['root_name']}{$attr}>";
|
||||
$xml .= self::data2xml($data, $config['item_name'], $config['item_key']);
|
||||
$xml .= "</{$config['root_name']}>";
|
||||
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 .= "</{$key}>";
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user