加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

简朴谈谈PHP面向工具之标识工具

发布时间:2021-01-24 08:06:26 所属栏目:编程 来源:网络整理
导读:标识工具模式 这个模式首要成果就是建设sql语句中的wehre前提字符串的,下面直接看代码和注释: //字段工具 class Field { protected $name = null; //字段名称 protected $operator = null; //操纵符 protected $comps = array(); //存放前提的数组 protect

标识工具模式

这个模式首要成果就是建设sql语句中的wehre前提字符串的,下面直接看代码和注释:

//字段工具
class Field {
protected $name = null; //字段名称
protected $operator = null; //操纵符
protected $comps = array(); //存放前提的数组
protected $incomplete = false; //搜查前提数组是否有值

function __construct ($name){
$this->name= $name;
}

//添加where 前提
function addTest($operator,$value){
$this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);
}

//获取存放前提的数组
function getComps(){
return $this->comps;
}

function isIncomplete(){
return empty($this->comps);
}
}

//标识工具
class IdentityObject {
protected $currentfield = null; //当前操纵的字段工具
protected $fields = array(); //字段荟萃
private $and = null;
private $enforce = array(); //限制的正当字段

function __construct($field = null,array $enforce = null){
if(!is_null($enforce)){
$this->enforce = $enforce;
}
if(!is_null($field)){
$this->field($field);
}
}

//获取限制的正当字段
function getObjectFields(){
return $this->enforce;
}

//首要成果为配置当前必要操纵的工具
function field($fieldname){
if(!$this->isVoid()&& $this->currentfield->isIncomplete()){
throw new Exception("Incomplete field");
}
$this->enforceField($fieldname);
if(isset($this->fields[$fieldname]){
$this->currentfield = $this->fields[$fieldname];
} else {
$this->currentfield = new Field($fieldname);
$this->fields[$fieldname] = $this->currentfield;
}
return $this; //回收连贯语法
}

//字段荟萃是否为空
function isVoid(){
return empty($this->fields);
}

//搜查字段是否正当
function enforceField ($fieldname){
if(!in_array($fieldname,$this->enforce) && !empty($this->enforce)){
$forcelist = implode(',',$this->enforce);
throw new Exception("{$fieldname} not a legal field {$forcelist}");
}
}

//向字段工具添加where前提
function eq($value){
return $this->operator("=",$value);
}

function lt($value){
return $this->operator("<",$value);
}

function gt($value){
return $this->operator(">",$value);
}

//向字段工具添加where前提
private function operator($symbol,$value){
if($this->isVoid){
throw new Exception("no object field defined");
}
$this->currentfield->addTest($symbol,$value);
return $this; //回收连贯语法
}

//获取此类中全部字段工具荟萃的where前提数组
function getComps(){
$ret = array();
foreach($this->fields as $key => $field){
$ret = array_merge($ret,$field->getComps());
}
return $ret;
}
}

//客户端代码
$idobj = new IdentityObject ();
$idobj->field("name")->eq("The Good Show")->field("start")->gt(time())->lt(time()+(246060));
$test = $idobj->getComps();
var_dump($test);

//输出相同下面的内容

/*
array{
array('name'=>'name','operator'=>'=','value'=>'The Good Show'),array('name'=>'start','operator'=>'>','value'=>'123456'),//123456暗示time()函数输出的时刻戳
array('name'=>'start','operator'=>'<','value'=>'123456')
}

*/

以上这篇简朴谈谈PHP面向工具之标识工具就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程之家。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读