<commit_before><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2017-2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\MenuBundle\Item\Factory;

use Darvin\ImageBundle\Entity\Image\AbstractImage;
use Darvin\MenuBundle\Entity\Menu\Item;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Item factory abstract implementation
 */
abstract class AbstractItemFactory implements ItemFactoryInterface
{
    /**
     * @var \Knp\Menu\FactoryInterface
     */
    protected $genericItemFactory;

    /**
     * @var \Symfony\Component\OptionsResolver\OptionsResolver|null
     */
    protected $extrasResolver = null;

    /**
     * @param \Knp\Menu\FactoryInterface $genericItemFactory Generic item factory
     */
    public function setGenericItemFactory(FactoryInterface $genericItemFactory): void
    {
        $this->genericItemFactory = $genericItemFactory;
    }

    /**
     * {@inheritDoc}
     */
    public function createItem($source): ItemInterface
    {
        if (!$this->supports($source)) {
            throw new \InvalidArgumentException('Source is not supported.');
        }

        return $this->genericItemFactory->createItem($this->nameItem($source), $this->getOptions($source));
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function nameItem($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return array
     */
    protected function getOptions($source): array
    {
        if (null === $this->extrasResolver) {
            $extrasResolver = new OptionsResolver();

            $this->configureExtras($extrasResolver);

            $this->extrasResolver = $extrasResolver;
        }

        return [
            'label'  => $this->getLabel($source),
            'uri'    => $this->getUri($source),
            'extras' => $this->extrasResolver->resolve($this->getExtras($source)),
        ];
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function getLabel($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function getUri($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return array
     */
    protected function getExtras($source): array
    {
        return [];
    }

    /**
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver Extras resolver
     */
    protected function configureExtras(OptionsResolver $resolver): void
    {
        $resolver
            ->setDefaults([
                'itemEntity'       => null,
                'object'           => null,
                'objectId'         => null,
                'objectName'       => null,
                'showSlugChildren' => false,
            ])
            ->setAllowedTypes('itemEntity', [Item::class, 'null'])
            ->setAllowedTypes('object', ['object', 'null'])
            ->setAllowedTypes('objectName', ['string', 'null'])
            ->setAllowedTypes('showSlugChildren', 'boolean');

        foreach ([
            'image',
            'hoverImage',<commit_msg>Remove redundant "showSlugChildren" extra.<commit_after><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2017-2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\MenuBundle\Item\Factory;

use Darvin\ImageBundle\Entity\Image\AbstractImage;
use Darvin\MenuBundle\Entity\Menu\Item;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Item factory abstract implementation
 */
abstract class AbstractItemFactory implements ItemFactoryInterface
{
    /**
     * @var \Knp\Menu\FactoryInterface
     */
    protected $genericItemFactory;

    /**
     * @var \Symfony\Component\OptionsResolver\OptionsResolver|null
     */
    protected $extrasResolver = null;

    /**
     * @param \Knp\Menu\FactoryInterface $genericItemFactory Generic item factory
     */
    public function setGenericItemFactory(FactoryInterface $genericItemFactory): void
    {
        $this->genericItemFactory = $genericItemFactory;
    }

    /**
     * {@inheritDoc}
     */
    public function createItem($source): ItemInterface
    {
        if (!$this->supports($source)) {
            throw new \InvalidArgumentException('Source is not supported.');
        }

        return $this->genericItemFactory->createItem($this->nameItem($source), $this->getOptions($source));
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function nameItem($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return array
     */
    protected function getOptions($source): array
    {
        if (null === $this->extrasResolver) {
            $extrasResolver = new OptionsResolver();

            $this->configureExtras($extrasResolver);

            $this->extrasResolver = $extrasResolver;
        }

        return [
            'label'  => $this->getLabel($source),
            'uri'    => $this->getUri($source),
            'extras' => $this->extrasResolver->resolve($this->getExtras($source)),
        ];
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function getLabel($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return string|null
     */
    protected function getUri($source): ?string
    {
        return null;
    }

    /**
     * @param mixed $source Source
     *
     * @return array
     */
    protected function getExtras($source): array
    {
        return [];
    }

    /**
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver Extras resolver
     */
    protected function configureExtras(OptionsResolver $resolver): void
    {
        $resolver
            ->setDefaults([
                'itemEntity' => null,
                'object'     => null,
                'objectId'   => null,
                'objectName' => null,
            ])
            ->setAllowedTypes('itemEntity', [Item::class, 'null'])
            ->setAllowedTypes('object', ['object', 'null'])
            ->setAllowedTypes('objectName', ['string', 'null']);

        foreach ([
            'image',
            'hoverImage',
{'fraction_non_alphanumeric': 0.12898112119902003, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 2.2264063226406323, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 61, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2474358', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1863, 'n_words': 398}