[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * This file is part of the DebugBar package. 4 * 5 * (c) 2013 Maxime Bouroumeau-Fuseau 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 namespace DebugBar\DataCollector; 12 13 use ArrayAccess; 14 use DebugBar\DebugBarException; 15 16 /** 17 * Aggregates data from multiple collectors 18 * 19 * <code> 20 * $aggcollector = new AggregateCollector('foobar'); 21 * $aggcollector->addCollector(new MessagesCollector('msg1')); 22 * $aggcollector->addCollector(new MessagesCollector('msg2')); 23 * $aggcollector['msg1']->addMessage('hello world'); 24 * </code> 25 */ 26 class AggregatedCollector implements DataCollectorInterface, ArrayAccess 27 { 28 protected $name; 29 30 protected $mergeProperty; 31 32 protected $sort; 33 34 protected $collectors = array(); 35 36 /** 37 * @param string $name 38 * @param string $mergeProperty 39 * @param boolean $sort 40 */ 41 public function __construct($name, $mergeProperty = null, $sort = false) 42 { 43 $this->name = $name; 44 $this->mergeProperty = $mergeProperty; 45 $this->sort = $sort; 46 } 47 48 /** 49 * @param DataCollectorInterface $collector 50 */ 51 public function addCollector(DataCollectorInterface $collector) 52 { 53 $this->collectors[$collector->getName()] = $collector; 54 } 55 56 /** 57 * @return array 58 */ 59 public function getCollectors() 60 { 61 return $this->collectors; 62 } 63 64 /** 65 * Merge data from one of the key/value pair of the collected data 66 * 67 * @param string $property 68 */ 69 public function setMergeProperty($property) 70 { 71 $this->mergeProperty = $property; 72 } 73 74 /** 75 * @return string 76 */ 77 public function getMergeProperty() 78 { 79 return $this->mergeProperty; 80 } 81 82 /** 83 * Sorts the collected data 84 * 85 * If true, sorts using sort() 86 * If it is a string, sorts the data using the value from a key/value pair of the array 87 * 88 * @param bool|string $sort 89 */ 90 public function setSort($sort) 91 { 92 $this->sort = $sort; 93 } 94 95 /** 96 * @return bool|string 97 */ 98 public function getSort() 99 { 100 return $this->sort; 101 } 102 103 /** 104 * @return array 105 */ 106 public function collect() 107 { 108 $aggregate = array(); 109 foreach ($this->collectors as $collector) { 110 $data = $collector->collect(); 111 if ($this->mergeProperty !== null) { 112 $data = $data[$this->mergeProperty]; 113 } 114 $aggregate = array_merge($aggregate, $data); 115 } 116 117 return $this->sort($aggregate); 118 } 119 120 /** 121 * Sorts the collected data 122 * 123 * @param array $data 124 * @return array 125 */ 126 protected function sort($data) 127 { 128 if (is_string($this->sort)) { 129 $p = $this->sort; 130 usort($data, function ($a, $b) use ($p) { 131 if ($a[$p] == $b[$p]) { 132 return 0; 133 } 134 return $a[$p] < $b[$p] ? -1 : 1; 135 }); 136 } elseif ($this->sort === true) { 137 sort($data); 138 } 139 return $data; 140 } 141 142 /** 143 * @return string 144 */ 145 public function getName() 146 { 147 return $this->name; 148 } 149 150 // -------------------------------------------- 151 // ArrayAccess implementation 152 153 /** 154 * @param mixed $key 155 * @param mixed $value 156 * @throws DebugBarException 157 */ 158 public function offsetSet($key, $value): void 159 { 160 throw new DebugBarException("AggregatedCollector[] is read-only"); 161 } 162 163 /** 164 * @param mixed $key 165 * @return mixed 166 */ 167 168 #[\ReturnTypeWillChange] 169 public function offsetGet($key) 170 { 171 return $this->collectors[$key]; 172 } 173 174 /** 175 * @param mixed $key 176 * @return bool 177 */ 178 public function offsetExists($key): bool 179 { 180 return isset($this->collectors[$key]); 181 } 182 183 /** 184 * @param mixed $key 185 * @throws DebugBarException 186 */ 187 public function offsetUnset($key): void 188 { 189 throw new DebugBarException("AggregatedCollector[] is read-only"); 190 } 191 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |