<commit_before><?php

namespace Openl10n\Bundle\CoreBundle\Form\Type;

use Openl10n\Bundle\CoreBundle\Repository\DomainRepositoryInterface;
use Openl10n\Bundle\CoreBundle\Translation\TranslationDumperInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ExportDomainType extends AbstractType
{
    protected $domainRepository;
    protected $translationDumper;

    /**
     * @param DomainRepositoryInterface  $domainRepository
     * @param TranslationDumperInterface $translationDumper
     */
    public function __construct(DomainRepositoryInterface $domainRepository, TranslationDumperInterface $translationDumper)
    {
        $this->domainRepository = $domainRepository;
        $this->translationDumper = $translationDumper;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $domainRepository = $this->domainRepository;

        $builder
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($domainRepository) {
                $form = $event->getForm();
                $data = $event->getData();

                if (null === $data) {
                    return;
                }

                $project = $data->getProject();
                $domains = $domainRepository->findByProject($project);

                $keys = array_map(function ($domain) { return $domain->getSlug(); }, $domains);
                $values = array_map(function ($domain) { return $domain->getName(); }, $domains);
                $domainList = array_combine($keys, $values);

                $form->add('domain', 'choice', array(
                    'choices' => $domainList,
                ));
            })
            ->add('locale', 'choice', array(
                'choices' => array(
                    'en_GB' => 'English',
                    'fr_FR' => 'French',
                )
            ))
            ->add('format', 'choice', array(
                'choices' => $this->getFormats()
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'openl10n_export_domain';
    }

    protected function getFormats()
    {
        $formats = $this->translationDumper->getFormats();
        $values = array_map(function($format) {
            return '.'.$format;
        }, $formats);

        return array_combine($formats, $values);
    }
}<commit_msg>Dynamic locale choice when exporting domains.<commit_after><?php

namespace Openl10n\Bundle\CoreBundle\Form\Type;

use Openl10n\Bundle\CoreBundle\Repository\DomainRepositoryInterface;
use Openl10n\Bundle\CoreBundle\Translation\TranslationDumperInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ExportDomainType extends AbstractType
{
    protected $domainRepository;
    protected $translationDumper;

    /**
     * @param DomainRepositoryInterface  $domainRepository
     * @param TranslationDumperInterface $translationDumper
     */
    public function __construct(DomainRepositoryInterface $domainRepository, TranslationDumperInterface $translationDumper)
    {
        $this->domainRepository = $domainRepository;
        $this->translationDumper = $translationDumper;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $domainRepository = $this->domainRepository;

        $builder
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($domainRepository) {
                $form = $event->getForm();
                $data = $event->getData();

                if (null === $data) {
                    return;
                }

                $project = $data->getProject();
                $domains = $domainRepository->findByProject($project);

                $projectLocales = array_map(function($language) {
                    return $language->getLocale()->toString();
                }, $project->getLanguages()->toArray());

                $keys = array_map(function ($domain) { return $domain->getSlug(); }, $domains);
                $values = array_map(function ($domain) { return $domain->getName(); }, $domains);
                $domainList = array_combine($keys, $values);

                $form
                    ->add('domain', 'choice', array(
                        'choices' => $domainList,
                    ))
                    ->add('locale', 'openl10n_locale_choice', array(
                        'restrict' => $projectLocales,
                    ))
                ;
            })
            ->add('format', 'choice', array(
                'choices' => $this->getFormats()
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'openl10n_export_domain';
    }

    protected function getFormats()
    {
        $formats = $this->translationDumper->getFormats();
        $values = array_map(function($format) {
            return '.'.$format;
        }, $formats);

        return array_combine($formats, $values);
    }
}
{'fraction_non_alphanumeric': 0.12404402163775415, 'fraction_numerical': 0.003357582540570789, 'mean_word_length': 2.1784232365145226, 'pattern_counts': {'":': 0, '<': 5, '<?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': '979761', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1360, 'n_words': 245}