96 implements \Iterator, \Countable {
103 private $elements = [];
110 private $sorted =
false;
118 foreach ($elements as
$priority => $element) {
148 $this->sorted =
false;
164 public function remove($element, $strict =
false) {
165 $index = array_search($element, $this->elements, $strict);
170 unset($this->elements[
$index]);
183 public function move($element, $new_priority, $strict =
false) {
184 $new_priority = (int) $new_priority;
186 $current_priority = $this->
getPriority($element, $strict);
187 if ($current_priority ===
false) {
191 if ($current_priority == $new_priority) {
196 $element = $this->
getElement($current_priority);
197 unset($this->elements[$current_priority]);
198 return ($this->
add($element, $new_priority) !==
false);
207 $this->sortIfUnsorted();
208 return $this->elements;
224 public function sort($callback = null) {
226 ksort($this->elements, SORT_NUMERIC);
228 $sorted = call_user_func($callback, $this->elements);
234 $this->elements = $sorted;
237 $this->sorted =
true;
246 private function sortIfUnsorted() {
247 if (!$this->sorted) {
261 while (array_key_exists($near, $this->elements)) {
279 $prio = array_search($element, $this->elements, $strict);
281 return ($prio ===
false) ?
false : (int) $prio;
301 public function contains($element, $strict =
false) {
302 return $this->
getPriority($element, $strict) !==
false;
321 $this->sortIfUnsorted();
322 return reset($this->elements);
332 $this->sortIfUnsorted();
333 return current($this->elements);
343 $this->sortIfUnsorted();
344 return key($this->elements);
354 $this->sortIfUnsorted();
355 return next($this->elements);
365 $this->sortIfUnsorted();
367 return (
$key !== null &&
$key !==
false);
377 return count($this->elements);
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
valid()
PHP Iterator Interface.