Advertisement
tabvn

BuilderBase.php

Aug 16th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class BuilderBase{
  5.  
  6.  
  7.     protected $cache_id;
  8.  
  9.     /**
  10.     * $rows['x'] = array(
  11.     *   'id' => 'x',
  12.     *   'weight' => 0,
  13.     *   'title' => ''.
  14.     *   'settings' => array(),
  15.     * );
  16.     */
  17.     protected $rows;
  18.    
  19.     /**
  20.     * $columns['x'] = array(
  21.     *   'id' => 'y',
  22.     *   'parent' => 'x'
  23.     *   'weight' => 0,
  24.     *   'title' => '',
  25.     *   'settings' => array(),
  26.     * );
  27.     *
  28.     *
  29.     */
  30.     protected $columns;
  31.    
  32.    
  33.     /**
  34.     * $contents['z'] = array(
  35.     *   'id' => 'z',
  36.     *   'parent'=> 'y',
  37.     *   'weight' => 0,
  38.     *   'title' => '',
  39.     *   'settings' => array(),
  40.     * );
  41.     */
  42.     protected $contents;
  43.    
  44.    
  45.     public function __construct(){
  46.        
  47.         $data = cache_get($this->cache_id);
  48.        
  49.         $this->rows = $data['rows'];
  50.         $this->columns = $data['columns'];
  51.         $this->contents = $data['contents'];
  52.        
  53.     }
  54.    
  55.     public function getElement($type = 'row', $id){
  56.        
  57.         switch $type{
  58.            
  59.             case 'row':
  60.                 return $this->rows[$id];
  61.             break;
  62.            
  63.             case 'column':
  64.                 return $this->columns[$id];
  65.             break;
  66.            
  67.             case 'content':
  68.                 return $this->contents[$id];
  69.             break;
  70.         }
  71.        
  72.     }
  73.    
  74.     public function setElement($type = 'row', $id = NULL, $data = array()){
  75.         $elements = array(
  76.             'row' => 'rows';
  77.             'column' => 'columns',
  78.             'content' => 'contents',
  79.         );
  80.         if(empty($id)){
  81.            
  82.             $id = uniqid($type.'_');
  83.         }
  84.        
  85.        
  86.         if(isset($elements[$type])){
  87.             $this->$elements[$type][$id] = $data;
  88.         }else{
  89.            
  90.             $this->$type[$id] = $data;
  91.         }
  92.        
  93.         // do save element to Object attributes here.
  94.        
  95.         // call to update function save builder to cache.
  96.         $this->update();
  97.        
  98.     }
  99.    
  100.    
  101.     /**
  102.     * Do save builder data to cache when Object changed.
  103.     */
  104.     public function update(){
  105.        
  106.         $data = get_object_vars($this);
  107.         // example to save builder to cache.
  108.         cache_save($this->cache_id, $data);
  109.        
  110.     }
  111.    
  112.     // Now idea for render this Builder
  113.    
  114.     public function render(){
  115.        
  116.         // Idea here.
  117.     }
  118.    
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125. // #version 2 *** =======  *////////////////
  126.  
  127. <?pph
  128.  
  129. class BuilderBase{
  130.    
  131.    
  132.     protected $cache_id;
  133.     protected $elements;
  134.    
  135.    
  136.    
  137.    
  138.     public function __construct(){
  139.        
  140.         $data = cache_get($this->cache_id);
  141.        
  142.         $this->elements = $data['elements'];
  143.     }
  144.    
  145.     public function __get($id){
  146.        
  147.        
  148.         return isset($elements->$id) ? $elements->$id : NULL;
  149.     }
  150.    
  151.    
  152.     public function __set($id, $element){
  153.        
  154.        
  155.        
  156.         // this is how element array look like.
  157.        
  158.         /*$element = array(
  159.             '#type' => 'row',
  160.             '#id' => $id,
  161.             '#settings' => array(),
  162.             '#weight' => 0,
  163.         ); */
  164.        
  165.         $this->elements->$id = $element;
  166.        
  167.         // detect object change. So we need save builder to cache.
  168.        
  169.         $this->update();
  170.     }
  171.    
  172.     private function update(){
  173.        
  174.         cache_save($this->cache_id, $elements);
  175.     }
  176.    
  177.     public function getAll(){
  178.         return $this->elements;
  179.     }
  180.    
  181.    
  182. }
  183.  
  184.  
  185.  
  186. $builder = new BuilderBase();
  187. $id = uniqid($type.'_');
  188. $element = array(
  189.     '#type' => 'row',
  190.     '#id' => $id,
  191.     '#settings' => array(),
  192.     '#weight' => 0,
  193.         '#parent' => '',
  194. );
  195.  
  196. $id = 'abcxyz';
  197. $builder->$id = $element;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement