<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2016, 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\AdminBundle\View\WidgetGenerator;

use Darvin\AdminBundle\Security\Permissions\Permission;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Entities list view widget generator
 */
class EntitiesListGenerator extends AbstractWidgetGenerator
{
    /**
     * @var \Darvin\AdminBundle\View\WidgetGenerator\WidgetGeneratorPool
     */
    private $widgetGeneratorPool;

    /**
     * @param \Darvin\AdminBundle\View\WidgetGenerator\WidgetGeneratorPool $widgetGeneratorPool View widget generator pool
     */
    public function setWidgetGeneratorPool(WidgetGeneratorPool $widgetGeneratorPool)
    {
        $this->widgetGeneratorPool = $widgetGeneratorPool;
    }

    /**
     * {@inheritdoc}
     */
    protected function generateWidget($entity, array $options, $property)
    {
        $collection = $this->getPropertyValue($entity, isset($options['property']) ? $options['property'] : $property);

        if (empty($collection)) {
            return '';
        }
        if (!is_array($collection)) {
            if (!is_object($collection)) {
                throw new WidgetGeneratorException(
                    sprintf('Entities collection must be array or object, "%s" provided.', gettype($collection))
                );
            }
            if (!$collection instanceof \Traversable) {
                throw new WidgetGeneratorException(
                    sprintf('Entities collection object "%s" must be instance of \Traversable.', ClassUtils::getClass($collection))
                );
            }
        }
        if (empty($options['item_widget_alias'])) {
            return $this->render($options, array(
                'widgets' => $collection,
            ));
        }

        $widgetGenerator = $this->widgetGeneratorPool->getWidgetGenerator($options['item_widget_alias']);

        $widgets = array();

        foreach ($collection as $item) {
            $widgets[] = $widgetGenerator->generate($item, $options['item_widget_options']);
        }

        return $this->render($options, array(
            'widgets' => $widgets,
        ));
    }

    /**
     * {@inheritdoc}
     */
    protected function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        $resolver
            ->setDefaults(array(
                'item_widget_alias'   => ShowLinkGenerator::ALIAS,
                'item_widget_options' => array(
                    'text_link' => true,
                ),
            ))
            ->setDefined('property')
            ->setAllowedTypes('item_widget_alias', array(
                'null',
                'string',<commit_msg>Add "item_title_property" option to entities list view widget generator.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2016, 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\AdminBundle\View\WidgetGenerator;

use Darvin\AdminBundle\Security\Permissions\Permission;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Entities list view widget generator
 */
class EntitiesListGenerator extends AbstractWidgetGenerator
{
    /**
     * @var \Darvin\AdminBundle\View\WidgetGenerator\WidgetGeneratorPool
     */
    private $widgetGeneratorPool;

    /**
     * @param \Darvin\AdminBundle\View\WidgetGenerator\WidgetGeneratorPool $widgetGeneratorPool View widget generator pool
     */
    public function setWidgetGeneratorPool(WidgetGeneratorPool $widgetGeneratorPool)
    {
        $this->widgetGeneratorPool = $widgetGeneratorPool;
    }

    /**
     * {@inheritdoc}
     */
    protected function generateWidget($entity, array $options, $property)
    {
        $collection = $this->getPropertyValue($entity, isset($options['property']) ? $options['property'] : $property);

        if (empty($collection)) {
            return '';
        }
        if (!is_array($collection)) {
            if (!is_object($collection)) {
                throw new WidgetGeneratorException(
                    sprintf('Entities collection must be array or object, "%s" provided.', gettype($collection))
                );
            }
            if (!$collection instanceof \Traversable) {
                throw new WidgetGeneratorException(
                    sprintf('Entities collection object "%s" must be instance of \Traversable.', ClassUtils::getClass($collection))
                );
            }
        }
        if (empty($options['item_widget_alias'])) {
            if (!isset($options['item_title_property'])) {
                return $this->render($options, array(
                    'widgets' => $collection,
                ));
            }

            $widgets = array();

            foreach ($collection as $item) {
                $widgets[] = $this->getPropertyValue($item, $options['item_title_property']);
            }

            return $this->render($options, array(
                'widgets' => $widgets,
            ));
        }

        $widgetGenerator = $this->widgetGeneratorPool->getWidgetGenerator($options['item_widget_alias']);

        $widgets = array();

        foreach ($collection as $item) {
            $widgets[] = $widgetGenerator->generate($item, $options['item_widget_options']);
        }

        return $this->render($options, array(
            'widgets' => $widgets,
        ));
    }

    /**
     * {@inheritdoc}
     */
    protected function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        $resolver
            ->setDefaults(array(
                'item_widget_alias'   => ShowLinkGenerator::ALIAS,
                'item_widget_options' => array(
                    'text_link' => true,
                ),
            ))
            ->setDefined(array(
                'item_title_property',
                'property',
            ))
            ->setAllowedTypes('item_title_property', 'string')
            ->setAllowedTypes('item_widget_alias', array(
                'null',
                'string',
{'fraction_non_alphanumeric': 0.12070282658517953, 'fraction_numerical': 0.0012223071046600458, 'mean_word_length': 2.235788433020267, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '3129414', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1612, 'n_words': 339}