<commit_before><?php

namespace Knplabs\Bundle\MenuBundle;
use Knplabs\Bundle\MenuBundle\Renderer\RendererInterface;
use Knplabs\Bundle\MenuBundle\Renderer\ListRenderer;

/**
 * This is your base menu item. It roughly represents a single <li> tag
 * and is what you should interact with most of the time by default.
 * Decoupled from Symfony2, can be used in any PHP 5.3 project.
 * Originally taken from ioMenuPlugin (http://github.com/weaverryan/ioMenuPlugin)
 */
class MenuItem implements \ArrayAccess, \Countable, \IteratorAggregate
{
    /**
     * Properties on this menu item
     */
    protected
        $name             = null,    // the name of this menu item (used for id by parent menu)
        $label            = null,    // the label to output, name is used by default
        $linkAttributes   = array(), // an array of attributes for the item link
        $labelAttributes  = array(), // an array of attributes for the item text
        $uri              = null,    // the uri to use in the anchor tag
        $attributes       = array(); // an array of attributes for the li

    /**
     * Options related to rendering
     */
    protected
        $show             = true,    // boolean to render this menu
        $showChildren     = true;    // boolean to render the children of this menu

    /**
     * Metadata on this menu item
     */
    protected
        $children         = array(), // an array of MenuItem children
        $num              = null,    // the order number this menu is in its parent
        $parent           = null,    // parent MenuItem
        $isCurrent        = null,    // whether or not this menu item is current
        $currentUri       = null,    // the current uri to use for selecting current menu
        $currentAsLink    = true;    // boolean to render the current uri as a link or not

    /**
     * The renderer used to render this menu

     * @var RendererInterface
     */
    protected $renderer   = null;

    /**
     * Class constructor
     *
     * @param string $name      The name of this menu, which is how its parent will
     *                          reference it. Also used as label if label not specified
     * @param string $uri       The uri for this menu to use. If not specified,
     *                          text will be shown without a link
     * @param array $attributes Attributes to place on the li tag of this menu item
     */
    public function __construct($name, $uri = null, $attributes = array())
    {
        $this->name = (string) $name;
        $this->uri = $uri;
        $this->attributes = $attributes;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param  string $name
     * @return MenuItem
     */
    public function setName($name)
    {
        if ($this->name == $name) {
            return $this;
        }

        if ($this->getParent() && $this->getParent()->getChild($name)) {
            throw new \InvalidArgumentException('Cannot rename item, name is already used by sibling.');
        }

        $oldName = $this->name;
        $this->name = $name;

        if ($this->getParent()) {
            $this->getParent()->updateChildId($this, $oldName);
        }

        return $this;
    }

    /**
     * Updates id for child based on new name.
     *
     * Used internally after renaming item which has parent.
     *
     * @param MenuItem $child Item whose name has been changed.
     * @param string $oldName Old (previous) name of item.
     *
     */
    protected function updateChildId(MenuItem $child, $oldName)
    {
        $names = array_keys($this->getChildren());
        $items = array_values($this->getChildren());

        $offset = array_search($oldName, $names);
        $names[$offset] = $child->getName();

        $children = array_combine($names, $items);
        $this->setChildren($children);
    }

    /**
     * Get the uri for a menu item
     *
     * @return string
     */
    public function getUri()
    {
        return $this->uri;
    }


    /**
     * Set the uri for a menu item
     *
     * @param  string $uri The uri to set on this menu item
     * @return MenuItem
     */
    public function setUri($uri)
    {
        $this->uri = $uri;

        return $this;
    }

    /**
     * Returns the label that will be used to render this menu item
     *
     * Defaults to the name of no label was specified
     *
     * @return string
     */
    public function getLabel()
    {
        return ($this->label !== null) ? $this->label : $this->name;
    }

    /**
     * @param  string $label    The text to use when rendering this menu item
     * @return MenuItem
     */
    public function setLabel($label)
    {
        $this->label = $label;

        return $this;
    }

    /**
     * @return array
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

    /**
     * @param  array $attributes
     * @return MenuItem
     */
    public function setAttributes(array $attributes)
    {
        $this->attributes = $attributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getAttribute($name, $default = null)
    {
        if (isset($this->attributes[$name])) {
            return $this->attributes[$name];
        }

        return $default;
    }

    public function setAttribute($name, $value)
    {
        $this->attributes[$name] = $value;

        return $this;
    }

    /**
     * @return array
     */
    public function getLinkAttributes()
    {
        return $this->linkAttributes;
    }

    /**
     * @param  array $linkAttributes
     * @return MenuItem
     */
    public function setLinkAttributes(array $linkAttributes)
    {
        $this->linkAttributes = $linkAttributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getLinkAttribute($name, $default = null)
    {
        if (isset($this->linkAttributes[$name])) {
            return $this->linkAttributes[$name];
        }

        return $default;
    }

    /**
     * @param string $name
     * @param string $value
     *
     * @return MenuItem
     */
    public function setLinkAttribute($name, $value)
    {
        $this->linkAttributes[$name] = $value;

        return $this;
    }

    /**
     * @return array
     */
    public function getLabelAttributes()
    {
        return $this->labelAttributes;
    }

    /**
     * @param  array $labelAttributes
     * @return MenuItem
     */
    public function setLabelAttributes(array $labelAttributes)
    {
        $this->labelAttributes = $labelAttributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getLabelAttribute($name, $default = null)
    {
        if (isset($this->labelAttributes[$name])) {
            return $this->labelAttributes[$name];
        }

        return $default;
    }

    public function setLabelAttribute($name, $value)
    {
        $this->labelAttributes[$name] = $value;

        return $this;
    }

    /**
     * @return bool Whether or not this menu item should show its children.
     */
    public function getShowChildren()
    {
        return $this->showChildren;
    }

    /**
     * Set whether or not this menu item should show its children
     *
     * @param bool $bool
     * @return MenuItem
     */
    public function setShowChildren($bool)
    {
        $this->showChildren = (bool) $bool;

        return $this;
    }

    /**
     * @return bool Whether or not to show this menu item
     */
    public function getShow()
    {
        return $this->show;
    }

    /**
     * Set whether or not this menu to show this menu item
     *
     * @param bool $bool
     * @return MenuItem
     */
    public function setShow($bool)
    {
        $this->show = (bool) $bool;

        return $this;
    }

    /**
     * Whether or not this menu item should be rendered or not based on all the available factors
     *
     * @return boolean
     */
    public function shouldBeRendered()
    {
        return $this->getShow();
    }

    /**
     * Whether or not this menu item should be rendered as a link based on the available factors
     *
     * @return boolean
     */
    public function shouldBeRenderedAsLink()
    {
        return ($this->getIsCurrent() && $this->getParent()->getCurrentAsLink()) || !$this->getIsCurrent();
    }

    /**
     * Add a child menu item to this menu
     *
     * @param mixed   $child    An MenuItem object or the name of a new menu to create
     * @param string  $uri    If creating a new menu, the uri for that menu
     * @param string  $attributes  If creating a new menu, the attributes for that menu
     * @param string  $class    The class for menu item, if it needs to be created
     *
     * @return MenuItem The child menu item
     */
    public function addChild($child, $uri = null, $attributes = array(), $class = null)
    {
        if (!$child instanceof MenuItem) {
            $child = $this->createChild($child, $uri, $attributes, $class);
        }
        elseif ($child->getParent()) {
            throw new \InvalidArgumentException('Cannot add menu item as child, it already belongs to another menu (e.g. has a parent).');
        }

        $child->setParent($this);
        $child->setShowChildren($this->getShowChildren());
        $child->setCurrentUri($this->getCurrentUri());
        $child->setNum($this->count());

        $this->children[$child->getName()] = $child;

        return $child;
    }<commit_msg>don't render item as link when no URI is specified<commit_after><?php

namespace Knplabs\Bundle\MenuBundle;
use Knplabs\Bundle\MenuBundle\Renderer\RendererInterface;
use Knplabs\Bundle\MenuBundle\Renderer\ListRenderer;

/**
 * This is your base menu item. It roughly represents a single <li> tag
 * and is what you should interact with most of the time by default.
 * Decoupled from Symfony2, can be used in any PHP 5.3 project.
 * Originally taken from ioMenuPlugin (http://github.com/weaverryan/ioMenuPlugin)
 */
class MenuItem implements \ArrayAccess, \Countable, \IteratorAggregate
{
    /**
     * Properties on this menu item
     */
    protected
        $name             = null,    // the name of this menu item (used for id by parent menu)
        $label            = null,    // the label to output, name is used by default
        $linkAttributes   = array(), // an array of attributes for the item link
        $labelAttributes  = array(), // an array of attributes for the item text
        $uri              = null,    // the uri to use in the anchor tag
        $attributes       = array(); // an array of attributes for the li

    /**
     * Options related to rendering
     */
    protected
        $show             = true,    // boolean to render this menu
        $showChildren     = true;    // boolean to render the children of this menu

    /**
     * Metadata on this menu item
     */
    protected
        $children         = array(), // an array of MenuItem children
        $num              = null,    // the order number this menu is in its parent
        $parent           = null,    // parent MenuItem
        $isCurrent        = null,    // whether or not this menu item is current
        $currentUri       = null,    // the current uri to use for selecting current menu
        $currentAsLink    = true;    // boolean to render the current uri as a link or not

    /**
     * The renderer used to render this menu

     * @var RendererInterface
     */
    protected $renderer   = null;

    /**
     * Class constructor
     *
     * @param string $name      The name of this menu, which is how its parent will
     *                          reference it. Also used as label if label not specified
     * @param string $uri       The uri for this menu to use. If not specified,
     *                          text will be shown without a link
     * @param array $attributes Attributes to place on the li tag of this menu item
     */
    public function __construct($name, $uri = null, $attributes = array())
    {
        $this->name = (string) $name;
        $this->uri = $uri;
        $this->attributes = $attributes;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param  string $name
     * @return MenuItem
     */
    public function setName($name)
    {
        if ($this->name == $name) {
            return $this;
        }

        if ($this->getParent() && $this->getParent()->getChild($name)) {
            throw new \InvalidArgumentException('Cannot rename item, name is already used by sibling.');
        }

        $oldName = $this->name;
        $this->name = $name;

        if ($this->getParent()) {
            $this->getParent()->updateChildId($this, $oldName);
        }

        return $this;
    }

    /**
     * Updates id for child based on new name.
     *
     * Used internally after renaming item which has parent.
     *
     * @param MenuItem $child Item whose name has been changed.
     * @param string $oldName Old (previous) name of item.
     *
     */
    protected function updateChildId(MenuItem $child, $oldName)
    {
        $names = array_keys($this->getChildren());
        $items = array_values($this->getChildren());

        $offset = array_search($oldName, $names);
        $names[$offset] = $child->getName();

        $children = array_combine($names, $items);
        $this->setChildren($children);
    }

    /**
     * Get the uri for a menu item
     *
     * @return string
     */
    public function getUri()
    {
        return $this->uri;
    }


    /**
     * Set the uri for a menu item
     *
     * @param  string $uri The uri to set on this menu item
     * @return MenuItem
     */
    public function setUri($uri)
    {
        $this->uri = $uri;

        return $this;
    }

    /**
     * Returns the label that will be used to render this menu item
     *
     * Defaults to the name of no label was specified
     *
     * @return string
     */
    public function getLabel()
    {
        return ($this->label !== null) ? $this->label : $this->name;
    }

    /**
     * @param  string $label    The text to use when rendering this menu item
     * @return MenuItem
     */
    public function setLabel($label)
    {
        $this->label = $label;

        return $this;
    }

    /**
     * @return array
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

    /**
     * @param  array $attributes
     * @return MenuItem
     */
    public function setAttributes(array $attributes)
    {
        $this->attributes = $attributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getAttribute($name, $default = null)
    {
        if (isset($this->attributes[$name])) {
            return $this->attributes[$name];
        }

        return $default;
    }

    public function setAttribute($name, $value)
    {
        $this->attributes[$name] = $value;

        return $this;
    }

    /**
     * @return array
     */
    public function getLinkAttributes()
    {
        return $this->linkAttributes;
    }

    /**
     * @param  array $linkAttributes
     * @return MenuItem
     */
    public function setLinkAttributes(array $linkAttributes)
    {
        $this->linkAttributes = $linkAttributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getLinkAttribute($name, $default = null)
    {
        if (isset($this->linkAttributes[$name])) {
            return $this->linkAttributes[$name];
        }

        return $default;
    }

    /**
     * @param string $name
     * @param string $value
     *
     * @return MenuItem
     */
    public function setLinkAttribute($name, $value)
    {
        $this->linkAttributes[$name] = $value;

        return $this;
    }

    /**
     * @return array
     */
    public function getLabelAttributes()
    {
        return $this->labelAttributes;
    }

    /**
     * @param  array $labelAttributes
     * @return MenuItem
     */
    public function setLabelAttributes(array $labelAttributes)
    {
        $this->labelAttributes = $labelAttributes;

        return $this;
    }

    /**
     * @param  string $name     The name of the attribute to return
     * @param  mixed  $default  The value to return if the attribute doesn't exist
     *
     * @return mixed
     */
    public function getLabelAttribute($name, $default = null)
    {
        if (isset($this->labelAttributes[$name])) {
            return $this->labelAttributes[$name];
        }

        return $default;
    }

    public function setLabelAttribute($name, $value)
    {
        $this->labelAttributes[$name] = $value;

        return $this;
    }

    /**
     * @return bool Whether or not this menu item should show its children.
     */
    public function getShowChildren()
    {
        return $this->showChildren;
    }

    /**
     * Set whether or not this menu item should show its children
     *
     * @param bool $bool
     * @return MenuItem
     */
    public function setShowChildren($bool)
    {
        $this->showChildren = (bool) $bool;

        return $this;
    }

    /**
     * @return bool Whether or not to show this menu item
     */
    public function getShow()
    {
        return $this->show;
    }

    /**
     * Set whether or not this menu to show this menu item
     *
     * @param bool $bool
     * @return MenuItem
     */
    public function setShow($bool)
    {
        $this->show = (bool) $bool;

        return $this;
    }

    /**
     * Whether or not this menu item should be rendered or not based on all the available factors
     *
     * @return boolean
     */
    public function shouldBeRendered()
    {
        return $this->getShow();
    }

    /**
     * Whether or not this menu item should be rendered as a link based on the available factors
     *
     * @return boolean
     */
    public function shouldBeRenderedAsLink()
    {
        return ($this->getIsCurrent() && $this->getParent()->getCurrentAsLink())
                || (!$this->getIsCurrent() && $this->getUri());
    }

    /**
     * Add a child menu item to this menu
     *
     * @param mixed   $child    An MenuItem object or the name of a new menu to create
     * @param string  $uri    If creating a new menu, the uri for that menu
     * @param string  $attributes  If creating a new menu, the attributes for that menu
     * @param string  $class    The class for menu item, if it needs to be created
     *
     * @return MenuItem The child menu item
     */
    public function addChild($child, $uri = null, $attributes = array(), $class = null)
    {
        if (!$child instanceof MenuItem) {
            $child = $this->createChild($child, $uri, $attributes, $class);
        }
        elseif ($child->getParent()) {
            throw new \InvalidArgumentException('Cannot add menu item as child, it already belongs to another menu (e.g. has a parent).');
        }

        $child->setParent($this);
        $child->setShowChildren($this->getShowChildren());
        $child->setCurrentUri($this->getCurrentUri());
        $child->setNum($this->count());

        $this->children[$child->getName()] = $child;

        return $child;
    }
{'fraction_non_alphanumeric': 0.10881597153587666, 'fraction_numerical': 0.00029650128483890096, 'mean_word_length': 1.9030268254195954, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '47324', 'n_tokens_mistral': 5579, 'n_tokens_neox': 5464, 'n_words': 1860}