Elgg  Version 1.9
ElggPriorityList.php
Go to the documentation of this file.
1 <?php
96  implements Iterator, Countable {
97 
103  private $elements = array();
104 
110  public function __construct(array $elements = array()) {
111  if ($elements) {
112  foreach ($elements as $priority => $element) {
113  $this->add($element, $priority);
114  }
115  }
116  }
117 
133  public function add($element, $priority = null, $exact = false) {
134  if ($priority !== null && !is_numeric($priority)) {
135  return false;
136  } else {
138  }
139 
140  $this->elements[$priority] = $element;
141  $this->sorted = false;
142  return $priority;
143  }
144 
155  public function remove($element, $strict = false) {
156  $index = array_search($element, $this->elements, $strict);
157  if ($index !== false) {
158  unset($this->elements[$index]);
159  return true;
160  } else {
161  return false;
162  }
163  }
164 
173  public function move($element, $new_priority, $strict = false) {
174  $new_priority = (int) $new_priority;
175 
176  $current_priority = $this->getPriority($element, $strict);
177  if ($current_priority === false) {
178  return false;
179  }
180 
181  if ($current_priority == $new_priority) {
182  return true;
183  }
184 
185  // move the actual element so strict operations still work
186  $element = $this->getElement($current_priority);
187  unset($this->elements[$current_priority]);
188  return $this->add($element, $new_priority);
189  }
190 
196  public function getElements() {
197  $this->sortIfUnsorted();
198  return $this->elements;
199  }
200 
214  public function sort($callback = null) {
215  if (!$callback) {
216  ksort($this->elements, SORT_NUMERIC);
217  } else {
218  $sorted = call_user_func($callback, $this->elements);
219 
220  if (!$sorted) {
221  return false;
222  }
223 
224  $this->elements = $sorted;
225  }
226 
227  $this->sorted = true;
228  return true;
229  }
230 
236  private function sortIfUnsorted() {
237  if (!$this->sorted) {
238  return $this->sort();
239  }
240  }
241 
248  public function getNextPriority($near = 0) {
249  $near = (int) $near;
250 
251  while (array_key_exists($near, $this->elements)) {
252  $near++;
253  }
254 
255  return $near;
256  }
257 
267  public function getPriority($element, $strict = false) {
268  return array_search($element, $this->elements, $strict);
269  }
270 
277  public function getElement($priority) {
278  return (isset($this->elements[$priority])) ? $this->elements[$priority] : false;
279  }
280 
288  public function contains($element, $strict = false) {
289  return $this->getPriority($element, $strict) !== false;
290  }
291 
292 
293  /**********************
294  * Interface methods *
295  **********************/
296 
307  public function rewind() {
308  $this->sortIfUnsorted();
309  return reset($this->elements);
310  }
311 
318  public function current() {
319  $this->sortIfUnsorted();
320  return current($this->elements);
321  }
322 
329  public function key() {
330  $this->sortIfUnsorted();
331  return key($this->elements);
332  }
333 
340  public function next() {
341  $this->sortIfUnsorted();
342  return next($this->elements);
343  }
344 
351  public function valid() {
352  $this->sortIfUnsorted();
353  $key = key($this->elements);
354  return ($key !== null && $key !== false);
355  }
356 
363  public function count() {
364  return count($this->elements);
365  }
366 }
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.
$exact
Definition: add_panel.php:12
$key
Definition: summary.php:34
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.
__construct(array $elements=array())
Create a new priority list.
getPriority($element, $strict=false)
Returns the priority of an element if it exists in the list.
valid()
PHP Iterator Interface.
$priority