[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Stdlib; 11 12 use Countable; 13 use Iterator; 14 15 class PriorityList implements Iterator, Countable 16 { 17 const EXTR_DATA = 0x00000001; 18 const EXTR_PRIORITY = 0x00000002; 19 const EXTR_BOTH = 0x00000003; 20 /** 21 * Internal list of all items. 22 * 23 * @var array[] 24 */ 25 protected $items = array(); 26 27 /** 28 * Serial assigned to items to preserve LIFO. 29 * 30 * @var int 31 */ 32 protected $serial = 0; 33 34 /** 35 * Serial order mode 36 * @var integer 37 */ 38 protected $isLIFO = 1; 39 40 /** 41 * Internal counter to avoid usage of count(). 42 * 43 * @var int 44 */ 45 protected $count = 0; 46 47 /** 48 * Whether the list was already sorted. 49 * 50 * @var bool 51 */ 52 protected $sorted = false; 53 54 /** 55 * Insert a new item. 56 * 57 * @param string $name 58 * @param mixed $value 59 * @param int $priority 60 * 61 * @return void 62 */ 63 public function insert($name, $value, $priority = 0) 64 { 65 $this->sorted = false; 66 $this->count++; 67 68 $this->items[$name] = array( 69 'data' => $value, 70 'priority' => (int) $priority, 71 'serial' => $this->serial++, 72 ); 73 } 74 75 /** 76 * @param string $name 77 * @param int $priority 78 * 79 * @return $this 80 * 81 * @throws \Exception 82 */ 83 public function setPriority($name, $priority) 84 { 85 if (!isset($this->items[$name])) { 86 throw new \Exception("item $name not found"); 87 } 88 89 $this->items[$name]['priority'] = (int) $priority; 90 $this->sorted = false; 91 92 return $this; 93 } 94 95 /** 96 * Remove a item. 97 * 98 * @param string $name 99 * @return void 100 */ 101 public function remove($name) 102 { 103 if (isset($this->items[$name])) { 104 $this->count--; 105 } 106 107 unset($this->items[$name]); 108 } 109 110 /** 111 * Remove all items. 112 * 113 * @return void 114 */ 115 public function clear() 116 { 117 $this->items = array(); 118 $this->serial = 0; 119 $this->count = 0; 120 $this->sorted = false; 121 } 122 123 /** 124 * Get a item. 125 * 126 * @param string $name 127 * @return mixed 128 */ 129 public function get($name) 130 { 131 if (!isset($this->items[$name])) { 132 return; 133 } 134 135 return $this->items[$name]['data']; 136 } 137 138 /** 139 * Sort all items. 140 * 141 * @return void 142 */ 143 protected function sort() 144 { 145 if (!$this->sorted) { 146 uasort($this->items, array($this, 'compare')); 147 $this->sorted = true; 148 } 149 } 150 151 /** 152 * Compare the priority of two items. 153 * 154 * @param array $item1, 155 * @param array $item2 156 * @return int 157 */ 158 protected function compare(array $item1, array $item2) 159 { 160 return ($item1['priority'] === $item2['priority']) 161 ? ($item1['serial'] > $item2['serial'] ? -1 : 1) * $this->isLIFO 162 : ($item1['priority'] > $item2['priority'] ? -1 : 1); 163 } 164 165 /** 166 * Get/Set serial order mode 167 * 168 * @param bool|null $flag 169 * 170 * @return bool 171 */ 172 public function isLIFO($flag = null) 173 { 174 if ($flag !== null) { 175 $isLifo = $flag === true ? 1 : -1; 176 177 if ($isLifo !== $this->isLIFO) { 178 $this->isLIFO = $isLifo; 179 $this->sorted = false; 180 } 181 } 182 183 return 1 === $this->isLIFO; 184 } 185 186 /** 187 * {@inheritDoc} 188 */ 189 public function rewind() 190 { 191 $this->sort(); 192 reset($this->items); 193 } 194 195 /** 196 * {@inheritDoc} 197 */ 198 public function current() 199 { 200 $this->sorted || $this->sort(); 201 $node = current($this->items); 202 203 return $node ? $node['data'] : false; 204 } 205 206 /** 207 * {@inheritDoc} 208 */ 209 public function key() 210 { 211 $this->sorted || $this->sort(); 212 return key($this->items); 213 } 214 215 /** 216 * {@inheritDoc} 217 */ 218 public function next() 219 { 220 $node = next($this->items); 221 222 return $node ? $node['data'] : false; 223 } 224 225 /** 226 * {@inheritDoc} 227 */ 228 public function valid() 229 { 230 return current($this->items) !== false; 231 } 232 233 /** 234 * @return self 235 */ 236 public function getIterator() 237 { 238 return clone $this; 239 } 240 241 /** 242 * {@inheritDoc} 243 */ 244 public function count() 245 { 246 return $this->count; 247 } 248 249 /** 250 * Return list as array 251 * 252 * @param int $flag 253 * 254 * @return array 255 */ 256 public function toArray($flag = self::EXTR_DATA) 257 { 258 $this->sort(); 259 260 if ($flag == self::EXTR_BOTH) { 261 return $this->items; 262 } 263 264 return array_map( 265 function ($item) use ($flag) { 266 return ($flag == PriorityList::EXTR_PRIORITY) ? $item['priority'] : $item['data']; 267 }, 268 $this->items 269 ); 270 } 271 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |