Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("HISTORY_OVERWRITE",1);
- define("HISTORY_PREFERENCES",2);
- define("HISTORY_OVERWRITEINARRAY",4);
- class History {
- public $id = '';
- private $history = array();
- private $options = array();
- private $final = array();
- private $preferences = array();
- public function __construct($id,$opt=array(),$pref=array()) {
- $this->id = $id;
- $this->setOptions($opt);
- $this->setPreferences($pref);
- }
- public function add($hist,$removeDuplicates=false) {
- if($removeDuplicates) {
- if($this->history[count($this->history)-1] != $this) {
- $this->history[] = $hist;
- }
- } else $this->history[] = $hist;
- $this->_mergeElement($hist);
- return $this->final;
- }
- public function setOptions($opt) {
- $this->options = $opt;
- return true;
- }
- public function setPreferences($pref) {
- $this->preferences = $pref;
- return true;
- }
- public function setHistory($hist) {
- $this->history = $hist;
- return true;
- }
- public function merge() {
- foreach($this->history as $hist) {
- $this->_mergeElement($hist);
- }
- return $this->final;
- }
- private function _mergeElement($hist) {
- foreach($hist as $key=>$elem) {
- if($this->_checkPermission($key,HISTORY_OVERWRITE)) {
- if(array_key_exists($key,$this->final)) {
- $this->final[$key] = ($this->_checkPermission($key,HISTORY_PREFERENCES)) ? $this->_checkPreferences($key,$elem,$this->final[$key]) : $elem;
- } else $this->final[$key] = $elem;
- } else $this->final[$key][] = $elem;
- }
- }
- private function _checkPermission($key,$option) {
- if(!array_key_exists($key,$this->options)) return false;
- return ($option & $this->options[$key]) ? true : false;
- }
- private function _checkPreferences($key,$e1,$e2) {
- if(array_key_exists($key,$this->preferences)) {
- foreach($this->preferences[$key] as $e) {
- if($e1 == $e or $e2 == $e) return $e;
- }
- } else return $e1;
- }
- }
- $o = new History("t");
- $o->setOptions(array(
- 'status' => HISTORY_OVERWRITE | HISTORY_PREFERENCES,
- 'dsn' => HISTORY_OVERWRITE,
- 'delays' => HISTORY_OVERWRITE,
- 'delay' => HISTORY_OVERWRITE,
- 'to' => HISTORY_OVERWRITE,
- 'relay' => HISTORY_OVERWRITE,
- 'id' => HISTORY_OVERWRITE
- ));
- $o->setPreferences(array(
- 'status' => array('sent','deferred')
- ));
- $arr = $o->add(array(
- "status" => "sent"
- , "dsn" => "2.0.0"
- , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
- , "delays" => "0.52/0/0.64/1.6"
- , "delay" => "2.7"
- , "to" => "testsetes"
- , "id" => "0039560E4"
- ));
- print_r($arr);
- $arr = $o->add(array(
- "status" => "deferred"
- , "dsn" => "2.0.0"
- , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
- , "delays" => "0.52/0/0.64/1.6"
- , "delay" => "2.7"
- , "to" => "something else"
- , "id" => "0039560E4"
- ));
- ?>
Add Comment
Please, Sign In to add comment