扩展库
This commit is contained in:
87
extend/org/transform/driver/Base64.php
Normal file
87
extend/org/transform/driver/Base64.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Haotong Lin<lofanmi@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace org\transform\driver;
|
||||
|
||||
/**
|
||||
* Base64编码实现
|
||||
*
|
||||
*/
|
||||
class Base64
|
||||
{
|
||||
/**
|
||||
* @access public
|
||||
* @static 编解码目标
|
||||
* default: 原始的编(解)码
|
||||
* url : URL友好的编(解)码
|
||||
* regex : 正则表达式友好的编(解)码
|
||||
*/
|
||||
public static $target = 'default';
|
||||
|
||||
/**
|
||||
* Base64编码函数
|
||||
*
|
||||
* @param string $data 欲编码的数据
|
||||
* @param string $target 编码目标
|
||||
*/
|
||||
public function encode($data, $target = '')
|
||||
{
|
||||
// 当函数没有特别指定编码目标时, 使用类自身编码目标
|
||||
if (empty($target)) {
|
||||
$target = self::$target;
|
||||
}
|
||||
// 进行一次原始编码
|
||||
$data = base64_encode($data);
|
||||
// 根据编码目标替换字符
|
||||
switch ($target) {
|
||||
case 'url':
|
||||
$data = str_replace(['+', '/', '='], ['-', '_', ''], $data);
|
||||
break;
|
||||
case 'regex':
|
||||
$data = str_replace(['+', '/', '='], ['!', '-', ''], $data);
|
||||
break;
|
||||
case 'default':
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 返回编码结果
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64解码函数
|
||||
*
|
||||
* @param string $data 欲解码的数据
|
||||
* @param string $target 解码目标
|
||||
*/
|
||||
public function decode($data, $target = '')
|
||||
{
|
||||
// 当函数没有特别指定解码目标时, 使用类自身解码目标
|
||||
if (empty($target)) {
|
||||
$target = self::$target;
|
||||
}
|
||||
// 根据解码目标替换字符
|
||||
switch ($target) {
|
||||
case 'url':
|
||||
$data = str_replace(['-', '_'], ['+', '/'], $data);
|
||||
break;
|
||||
case 'regex':
|
||||
$data = str_replace(['!', '-'], ['+', '/'], $data);
|
||||
break;
|
||||
case 'default':
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 原始解码,并返回结果
|
||||
return base64_decode($data);
|
||||
}
|
||||
|
||||
}
|
||||
25
extend/org/transform/driver/Json.php
Normal file
25
extend/org/transform/driver/Json.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace org\transform\driver;
|
||||
|
||||
class Json
|
||||
{
|
||||
public function encode($data)
|
||||
{
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
public function decode($data, $assoc = true)
|
||||
{
|
||||
return json_decode($data, $assoc);
|
||||
}
|
||||
}
|
||||
124
extend/org/transform/driver/Xml.php
Normal file
124
extend/org/transform/driver/Xml.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace org\transform\driver;
|
||||
|
||||
class Xml
|
||||
{
|
||||
/**
|
||||
* XML数据默认配置项
|
||||
* @var array
|
||||
*/
|
||||
private $config = [
|
||||
'root_name' => 'think', //根节点名称
|
||||
'root_attr' => [], //根节点属性
|
||||
'item_name' => 'item', //数字节点转换的名称
|
||||
'item_key' => 'id', //数字节点转换的属性名
|
||||
];
|
||||
|
||||
/**
|
||||
* 编码XML数据
|
||||
* @param mixed $data 被编码的数据
|
||||
* @param array $config 数据配置项
|
||||
* @return string 编码后的XML数据
|
||||
*/
|
||||
public function encode($data, array $config = [])
|
||||
{
|
||||
//初始化配置
|
||||
$config = array_merge($this->config, $config);
|
||||
|
||||
//创建XML对象
|
||||
$xml = new \SimpleXMLElement("<{$config['root_name']}></{$config['root_name']}>");
|
||||
self::data2xml($xml, $data, $config['item_name'], $config['item_key']);
|
||||
return $xml->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码XML数据
|
||||
* @param string $str XML字符串
|
||||
* @param boolean $assoc 是否转换为数组
|
||||
* @param array $config 数据配置项
|
||||
* @return string 解码后的XML数据
|
||||
*/
|
||||
public function decode($str, $assoc = true, array $config = [])
|
||||
{
|
||||
//初始化配置
|
||||
$config = array_merge($this->config, $config);
|
||||
|
||||
//创建XML对象
|
||||
$xml = new \SimpleXMLElement($str);
|
||||
if ($assoc) {
|
||||
self::xml2data($xml, $data, $config['item_name'], $config['item_key']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML编码
|
||||
* @static
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
* @return string
|
||||
*/
|
||||
public static function data2xml(\SimpleXMLElement $xml, $data, $item = 'item', $id = 'id')
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
//指定默认的数字key
|
||||
if (is_numeric($key)) {
|
||||
$id && $val = $key;
|
||||
$key = $item;
|
||||
}
|
||||
|
||||
//添加子元素
|
||||
if (is_array($value) || is_object($value)) {
|
||||
$child = $xml->addChild($key);
|
||||
self::data2xml($child, $value, $item, $id);
|
||||
} else {
|
||||
$child = $xml->addChild($key, $value);
|
||||
}
|
||||
|
||||
//记录原来的key
|
||||
isset($val) && $child->addAttribute($id, $val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML解码
|
||||
* @static
|
||||
* @access public
|
||||
* @param SimpleXMLElement $xml xml对象
|
||||
* @param array $data 解码后的数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
*/
|
||||
public static function xml2data(SimpleXMLElement $xml, &$data, $item = 'item', $id = 'id')
|
||||
{
|
||||
foreach ($xml->children() as $items) {
|
||||
$key = $items->getName();
|
||||
$attr = $items->attributes();
|
||||
if ($key == $item && isset($attr[$id])) {
|
||||
$key = strval($attr[$id]);
|
||||
}
|
||||
|
||||
if ($items->count()) {
|
||||
self::xml2data($items, $val);
|
||||
} else {
|
||||
$val = strval($items);
|
||||
}
|
||||
|
||||
$data[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user