<commit_before><?php

namespace BlueBear\AdminBundle\Admin\Factory;

use BlueBear\AdminBundle\Admin\Configuration\ApplicationConfiguration;
use BlueBear\AdminBundle\Admin\Field;
use BlueBear\AdminBundle\Admin\Render\RendererInterface;
use BlueBear\AdminBundle\Admin\Render\TwigRendererInterface;
use Exception;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig_Environment;

class FieldRendererFactory
{
    /**
     * @var array
     */
    protected $renderMapping;

    protected $twig;

    /**
     * @var ApplicationConfiguration
     */
    protected $application;

    public function __construct(array $renderMapping = [], Twig_Environment $twig, ApplicationConfiguration $application)
    {
        $resolver = new OptionsResolver();
        // TODO allow thoses values in configuration
        $resolver->setDefaults([
            Field::TYPE_STRING => 'BlueBear\AdminBundle\Admin\Render\StringRenderer',
            Field::TYPE_ARRAY => 'BlueBear\AdminBundle\Admin\Render\ArrayRenderer',
            Field::TYPE_LINK => 'BlueBear\AdminBundle\Admin\Render\LinkRenderer',
            Field::TYPE_DATE => 'BlueBear\AdminBundle\Admin\Render\DateRenderer',
        ]);
        $this->renderMapping = $resolver->resolve($renderMapping);
        $this->twig = $twig;
        $this->application = $application;
    }

    /**
     * @param $fieldType
     * @param array $fieldOptions
     * @return RendererInterface
     * @throws Exception
     */
    public function create($fieldType, $fieldOptions = [])
    {
        if (array_key_exists($fieldType, $this->renderMapping)) {
            $class = $this->renderMapping[$fieldType];
            $renderer = new $class($fieldOptions, $this->application);

            if (!in_array('BlueBear\AdminBundle\Admin\Render\RendererInterface', class_implements($renderer))) {
                throw new Exception("The render $class should implements RenderInterface");
            }
            if (in_array('BlueBear\AdminBundle\Admin\Render\RendererInterface', class_implements($renderer))) {
                /** @var TwigRendererInterface $renderer */
                $renderer->setTwig($this->twig);
            }
        } else {
            throw new InvalidConfigurationException('Invalid field type "' . $fieldType . '"for renderer');
        }<commit_msg>fixing bug in field twig rendering<commit_after><?php

namespace BlueBear\AdminBundle\Admin\Factory;

use BlueBear\AdminBundle\Admin\Configuration\ApplicationConfiguration;
use BlueBear\AdminBundle\Admin\Field;
use BlueBear\AdminBundle\Admin\Render\RendererInterface;
use BlueBear\AdminBundle\Admin\Render\TwigRendererInterface;
use Exception;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig_Environment;

class FieldRendererFactory
{
    /**
     * @var array
     */
    protected $renderMapping;

    protected $twig;

    /**
     * @var ApplicationConfiguration
     */
    protected $application;

    public function __construct(array $renderMapping = [], Twig_Environment $twig, ApplicationConfiguration $application)
    {
        $resolver = new OptionsResolver();
        // TODO allow thoses values in configuration
        $resolver->setDefaults([
            Field::TYPE_STRING => 'BlueBear\AdminBundle\Admin\Render\StringRenderer',
            Field::TYPE_ARRAY => 'BlueBear\AdminBundle\Admin\Render\ArrayRenderer',
            Field::TYPE_LINK => 'BlueBear\AdminBundle\Admin\Render\LinkRenderer',
            Field::TYPE_DATE => 'BlueBear\AdminBundle\Admin\Render\DateRenderer',
        ]);
        $this->renderMapping = $resolver->resolve($renderMapping);
        $this->twig = $twig;
        $this->application = $application;
    }

    /**
     * @param $fieldType
     * @param array $fieldOptions
     * @return RendererInterface
     * @throws Exception
     */
    public function create($fieldType, $fieldOptions = [])
    {
        if (array_key_exists($fieldType, $this->renderMapping)) {
            $class = $this->renderMapping[$fieldType];
            $renderer = new $class($fieldOptions, $this->application);

            if (!in_array('BlueBear\AdminBundle\Admin\Render\RendererInterface', class_implements($renderer))) {
                throw new Exception("The render $class should implements RenderInterface");
            }
            if (in_array('BlueBear\AdminBundle\Admin\Render\TwigRendererInterface', class_implements($renderer))) {
                /** @var TwigRendererInterface $renderer */
                $renderer->setTwig($this->twig);
            }
        } else {
            throw new InvalidConfigurationException('Invalid field type "' . $fieldType . '"for renderer');
        }
{'fraction_non_alphanumeric': 0.1152654410245817, 'fraction_numerical': 0.0, 'mean_word_length': 3.11734693877551, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '915651', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1260, 'n_words': 229}