PHP物件導向之標識對象

來源:互聯網
上載者:User

標籤:函數   function   app   map   false   const   value   設定   add   

  1. /*
  2. 標識對象模式
  3. 這個模式主要功能就是建立sql語句中的wehre條件字串的,下面直接看代碼和注釋:
  4. */
  5. namespace woo\mapper;
  6. //欄位對象
  7. class Field {
  8.     protected $name = null;          //欄位名稱
  9.     protected $operator = null;         //操作符    
  10.     protected $comps = array();         //存放條件的數組    
  11.     protected $incomplete = false;     //檢查條件數組是否有值
  12.     
  13.     function __construct ($name){
  14.         $this->name= $name;
  15.     }
  16.     
  17.     //添加where 條件
  18.     function addTest($operator,$value){
  19.         $this->comps[] = array(‘name‘=>$this->name,‘operator‘=>$operator,‘value‘=>$value);
  20.     }
  21.     
  22.     //擷取存放條件的數組
  23.     function getComps(){
  24.         return $this->comps;
  25.     }
  26.     
  27.     function isIncomplete(){
  28.         return empty($this->comps);
  29.     }
  30. }
  31. //標識對象
  32. class IdentityObject {
  33.     protected $currentfield = null;        //當前操作的欄位對象
  34.     protected $fields = array();        //欄位集合
  35.     private $and = null;
  36.     private $enforce = array();            //限定的合法欄位        
  37.     
  38.     function __construct($field = null, array $enforce = null){
  39.         if(!is_null($enforce)){
  40.             $this->enforce = $enforce;
  41.         }
  42.         if(!is_null($field)){
  43.             $this->field($field);
  44.         }
  45.     }
  46.     
  47.     //擷取限定的合法欄位
  48.     function getObjectFields(){
  49.         return $this->enforce;
  50.     }
  51.     
  52.     //主要功能為設定當前需要操作的對象
  53.     function field($fieldname){
  54.         if(!$this->isVoid()&& $this->currentfield->isIncomplete()){
  55.             throw new \Exception("Incomplete field");
  56.         }
  57.         $this->enforceField($fieldname);
  58.         if(isset($this->fields[$fieldname]){
  59.             $this->currentfield = $this->fields[$fieldname];
  60.         } else {
  61.             $this->currentfield = new Field($fieldname);
  62.             $this->fields[$fieldname] = $this->currentfield;
  63.         }
  64.         return $this;                    //採用連貫文法
  65.     }
  66.     
  67.     //欄位集合是否為空白
  68.     function isVoid(){
  69.         return empty($this->fields);
  70.     }
  71.     
  72.     //檢查欄位是否合法
  73.     function enforceField ($fieldname){
  74.         if(!in_array($fieldname,$this->enforce) && !empty($this->enforce)){
  75.             $forcelist = implode(‘,‘,$this->enforce);
  76.             throw new \Exception("{$fieldname} not a legal field {$forcelist}");
  77.         }
  78.     }
  79.     
  80.     
  81.     //向欄位對象添加where條件
  82.     function eq($value){
  83.         return $this->operator("=",$value);
  84.     }
  85.     
  86.     function lt($value){
  87.         return $this->operator("<",$value);
  88.     }
  89.     
  90.     function gt($value){
  91.         return $this->operator(">",$value);
  92.     }
  93.     
  94.     //向欄位對象添加where條件
  95.     private function operator($symbol,$value){
  96.         if($this->isVoid){
  97.             throw new \Exception("no object field defined");
  98.         }
  99.         $this->currentfield->addTest($symbol,$value);
  100.         return $this;                                     //採用連貫文法
  101.     }
  102.     
  103.     //擷取此類中所有欄位對象集合的where條件數組
  104.     function getComps(){
  105.         $ret = array();
  106.         foreach($this->fields as $key => $field){
  107.             $ret = array_merge($ret,$field->getComps());
  108.         }
  109.         return $ret;
  110.     }
  111. }
  112. //用戶端代碼
  113. $idobj = new IdentityObject ();
  114. $idobj->field("name")->eq("The Good Show")->field("start")->gt(time())->lt(time()+(24*60*60));
  115. $test = $idobj->getComps();
  116. var_dump($test);
  117. //輸出類似下面的內容
  118. /*
  119. array{
  120.     array(‘name‘=>‘name‘,‘operator‘=>‘=‘,‘value‘=>‘The Good Show‘),
  121.     array(‘name‘=>‘start‘,‘operator‘=>‘>‘,‘value‘=>‘123456‘),   //123456表示time()函數輸出的時間戳記
  122.     array(‘name‘=>‘start‘,‘operator‘=>‘<‘,‘value‘=>‘123456‘)
  123. }
  124. */

PHP物件導向之標識對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.