<commit_before><?php
/**
 * ListMenuBuilder.php
 *
 * @since 20/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\AppBundle\Menu\Menu;

use Enhavo\Bundle\AppBundle\Exception\TypeMissingException;
use Enhavo\Bundle\AppBundle\Menu\AbstractMenu;
use Enhavo\Bundle\AppBundle\Menu\Menu;
use Enhavo\Bundle\AppBundle\Type\TypeFactory;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class ListMenu extends AbstractMenu
{
    public function render(array $options)
    {
        $template = $options['template'];
        $translationDomain = $options['translationDomain'];
        $icon = $options['icon'];
        $label = $options['label'];
        $menuConfig = $options['menu'];

        $menus = $this->getMenus($menuConfig);

        $active = false;
        foreach($menus as $menu) {
            if($menu->isActive()) {
                $active = true;
                break;
            }
        }

        return $this->renderTemplate($template, [
            'label' => $label,
            'translationDomain' => $translationDomain,
            'icon' => $icon,
            'menus' => $menus,
            'active' => $active
        ]);
    }

    /**
     * @return TypeFactory
     */
    private function getFactory()
    {
        return $this->container->get('enhavo_app.menu.factory');
    }

    /**
     * @return AuthorizationCheckerInterface
     */
    private function getSecurityChecker()
    {
        return $this->container->get('security.authorization_checker');
    }

    /**
     * @param $menuConfig
     * @return Menu[]
     * @throws TypeMissingException
     */
    private function getMenus($menuConfig)
    {
        $menus = [];
        foreach($menuConfig as $name => $options) {
            /** @var Menu $menu */
            $menu = $this->getFactory()->create($options);

            if($menu->isHidden()) {
                continue;
            }

            if($menu->getPermission() !== null && !$this->getSecurityChecker()->isGranted($menu->getPermission())) {
                continue;
            }

            $menus[] = $menu;
        }
        return $menus;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'template' => 'EnhavoAppBundle:Menu:list.html.twig',
            'translationDomain' => null,
            'icon' => ''
        ]);

        $resolver->setRequired([
            'label',
            'menu'
        ]);
    }

    public function getType()
    {
        return 'list';
    }
}<commit_msg>hide list menu item if none childen is displayed<commit_after><?php
/**
 * ListMenuBuilder.php
 *
 * @since 20/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\AppBundle\Menu\Menu;

use Enhavo\Bundle\AppBundle\Exception\TypeMissingException;
use Enhavo\Bundle\AppBundle\Menu\AbstractMenu;
use Enhavo\Bundle\AppBundle\Menu\Menu;
use Enhavo\Bundle\AppBundle\Type\TypeFactory;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class ListMenu extends AbstractMenu
{
    public function render(array $options)
    {
        $template = $options['template'];
        $translationDomain = $options['translationDomain'];
        $icon = $options['icon'];
        $label = $options['label'];
        $menuConfig = $options['menu'];

        $menus = $this->getMenus($menuConfig);

        $active = false;
        foreach($menus as $menu) {
            if($menu->isActive()) {
                $active = true;
                break;
            }
        }

        return $this->renderTemplate($template, [
            'label' => $label,
            'translationDomain' => $translationDomain,
            'icon' => $icon,
            'menus' => $menus,
            'active' => $active
        ]);
    }
    
    public function isHidden(array $options)
    {
        $isHidden = parent::isHidden($options);
        if(!$isHidden) {
            $menus = $this->getMenus($options['menu']);
            return count($menus) == 0;
        }
        return $isHidden;
    }

    /**
     * @return TypeFactory
     */
    private function getFactory()
    {
        return $this->container->get('enhavo_app.menu.factory');
    }

    /**
     * @return AuthorizationCheckerInterface
     */
    private function getSecurityChecker()
    {
        return $this->container->get('security.authorization_checker');
    }

    /**
     * @param $menuConfig
     * @return Menu[]
     * @throws TypeMissingException
     */
    private function getMenus($menuConfig)
    {
        $menus = [];
        foreach($menuConfig as $name => $options) {
            /** @var Menu $menu */
            $menu = $this->getFactory()->create($options);

            if($menu->isHidden()) {
                continue;
            }

            if($menu->getPermission() !== null && !$this->getSecurityChecker()->isGranted($menu->getPermission())) {
                continue;
            }

            $menus[] = $menu;
        }
        return $menus;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'template' => 'EnhavoAppBundle:Menu:list.html.twig',
            'translationDomain' => null,
            'icon' => ''
        ]);

        $resolver->setRequired([
            'label',
            'menu'
        ]);
    }

    public function getType()
    {
        return 'list';
    }
}
{'fraction_non_alphanumeric': 0.14138863841298466, 'fraction_numerical': 0.002344454463480613, 'mean_word_length': 2.020697167755991, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '198622', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1551, 'n_words': 266}