<commit_before><?php

/**
 * @author:  <NAME> <<EMAIL>>
 * @license: MIT
 */

namespace IDCI\Bundle\ExtraFormBundle\Configuration\Builder;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use IDCI\Bundle\ExtraFormBundle\Configuration\Fetcher\ConfigurationFetcherRegistry;
use IDCI\Bundle\ExtraFormBundle\Configuration\Fetcher\ConfigurationFetcherInterface;
use IDCI\Bundle\ExtraFormBundle\Type\ExtraFormTypeRegistryInterface;
use IDCI\Bundle\ExtraFormBundle\Constraint\ExtraFormConstraintRegistryInterface;

class ExtraFormBuilder implements ExtraFormBuilderInterface
{
    protected $formFactory;
    protected $configurationFetcherRegistry;
    protected $typeRegistry;
    protected $constraintRegistry;

    /**
     * Constructor
     *
     * @param FormFactoryInterface                 $formFactory                  The form factory.
     * @param ConfigurationFetcherRegistry         $configurationFetcherRegistry The configuration fetcher registry.
     * @param ExtraFormTypeRegistryInterface       $typeRegistry                 The type registry.
     * @param ExtraFormConstraintRegistryInterface $constraintRegistry           The constraint registry.
     */
    public function __construct(
        FormFactoryInterface                 $formFactory,
        ConfigurationFetcherRegistry         $configurationFetcherRegistry,
        ExtraFormTypeRegistryInterface       $typeRegistry,
        ExtraFormConstraintRegistryInterface $constraintRegistry
    ) {
        $this->formFactory                  = $formFactory;
        $this->configurationFetcherRegistry = $configurationFetcherRegistry;
        $this->typeRegistry                 = $typeRegistry;
        $this->constraintRegistry           = $constraintRegistry;
    }

    /**
     * Define the field configuration using the option resolver component.
     *
     * @param OptionsResolverInterface $resolver
     */
    protected function configureField(OptionsResolverInterface $resolver)
    {
        $resolver
            ->setDefaults(array(
                'extra_form_type' => 'text',
                'options'         => array(),
                'constraints'     => array()
            ))
        ;
    }

    /**
     * {@inheritDoc}
     */
    public function build(
        $configuration,
        array $parameters = array(),
        $data = null,
        FormBuilderInterface $formBuilder = null
    ) {
        if (null === $formBuilder) {
            $formBuilder = $this->formFactory->createBuilder();
        }

        if (is_string($configuration)) {
            $configuration = $this
                ->configurationFetcherRegistry
                ->getFetcher($configuration)
            ;
        }

        if ($configuration instanceof ConfigurationFetcherInterface) {
            $configuration = $configuration->fetch($parameters);
        }

        foreach ($configuration as $name => $field) {
            $resolver = new OptionsResolver();
            $this->configureField($resolver);
            $resolvedField = $resolver->resolve($field);

            $formBuilder->add(
                $name,
                $this->buildFormType($resolvedField),
                $this->buildFormOptions($name, $resolvedField, $data)
            );
        }

        return $formBuilder;
    }

    /**
     * Build form type.
     *
     * @param array $field<commit_msg>Test the configuration type before foreach<commit_after><?php

/**
 * @author:  <NAME> <<EMAIL>>
 * @license: MIT
 */

namespace IDCI\Bundle\ExtraFormBundle\Configuration\Builder;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use IDCI\Bundle\ExtraFormBundle\Configuration\Fetcher\ConfigurationFetcherRegistry;
use IDCI\Bundle\ExtraFormBundle\Configuration\Fetcher\ConfigurationFetcherInterface;
use IDCI\Bundle\ExtraFormBundle\Type\ExtraFormTypeRegistryInterface;
use IDCI\Bundle\ExtraFormBundle\Constraint\ExtraFormConstraintRegistryInterface;

class ExtraFormBuilder implements ExtraFormBuilderInterface
{
    protected $formFactory;
    protected $configurationFetcherRegistry;
    protected $typeRegistry;
    protected $constraintRegistry;

    /**
     * Constructor
     *
     * @param FormFactoryInterface                 $formFactory                  The form factory.
     * @param ConfigurationFetcherRegistry         $configurationFetcherRegistry The configuration fetcher registry.
     * @param ExtraFormTypeRegistryInterface       $typeRegistry                 The type registry.
     * @param ExtraFormConstraintRegistryInterface $constraintRegistry           The constraint registry.
     */
    public function __construct(
        FormFactoryInterface                 $formFactory,
        ConfigurationFetcherRegistry         $configurationFetcherRegistry,
        ExtraFormTypeRegistryInterface       $typeRegistry,
        ExtraFormConstraintRegistryInterface $constraintRegistry
    ) {
        $this->formFactory                  = $formFactory;
        $this->configurationFetcherRegistry = $configurationFetcherRegistry;
        $this->typeRegistry                 = $typeRegistry;
        $this->constraintRegistry           = $constraintRegistry;
    }

    /**
     * Define the field configuration using the option resolver component.
     *
     * @param OptionsResolverInterface $resolver
     */
    protected function configureField(OptionsResolverInterface $resolver)
    {
        $resolver
            ->setDefaults(array(
                'extra_form_type' => 'text',
                'options'         => array(),
                'constraints'     => array()
            ))
        ;
    }

    /**
     * {@inheritDoc}
     */
    public function build(
        $configuration,
        array $parameters = array(),
        $data = null,
        FormBuilderInterface $formBuilder = null
    ) {
        if (null === $formBuilder) {
            $formBuilder = $this->formFactory->createBuilder();
        }

        if (is_string($configuration)) {
            $configuration = $this
                ->configurationFetcherRegistry
                ->getFetcher($configuration)
            ;
        }

        if ($configuration instanceof ConfigurationFetcherInterface) {
            $configuration = $configuration->fetch($parameters);
        }

        if (is_array($configuration)) {
            foreach ($configuration as $name => $field) {
                $resolver = new OptionsResolver();
                $this->configureField($resolver);
                $resolvedField = $resolver->resolve($field);

                $formBuilder->add(
                    $name,
                    $this->buildFormType($resolvedField),
                    $this->buildFormOptions($name, $resolvedField, $data)
                );
            }
        }

        return $formBuilder;
    }

    /**
     * Build form type.
     *
     * @param array $field
{'fraction_non_alphanumeric': 0.08908964558721334, 'fraction_numerical': 0.0, 'mean_word_length': 2.3161290322580643, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, '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': '515473', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1671, 'n_words': 326}