Guest User

Untitled

a guest
Jun 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2. define("HISTORY_OVERWRITE",1);
  3. define("HISTORY_PREFERENCES",2);
  4. define("HISTORY_OVERWRITEINARRAY",4);
  5.  
  6. class History {
  7.   public $id = '';
  8.   private $history = array();
  9.   private $options = array();
  10.   private $final = array();
  11.   private $preferences = array();
  12.  
  13.   public function __construct($id,$opt=array(),$pref=array()) {
  14.     $this->id = $id;
  15.     $this->setOptions($opt);
  16.     $this->setPreferences($pref);
  17.   }
  18.  
  19.   public function add($hist,$removeDuplicates=false) {
  20.     if($removeDuplicates) {
  21.       if($this->history[count($this->history)-1] != $this) {
  22.         $this->history[] = $hist;
  23.       }
  24.     } else $this->history[] = $hist;
  25.  
  26.     $this->_mergeElement($hist);
  27.  
  28.     return $this->final;
  29.   }
  30.  
  31.   public function setOptions($opt) {
  32.     $this->options = $opt;
  33.  
  34.     return true;
  35.   }
  36.  
  37.   public function setPreferences($pref) {
  38.     $this->preferences = $pref;
  39.  
  40.     return true;
  41.   }
  42.  
  43.   public function setHistory($hist) {
  44.     $this->history = $hist;
  45.  
  46.     return true;
  47.   }
  48.  
  49.   public function merge() {
  50.     foreach($this->history as $hist) {
  51.       $this->_mergeElement($hist);
  52.     }
  53.  
  54.     return $this->final;
  55.   }
  56.  
  57.   private function _mergeElement($hist) {
  58.     foreach($hist as $key=>$elem) {
  59.       if($this->_checkPermission($key,HISTORY_OVERWRITE)) {
  60.         if(array_key_exists($key,$this->final)) {
  61.           $this->final[$key] = ($this->_checkPermission($key,HISTORY_PREFERENCES)) ? $this->_checkPreferences($key,$elem,$this->final[$key]) : $elem;
  62.         } else $this->final[$key] = $elem;
  63.       } else $this->final[$key][] = $elem;
  64.     }
  65.   }
  66.  
  67.   private function _checkPermission($key,$option) {
  68.     if(!array_key_exists($key,$this->options)) return false;
  69.     return ($option & $this->options[$key]) ? true : false;
  70.   }
  71.  
  72.   private function _checkPreferences($key,$e1,$e2) {
  73.     if(array_key_exists($key,$this->preferences)) {
  74.       foreach($this->preferences[$key] as $e) {
  75.         if($e1 == $e or $e2 == $e) return $e;
  76.       }
  77.     } else return $e1;
  78.   }
  79. }
  80.  
  81. $o = new History("t");
  82. $o->setOptions(array(
  83.   'status' => HISTORY_OVERWRITE | HISTORY_PREFERENCES,
  84.   'dsn' => HISTORY_OVERWRITE,
  85.   'delays' => HISTORY_OVERWRITE,
  86.   'delay' => HISTORY_OVERWRITE,
  87.   'to' => HISTORY_OVERWRITE,
  88.   'relay' => HISTORY_OVERWRITE,
  89.   'id' => HISTORY_OVERWRITE
  90. ));
  91.  
  92. $o->setPreferences(array(
  93.   'status' => array('sent','deferred')
  94. ));
  95.  
  96. $arr = $o->add(array(
  97.     "status" => "sent"
  98.     , "dsn" => "2.0.0"
  99.     , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
  100.     , "delays" => "0.52/0/0.64/1.6"
  101.     , "delay" => "2.7"
  102.     , "to" => "testsetes"
  103.     , "id" => "0039560E4"
  104. ));
  105. print_r($arr);
  106. $arr = $o->add(array(
  107.     "status" => "deferred"
  108.     , "dsn" => "2.0.0"
  109.     , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
  110.     , "delays" => "0.52/0/0.64/1.6"
  111.     , "delay" => "2.7"
  112.     , "to" => "something else"
  113.     , "id" => "0039560E4"
  114. ));
  115. ?>
Add Comment
Please, Sign In to add comment