<commit_before><?php
/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace Sonata\ProductBundle\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sonata\Component\Product\ProductCategoryManagerInterface;
use Symfony\Component\Routing\RouterInterface;


/**
 * Class ProductMenuBuilder
 *
 * @package Sonata\ProductBundle\Menu
 *
 * @author <NAME> <<EMAIL>>
 */
class ProductMenuBuilder
{
    /**
     * @var FactoryInterface
     */
    protected $factory;

    /**
     * @var ProductCategoryManagerInterface
     */
    protected $categoryManager;

    /**
     * @var RouterInterface
     */
    protected $router;

    /**
     * Constructor
     *
     * @param MenuFactory                     $factory
     * @param ProductCategoryManagerInterface $categoryManager
     * @param RouterInterface                 $router
     */
    public function __construct(FactoryInterface $factory, ProductCategoryManagerInterface $categoryManager, RouterInterface $router)
    {
        $this->factory         = $factory;
        $this->categoryManager = $categoryManager;
        $this->router          = $router;
    }

    /**
     * @param array  $itemOptions The options given to the created menuItem
     * @param string $currentUri  The current URI
     *
     * @return \Knp\Menu\ItemInterface
     */
    public function createCategoryMenu(array $itemOptions = array(), $currentUri = null)
    {
        $menu = $this->factory->createItem('categories', $itemOptions);

        $this->buildCategoryMenu($menu, $itemOptions, $currentUri);

        return $menu;
    }

    /**
     * @param \Knp\Menu\ItemInterface $menu        The item to fill with $routes
     * @param array                   $options     The item options
     * @param string                  $currentUri  The current URI
     */
    public function buildCategoryMenu(ItemInterface $menu, array $options = array(), $currentUri = null)
    {
        $categories = $this->categoryManager->getCategoryTree();

        $this->fillMenu($menu, $categories, $options, $currentUri);
    }

    /**
     * Recursive method to fill $menu with $categories
     *
     * @param ItemInterface $menu
     * @param array         $categories
     * @param array         $options
     * @param strubg        $currentUri
     */
    protected function fillMenu(ItemInterface $menu, array $categories, array $options = array(), $currentUri = null)
    {
        foreach ($categories as $category) {
            if (null === $category->getParent()) {
                $fullOptions = array_merge(array('attributes' => array('class' => 'nav-header')), $options);
            } else {
                $fullOptions = array_merge(array(
                    'route'           => 'sonata_catalog_category',
                    'routeParameters' => array(
                        'category_id'   => $category->getId(),
                        'category_slug' => $category->getSlug()
                    )
                ), $options);
            }

            $child = $menu->addChild(
                $category->getName(),
                $fullOptions
            );
<commit_msg>Fixed error occuring when loading ArrayCollection productCategories<commit_after><?php
/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace Sonata\ProductBundle\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sonata\Component\Product\ProductCategoryManagerInterface;
use Symfony\Component\Routing\RouterInterface;


/**
 * Class ProductMenuBuilder
 *
 * @package Sonata\ProductBundle\Menu
 *
 * @author <NAME> <<EMAIL>>
 */
class ProductMenuBuilder
{
    /**
     * @var FactoryInterface
     */
    protected $factory;

    /**
     * @var ProductCategoryManagerInterface
     */
    protected $categoryManager;

    /**
     * @var RouterInterface
     */
    protected $router;

    /**
     * Constructor
     *
     * @param MenuFactory                     $factory
     * @param ProductCategoryManagerInterface $categoryManager
     * @param RouterInterface                 $router
     */
    public function __construct(FactoryInterface $factory, ProductCategoryManagerInterface $categoryManager, RouterInterface $router)
    {
        $this->factory         = $factory;
        $this->categoryManager = $categoryManager;
        $this->router          = $router;
    }

    /**
     * @param array  $itemOptions The options given to the created menuItem
     * @param string $currentUri  The current URI
     *
     * @return \Knp\Menu\ItemInterface
     */
    public function createCategoryMenu(array $itemOptions = array(), $currentUri = null)
    {
        $menu = $this->factory->createItem('categories', $itemOptions);

        $this->buildCategoryMenu($menu, $itemOptions, $currentUri);

        return $menu;
    }

    /**
     * @param \Knp\Menu\ItemInterface $menu        The item to fill with $routes
     * @param array                   $options     The item options
     * @param string                  $currentUri  The current URI
     */
    public function buildCategoryMenu(ItemInterface $menu, array $options = array(), $currentUri = null)
    {
        $categories = $this->categoryManager->getCategoryTree();

        $this->fillMenu($menu, $categories, $options, $currentUri);
    }

    /**
     * Recursive method to fill $menu with $categories
     *
     * @param ItemInterface $menu
     * @param array         $categories
     * @param array         $options
     * @param strubg        $currentUri
     */
    protected function fillMenu(ItemInterface $menu, $categories, array $options = array(), $currentUri = null)
    {
        foreach ($categories as $category) {
            if (null === $category->getParent()) {
                $fullOptions = array_merge(array('attributes' => array('class' => 'nav-header')), $options);
            } else {
                $fullOptions = array_merge(array(
                    'route'           => 'sonata_catalog_category',
                    'routeParameters' => array(
                        'category_id'   => $category->getId(),
                        'category_slug' => $category->getSlug()
                    )
                ), $options);
            }

            $child = $menu->addChild(
                $category->getName(),
                $fullOptions
            );

{'fraction_non_alphanumeric': 0.11244377811094453, 'fraction_numerical': 0.0, 'mean_word_length': 2.126054358013121, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 55, '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': '4436235', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1702, 'n_words': 431}