<commit_before><?php

namespace Sidus\EAVModelBundle\Form\Type;

use Sidus\EAVModelBundle\Configuration\FamilyConfigurationHandler;
use Sidus\EAVModelBundle\Exception\MissingFamilyException;
use Sidus\EAVModelBundle\Model\FamilyInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Simple family selector
 *
 * @author <NAME> <<EMAIL>>
 */
class FamilySelectorType extends AbstractType
{
    /** @var FamilyConfigurationHandler */
    protected $familyConfigurationHandler;

    /**
     * @param FamilyConfigurationHandler $familyConfigurationHandler
     */
    public function __construct(FamilyConfigurationHandler $familyConfigurationHandler)
    {
        $this->familyConfigurationHandler = $familyConfigurationHandler;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     * @throws \InvalidArgumentException
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer(new CallbackTransformer(
            function ($originalData) {
                if ($originalData instanceof FamilyInterface) {
                    $originalData = $originalData->getCode();
                }

                return $originalData;
            },
            function ($submittedData) {
                if ($submittedData instanceof FamilyInterface) {
                    // Should actually never happen ?
                    return $submittedData;
                }
                try {
                    return $this->familyConfigurationHandler->getFamily($submittedData);
                } catch (MissingFamilyException $e) {
                    throw new \InvalidArgumentException($e->getMessage(), 0, $e);
                }
            }
        ));
    }

    /**
     * @param OptionsResolver $resolver
     * @throws \Exception
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choices_as_values' => true,
            'choices' => null,
            'families' => null,
        ]);

        $resolver->setNormalizer('families', function (Options $options, $values) {
            if (null === $values) {
                $values = $this->familyConfigurationHandler->getFamilies();<commit_msg>FIX if no family selected<commit_after><?php

namespace Sidus\EAVModelBundle\Form\Type;

use Sidus\EAVModelBundle\Configuration\FamilyConfigurationHandler;
use Sidus\EAVModelBundle\Exception\MissingFamilyException;
use Sidus\EAVModelBundle\Model\FamilyInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Simple family selector
 *
 * @author <NAME> <<EMAIL>>
 */
class FamilySelectorType extends AbstractType
{
    /** @var FamilyConfigurationHandler */
    protected $familyConfigurationHandler;

    /**
     * @param FamilyConfigurationHandler $familyConfigurationHandler
     */
    public function __construct(FamilyConfigurationHandler $familyConfigurationHandler)
    {
        $this->familyConfigurationHandler = $familyConfigurationHandler;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     * @throws \InvalidArgumentException
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer(new CallbackTransformer(
            function ($originalData) {
                if ($originalData instanceof FamilyInterface) {
                    $originalData = $originalData->getCode();
                }

                return $originalData;
            },
            function ($submittedData) {
                if ($submittedData === null) {
                    return $submittedData;
                } elseif ($submittedData instanceof FamilyInterface) {
                    // Should actually never happen ?
                    return $submittedData;
                }
                try {
                    return $this->familyConfigurationHandler->getFamily($submittedData);
                } catch (MissingFamilyException $e) {
                    throw new \InvalidArgumentException($e->getMessage(), 0, $e);
                }
            }
        ));
    }

    /**
     * @param OptionsResolver $resolver
     * @throws \Exception
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choices_as_values' => true,
            'choices' => null,
            'families' => null,
        ]);

        $resolver->setNormalizer('families', function (Options $options, $values) {
            if (null === $values) {
                $values = $this->familyConfigurationHandler->getFamilies();
{'fraction_non_alphanumeric': 0.09811616954474098, 'fraction_numerical': 0.0003924646781789639, 'mean_word_length': 2.377733598409543, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '4017826', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1195, 'n_words': 242}