Elgg  Version 3.0
ElggPriorityList.php
Go to the documentation of this file.
1 <?php
96  implements \Iterator, \Countable {
97 
103  private $elements = [];
104 
110  private $sorted = false;
111 
117  public function __construct(array $elements = []) {
118  foreach ($elements as $priority => $element) {
119  $this->add($element, $priority);
120  }
121  }
122 
140  public function add($element, $priority = null, $exact = false) {
141  if ($priority !== null && !is_numeric($priority)) {
142  return false;
143  } else {
145  }
146 
147  $this->elements[$priority] = $element;
148  $this->sorted = false;
149 
150  return $priority;
151  }
152 
164  public function remove($element, $strict = false) {
165  $index = array_search($element, $this->elements, $strict);
166  if ($index === false) {
167  return false;
168  }
169 
170  unset($this->elements[$index]);
171  return true;
172  }
173 
183  public function move($element, $new_priority, $strict = false) {
184  $new_priority = (int) $new_priority;
185 
186  $current_priority = $this->getPriority($element, $strict);
187  if ($current_priority === false) {
188  return false;
189  }
190 
191  if ($current_priority == $new_priority) {
192  return true;
193  }
194 
195  // move the actual element so strict operations still work
196  $element = $this->getElement($current_priority);
197  unset($this->elements[$current_priority]);
198  return ($this->add($element, $new_priority) !== false);
199  }
200 
206  public function getElements() {
207  $this->sortIfUnsorted();
208  return $this->elements;
209  }
210 
224  public function sort($callback = null) {
225  if (!$callback) {
226  ksort($this->elements, SORT_NUMERIC);
227  } else {
228  $sorted = call_user_func($callback, $this->elements);
229 
230  if (!$sorted) {
231  return false;
232  }
233 
234  $this->elements = $sorted;
235  }
236 
237  $this->sorted = true;
238  return true;
239  }
240 
246  private function sortIfUnsorted() {
247  if (!$this->sorted) {
248  $this->sort();
249  }
250  }
251 
258  public function getNextPriority($near = 0) {
259  $near = (int) $near;
260 
261  while (array_key_exists($near, $this->elements)) {
262  $near++;
263  }
264 
265  return $near;
266  }
267 
278  public function getPriority($element, $strict = false) {
279  $prio = array_search($element, $this->elements, $strict);
280 
281  return ($prio === false) ? false : (int) $prio;
282  }
283 
290  public function getElement($priority) {
291  return (isset($this->elements[$priority])) ? $this->elements[$priority] : false;
292  }
293 
301  public function contains($element, $strict = false) {
302  return $this->getPriority($element, $strict) !== false;
303  }
304 
305 
306  /**********************
307  * Interface methods *
308  **********************/
309 
320  public function rewind() {
321  $this->sortIfUnsorted();
322  return reset($this->elements);
323  }
324 
331  public function current() {
332  $this->sortIfUnsorted();
333  return current($this->elements);
334  }
335 
342  public function key() {
343  $this->sortIfUnsorted();
344  return key($this->elements);
345  }
346 
353  public function next() {
354  $this->sortIfUnsorted();
355  return next($this->elements);
356  }
357 
364  public function valid() {
365  $this->sortIfUnsorted();
366  $key = key($this->elements);
367  return ($key !== null && $key !== false);
368  }
369 
376  public function count() {
377  return count($this->elements);
378  }
379 }
getElement($priority)
Returns the element at $priority.
add($element, $priority=null, $exact=false)
Adds an element to the list.
contains($element, $strict=false)
Returns if the list contains $element.
getNextPriority($near=0)
Returns the next priority available.
getElements()
Returns the elements.
key()
PHP Iterator Interface.
__construct(array $elements=[])
Create a new priority list.
count()
Countable interface.
move($element, $new_priority, $strict=false)
Move an existing element to a new priority.
current()
PHP Iterator Interface.
next()
PHP Iterator Interface.
sort($callback=null)
Sort the elements optionally by a callback function.
getPriority($element, $strict=false)
Returns the priority of an element if it exists in the list.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
valid()
PHP Iterator Interface.
$index
Definition: gallery.php:47
$priority