Navigation: Add method order()

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-03 08:52:02 +02:00
parent 00447488ee
commit 1fb5c96ef1
1 changed files with 35 additions and 0 deletions

View File

@ -109,6 +109,7 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
*/ */
public function getIterator() public function getIterator()
{ {
$this->order();
return new ArrayIterator($this->items); return new ArrayIterator($this->items);
} }
@ -269,6 +270,40 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
return new RecursiveNavigationRenderer($this); return new RecursiveNavigationRenderer($this);
} }
/**
* Order this navigation's items
*
* @return $this
*/
public function order()
{
uasort($this->items, array($this, 'compareItems'));
foreach ($this->items as $item) {
if ($item->hasChildren()) {
$item->getChildren()->order();
}
}
return $this;
}
/**
* Return whether the first item is less than, more than or equal to the second one
*
* @param NavigationItem $a
* @param NavigationItem $b
*
* @return int
*/
protected function compareItems(NavigationItem $a, NavigationItem $b)
{
if ($a->getPriority() === $b->getPriority()) {
return strcasecmp($a->getLabel(), $b->getLabel());
}
return $a->getPriority() > $b->getPriority() ? 1 : -1;
}
/** /**
* Merge this navigation with the given one * Merge this navigation with the given one
* *