Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
PriorityList.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009
010 namespace Zend\Stdlib;
011
012 use Countable;
013 use Iterator;
014
015 class PriorityList implements Iterator, Countable
016 {
017 const EXTR_DATA = 0x00000001;
018 const EXTR_PRIORITY = 0x00000002;
019 const EXTR_BOTH = 0x00000003;
020 /**
021 * Internal list of all items.
022 *
023 * @var array[]
024 */
025 protected $items = array();
026
027 /**
028 * Serial assigned to items to preserve LIFO.
029 *
030 * @var int
031 */
032 protected $serial = 0;
033
034 /**
035 * Serial order mode
036 * @var integer
037 */
038 protected $isLIFO = 1;
039
040 /**
041 * Internal counter to avoid usage of count().
042 *
043 * @var int
044 */
045 protected $count = 0;
046
047 /**
048 * Whether the list was already sorted.
049 *
050 * @var bool
051 */
052 protected $sorted = false;
053
054 /**
055 * Insert a new item.
056 *
057 * @param string $name
058 * @param mixed $value
059 * @param int $priority
060 *
061 * @return void
062 */
063 public function insert($name, $value, $priority = 0)
064 {
065 $this->sorted = false;
066 $this->count++;
067
068 $this->items[$name] = array(
069 'data' => $value,
070 'priority' => (int) $priority,
071 'serial' => $this->serial++,
072 );
073 }
074
075 /**
076 * @param string $name
077 * @param int $priority
078 *
079 * @return $this
080 *
081 * @throws \Exception
082 */
083 public function setPriority($name, $priority)
084 {
085 if (!isset($this->items[$name])) {
086 throw new \Exception("item $name not found");
087 }
088
089 $this->items[$name]['priority'] = (int) $priority;
090 $this->sorted = false;
091
092 return $this;
093 }
094
095 /**
096 * Remove a item.
097 *
098 * @param string $name
099 * @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 }
272