/**
  * @expectedException InvalidArgumentException
  */
 public function testReorderingWithTooManyItemNames()
 {
     $menu = new MenuItem('root');
     $menu->addChild('c1');
     $menu->reorderChildren(array('c1', 'c3'));
 }
Beispiel #2
0
    /**
     * Moves child to specified position. Rearange other children accordingly.
     *
     * @param MenuItem $child Child to move.
     * @param numeric $position Position to move child to.
     */
    public function moveChildToPosition(MenuItem $child, $position)
    {
        $name = $child->getName();
        $order = array_keys($this->children);

        $oldPosition = array_search($name, $order);
        unset($order[$oldPosition]);

        $order = array_values($order);

        array_splice($order, $position, 0, $name);
        $this->reorderChildren($order);
    }
    /**
     *
     * @param MenuItem $menuItem
     * @return array
     */
    public function getMenuAttributes(MenuItem $item)
    {
        // if we don't have access or this item is marked to not be shown
        if (!$item->shouldBeRendered()) {
            return;
        }

        $depth = $item->getLevel();
        $maxDepth = $item->getMaxDepth();
            
        $class = array();
        
        if ($maxDepth > 1) {
            $class[] = "deeper";
        }
        $class[] = "level_{$depth}";

        // retrieve the attributes and put the final class string back on it
        $attributes = $item->getAttributes();
        if (!empty($class)) {
            $attributes['class'] = implode(' ', $class);
        }

        return $attributes;
    }
    /**
     * Renders the link in a a tag with link attributes or
     * the label in a span tag with label attributes
     *
     * Tests if item has a an uri and if not tests if it's
     * the current item and if the text has to be rendered
     * as a link or not.
     *
     * @param MenuItem $item The item to render the link or label for
     * @return string
     */
    public function renderLink($item)
    {
        $text = '';
        if (!$item->getUri()) {
            $text = sprintf('<span%s>%s</span>', $this->renderHtmlAttributes($item->getLabelAttributes()), $item->renderLabel());
        }
        else {
            if (($item->getIsCurrent() && $item->getParent()->getCurrentAsLink())
                || !$item->getIsCurrent()) {
                $text = sprintf('<a href="%s"%s>%s</a>', $item->getUri(), $this->renderHtmlAttributes($item->getLinkAttributes()), $item->renderLabel());
            }
            else {
                $text = sprintf('<span%s>%s</span>', $this->renderHtmlAttributes($item->getLabelAttributes()), $item->renderLabel());
            }
        }

        return $this->format($text, 'link', $item->getLevel());
    }