Guest User

Untitled

a guest
Jun 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2. define("HISTORY_OVERWRITE",1);
  3. define("HISTORY_PREFERENCES",2);
  4. define("HISTORY_ARRAY",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. return $opt;
  34. }
  35.  
  36. public function setPreferences($pref) {
  37. $this->preferences = $pref;
  38. return $pref;
  39. }
  40.  
  41. public function setHistory($hist) {
  42. $this->history = $hist;
  43. return $hist;
  44. }
  45.  
  46. public function merge() {
  47. foreach($this->history as $hist) {
  48. $this->_mergeElement($hist);
  49. }
  50. return $this->final;
  51. }
  52.  
  53. private function _mergeElement($hist) {
  54. foreach($hist as $key=>$elem) {
  55. if($this->_checkPermission($key,HISTORY_OVERWRITE)) {
  56. if($this->_checkPermission($key,HISTORY_ARRAY)) {
  57. $this->final[$key][] = $elem;
  58. } else {
  59. if(array_key_exists($key,$this->final)) {
  60. $this->final[$key] = ($this->_checkPermission($key,HISTORY_PREFERENCES)) ? $this->_checkPreferences($key,$elem,$this->final[$key]) : $elem;
  61. } else $this->final[$key] = $elem;
  62. }
  63. } else $this->final[$key][] = $elem;
  64. }
  65.  
  66. foreach($this->options as $key=>$elem) {
  67. if($this->_checkPermission($key,HISTORY_OVERWRITE|HISTORY_ARRAY)) {
  68. $size = count($this->final[$key]);
  69.  
  70. /*
  71. // double loop checks values for equality
  72. for($i = 0; $i < count($this->final[$key])-1; $i++) {
  73. for($j = $i+1; $j < count($this->final[$key]); $j++) {
  74.  
  75. # wenn die 2 elemente identisch sind
  76. if($this->final[$key][$i] == $this->final[$key][$j]) {
  77. foreach($this->final as $k=>$e) {
  78. if(is_array($e)) {
  79. $this->final[$k][$i] = ($this->_checkPermission($k,HISTORY_PREFERENCES)) ? $this->_checkPreferences($k,$this->final[$k][$j],$this->final[$k][$i]) : $this->final[$k][$j];
  80. unset($this->final[$k][$j]);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. //*/
  87. }
  88. }
  89. }
  90.  
  91. private function _checkPermission($key,$option) {
  92. if(!array_key_exists($key,$this->options)) return false;
  93. return ($option & $this->options[$key]) ? true : false;
  94. }
  95.  
  96. private function _checkPreferences($key,$e1,$e2) {
  97. if(array_key_exists($key,$this->preferences)) {
  98. foreach($this->preferences[$key] as $e) {
  99. if($e1 == $e or $e2 == $e) return $e;
  100. }
  101. } else return $e1;
  102. }
  103. }
  104.  
  105. $o = new History("t");
  106. $o->setOptions(array(
  107. // 'status' => HISTORY_OVERWRITE,
  108. 'dsn' => HISTORY_OVERWRITE,
  109. 'delays' => HISTORY_OVERWRITE,
  110. 'delay' => HISTORY_OVERWRITE,
  111. // 'to' => HISTORY_ARRAY,
  112. 'relay' => HISTORY_OVERWRITE,
  113. 'id' => HISTORY_OVERWRITE
  114. ));
  115.  
  116. $o->setPreferences(array(
  117. 'status' => array('sent','deffered')
  118. ));
  119.  
  120. $arr = $o->add(array(
  121. "status" => "sent"
  122. , "dsn" => "2.0.0"
  123. , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
  124. , "delays" => "0.52/0/0.64/1.6"
  125. , "delay" => "2.7"
  126. , "to" => "testsetes"
  127. , "id" => "0039560E4"
  128. ));
  129. print_r($arr);
  130. $arr = $o->add(array(
  131. "status" => "deffered"
  132. , "dsn" => "2.0.0"
  133. , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
  134. , "delays" => "0.52/0/0.64/1.6"
  135. , "delay" => "2.7"
  136. , "to" => "something else"
  137. , "id" => "0039560E4"
  138. ));
  139. $arr = $o->add(array(
  140. "status" => "sent"
  141. , "dsn" => "2.0.0"
  142. , "relay" => "mailin-03.mx.aol.com[205.188.59.193] =>25"
  143. , "delays" => "0.52/0/0.64/1.6"
  144. , "delay" => "2.7"
  145. , "to" => "something else"
  146. , "id" => "0039560E4"
  147. ));
  148. print_r($arr);
Add Comment
Please, Sign In to add comment