xml模仿数据简单的操作类 不喜勿喷

浏览:1400 发布日期:2017/09/01 分类:功能实现 关键字: xml模仿数据
xml模仿数据简单的操作类 不喜勿喷 根据别人的二次开发
<?php
/**
* Created by PhpStorm.
* User: QQWWBYC
* Date: 2017/8/30
* Time: 13:44
*/
namespace Org\Net;
class xml
{
private $dbase; //数据库,要读取的xml文件
private $dbname; //数据库名称,顶层元素,与数据库文件名称一致
private $dbtable; //数据表,要取得的节点
private $parser; //剖析器
private $vals; //属性
private $index; //索引
private $dbtable_array;//节点数组
private $array; //下级节点的数组
private $result; //返回的结果
private $querys;
private $xml_where;//条件查询数据

/**
* @param $dbase xml数据文件绝对路径
* @param $dbtable xml数据表名
* @name 初始化xml数据库操作
*/
public function __construct($dbase,$dbtable){
$this->dbase=$dbase;
$this->dbname=substr($dbase,strrpos($dbase,"/")+1,-4);
$this->dbtable=$dbtable;
$data=$this->Readxml($this->dbase);
if(!$data){
die("无法读取 $this->dbname.xml");
}
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser,xml_OPTION_CASE_FOLDING,0);//控制在该 xml 解析器中 大小写折叠(case-folding) 是否有效
xml_parser_set_option($this->parser,xml_OPTION_SKIP_WHITE,1);//是否略过由白空字符组成的值。
$state=xml_parse_into_struct($this->parser,$data,$this->vals,$this->index);
xml_parser_free($this->parser);
if(!$state){
die("无法读取 $this->dbname.xml 数据失败");
}
//遍历索引,筛选出要取值的节点 节点名:$dbtable
foreach ($this->index as $key=>$val) {
if ($key == $this->dbtable) {
//取得节点数组
$this->dbtable_array = $val;
} else {
continue;
}
}
for ($i=0; $i < count($this->dbtable_array); $i+=2) {
$offset = $this->dbtable_array[$i] + 1;
$len = $this->dbtable_array[$i + 1] - $offset;
//array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。
//所取节点下级数组
$value=array_slice($this->vals,$offset,$len);
//取得有效数组,合并为结果数组
$this->array[]=$this->parseEFF($value);
}
return true;
}

/**
* @param $condition 查询条件
* @param $offset 取出数组开始坐标
* @param $limit_num 取出数据条数
* @return array|bool|int 根据查询条件和($offset和$limit_num)取出数据
*/
public function limit($condition,$offset,$limit_num){
if(empty($condition) && !is_array($condition)){
return false;
}
$offset=intval($offset);
$limit_num=intval($limit_num);
if(empty($limit_num)){
return false;
}
$data=$this->xml_select('select',$condition);
if(empty($data)){
return false;
}
$data=array_values($data);
$data=array_slice($data,$offset,$limit_num);
if(empty($data)){
return false;
}
return $data;
}
/**
* @param $file xml 文件绝对路径
* @return bool|string xml文件信息
*/
private function Readxml($file)
{
if(!file_exists($file)){
$this->initialization_xlm($file);
}
return file_get_contents($file);
}

/**
* @param $file xml 文件绝对路径
* @return bool 当数据库xml存在是自动创建xml数据文件
*/
private function initialization_xlm($file){
if(!file_exists($file)){
$this->Writexml();
}
return true;
}
/**
* @param $effective 坐标
* @return mixed 取得有效数组
*/
private function parseEFF($effective) {
for ($i=0; $i < count($effective); $i++){
$effect[$effective[$i]["tag"]] = $effective[$i]["value"];
}
return $effect;
}
//xml_query(方法,条件,多条件时逻辑运算符and or or,插入或更新的数组)
/**
* @param $method 操作方式
* @param $condition 操作方式所对应的查询条件
* @param array $array 跟新或插入数据数组
* @return array|int|void 返回操作结果
*/
public function xml_query($method,$condition,$array=array())
{
if(($method=='select')||($method=='count')){
return $this->xml_select($method,$condition);
} elseif($method=='insert') {
return $this->xml_insert($array);
} elseif($method=='update') {
return $this->xml_update($condition,$array);
}elseif($method=='delete'){
return $this->xml_del($condition);
}else{
die("操作错误");
}
}

/**
* @param $condition 条件数组 组装
EQ 等于(=)
NEQ 不等于(<>)
GT 大于(>)
EGT 大于等于(>=)
LT 小于(<)
ELT 小于等于(<=)
BETWEEN 区间查询
IN 多值查询
*/
private function xml_fetch_where($condition){
if(is_array($condition)){
$operator_fields=array("EQ","NEQ","GT","EGT","LT","ELT","BETWEEN","IN");
$num=0;
foreach($condition as $key=>$val){
if(is_array($val)){
$operator=strtoupper($val[0]);//运算符
$field=$key;//条件字段
$match_val=$val[1];//筛选值
if(in_array($operator,$operator_fields) && $match_val){
$this->xml_where[$num]['field']=$field;
$this->xml_where[$num]['match']=$match_val;
if(is_string($match_val)){
if($operator=="EQ"){
//运算符 等于(=)
$this->xml_where[$num]['operator']='=';
}elseif($operator=="NEQ"){
//运算符 不等于(<>)
$this->xml_where[$num]['operator']='!=';
}elseif($operator=="GT"){
//运算符 大于(>)
$this->xml_where[$num]['operator']='>';
}elseif($operator=="EGT"){
//运算符 大于等于(>=)
$this->xml_where[$num]['operator']='>=';
}elseif($operator=="LT"){
//运算符 小于(<)
$this->xml_where[$num]['operator']='<';
}elseif($operator=="ELT"){
//运算符 小于等于(<=)
$this->xml_where[$num]['operator']='<=';
}else{
die($operator.":查询条件错误");
}
}elseif(is_array($match_val)){
if($operator=="BETWEEN"){
//运算符 区间查询
$num=count($match_val);
if($num==2){
$this->xml_where[$num]['operator']='BETWEEN_AND';
}else{
die("BETWEEN:查询条件最小值和最大值错误");
}
}elseif($operator=="IN"){
//运算符 IN 多个值查询
if(count($match_val)>=1){
$this->xml_where[$num]['operator']='IN';
}else{
die("IN:查询条件值错误");
}
}else{
die($operator.":查询条件错误");
}
}else{
die($operator.":查询条件错误");
}
}else{
die("查询条件错误,条件查询说明 :EQ 等于(=)NEQ 不等于(<>)GT 大于(>)EGT 大于等于(>=)LT 小于(<)ELT 小于等于(<=)BETWEEN 区间查询 IN 多值查询");
}
}else{
die("查询条件错误");
}
}
}else{
die("查询条件为数组");
}
}

/**
* @return array 得到当前表名下所有数据
*/
public function get_xml_all_data(){
return $this->xml_fetch_array();
}

/**
* @param $data 批量新增数组 必须是二维数组
* @return bool 批量新增多条数据
*/
public function all_xml_insert($data){
if(empty($data)){
return false;
}
$data_Original=$this->get_xml_all_data();
foreach($data as $val){
if(is_array($val) && !empty($val)){
$data_Original[]=$val;
}else{
break;
}
}
if(empty($data_Original)){
return false;
}
$this->Writexml($data_Original);
return true;
}
/**
* @param null $condition 查询条件
* @return array 获取所有满足条件数据
* @name 筛选满足条件数据
*/
private function xml_fetch_array($condition=null)
{
$row = $this->array; //初始化数据数组
if($condition) {
//是否有条件,如有条件则生成符合条件的数组
//生成条件数组,条件格式 field,operator,match
$this->xml_fetch_where($condition);
$cs=count($this->xml_where); //条件数
if($cs==0){
die("查询条件为空");
}
if(!empty($row)){
foreach($row as $key=>$val){
$r=$key;
foreach($this->xml_where as $va){
$field=$va['field']; //字段
$operator=$va["operator"];//运算符
$match=$va['match']; //匹配
if(($operator=='=')&&($row[$r][$field]==$match)){
$need_key=$r;
} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){
$need_key=$r;
} elseif(($operator=='>')&&($row[$r][$field]>$match)){
$need_key=$r;
} elseif(($operator=='>=')&&($row[$r][$field]>=$match)){
$need_key=$r;
} elseif(($operator=='<')&&($row[$r][$field]<$match)){
$need_key=$r;
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){
$need_key=$r;
}elseif(($operator=='BETWEEN_AND')&&($row[$r][$field]>=$match[0] && $row[$r][$field]<=$match[1])){
$need_key=$r;
}elseif(($operator=='IN')&&(in_array($row[$r][$field],$match))){
$need_key=$r;
}
}
if(isset($row[$need_key])){
$result[$need_key]=$row[$need_key];
}
}
$this->dbtable_array=null;
$this->array=null;
}
} else {
$result=$this->array;
}
return $result;
}

/**
* @param $method 查询方式 select 统计数据个数count
* @param $condition 查询条件
* @return array|int 获取所有满足条件数据
* @name 查询满足条件的数据
*/
public function xml_select($method,$condition)
{
$result=$this->xml_fetch_array($condition);
if($method=='select'){
return $result;
} else {
return count($result);
}

}

/**
* @param $arr 需要判断数组
* @return mixed 判断数据的维数
*/
public function arrayLevel($arr){
$al = array(0);
$this->aL($arr,$al,$level=0);
return max($al);
}

/**
* @param $arr 需要判断数组
* @param $al 关联数据
* @param int $level 级别
* @name 判断数据的维数的核心方法
*/
private function aL($arr,&$al,$level=0){
if(is_array($arr)){
$level++;
$al[] = $level;
foreach($arr as $v){
$this->aL($v,$al,$level);
}
}
}
/**
* @param $array 要插入的数据
* @name 新增数据
*/
public function xml_insert($array)
{
if(empty($array)){
return false;
}
$num=$this->arrayLevel($array);
if($num==1){
$data=$this->xml_fetch_array();//总数据数组
$data=array_filter($data);
$data[]=$array; //插入后的总数据数组
$this->array=$data; //更新总数组
$this->Writexml($data);
return true;
}
return false;
}
/**
* @param $condition 需要删除的数据的条件语句
* @return bool 删除不想要的数据
*/
public function xml_del($condition){
if(empty($condition)){
return false;
}
$datas=$this->get_xml_all_data(); //总数据数组
$del_data=$this->xml_fetch_array($condition);//要删除的数据
$del_data=array_keys($del_data);
if(empty($del_data)){
return false;
}
foreach($del_data as $val){
$state=isset($datas[$val])?true:false;
if($state){
unset($datas[$val]);
}
}
if(empty($datas)){
$this->initialization_xlm($this->dbase);
}
$datas=array_values($datas);
$this->Writexml($datas);
return true;
}
/**
* @param $condition 更新条件
* @param $array 要更新的数组
* @return bool 跟新数据
*/
public function xml_update($condition,$array)
{
$datas=$this->get_xml_all_data(); //总数据数组
$subtract=$this->xml_fetch_array($condition);//要更新的数组
if(empty($subtract) || empty($datas)){
return false;
}
$state=1;
$keys_update=array_keys($subtract);
if(empty($keys_update)){
return false;
}
foreach($keys_update as $val){
$new_data_arr=isset($datas[$val])?$datas[$val]:null;
if(empty($new_data_arr)){
$state=0;
break;
}
$new_data_arr=$this->update_data_assemble($new_data_arr,$array);
if($new_data_arr){
$datas[$val]=$new_data_arr;
}else{
$state=0;
break;
}
}
if(empty($state)){
return false;
}
$this->array=$datas;
$this->Writexml($datas);
return true;
}

/**
* @param $data_arr 原始数据
* @param $update_arr 需要更新的数据 跟新的数据的值不能null
* @return array|bool 返回当前的要跟新的数据
*/
private function update_data_assemble($data_arr,$update_arr){
if(empty($data_arr) || empty($update_arr)){
return false;
}
$state_update=false;
$need_update_num=0;
if(is_array($data_arr) && is_array($update_arr)){
foreach($data_arr as $key=>$val){
if(is_array($val)){
$state_update=true;
break;
}
$update_val=isset($update_arr[$key])?$update_arr[$key]:null;
if($update_val=='0'){
if($data_arr[$key] != $update_val){
$data_arr[$key]=$update_val;
$need_update_num++;
}
}
if(!empty($update_val)){
if($data_arr[$key] != $update_val){
$data_arr[$key]=$update_val;
$need_update_num++;
}
}
}
if($state_update){
return false;
}
if($need_update_num==0){
return false;
}
return $data_arr;

}
return false;
}
/**
* @param null $array 写入xml文件(全部写入)
*/
private function Writexml($array=null)
{
if(is_dir($this->dbase)){
if(!is_writeable($this->dbase)){
die("无法写入".$this->dbname.".xml");
}
}
$xml="";
$xml.="<?xml version='1.0' encoding='utf-8'?>";
$xml.="<$this->dbname>";
if(empty($array)){
$xml.="<$this->dbtable>";
$xml.="</$this->dbtable>";
}else{
for($i=0;$i<count($array);$i++){
$xml.="<$this->dbtable>";
foreach($array[$i] as $k=>$s){
$xml.="<$k>$s</$k>";
}
$xml.="</$this->dbtable>";
}
}
$xml.="</$this->dbname>";
$fp=@fopen($this->dbase,"w");
flock($fp, LOCK_EX);
rewind($fp);
fputs($fp,$xml);
fclose($fp);
}
}
?>
评论( 相关
后面还有条评论,点击查看>>