<commit_before><?php

namespace Ekyna\Bundle\CoreBundle\Form\Type;

use Doctrine\Common\Persistence\ObjectManager;
use Ekyna\Bundle\CoreBundle\Form\DataTransformer\ObjectToIdentifierTransformer;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Class HiddenEntityType
 * @package Ekyna\Bundle\CoreBundle\Form\Type
 * @author <NAME> <<EMAIL>>
 */
class HiddenEntityType extends AbstractType
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @var DoctrineOrmTypeGuesser
     */
    private $guesser;

    /**
     * @param ObjectManager $om
     * @param DoctrineOrmTypeGuesser $guesser
     */
    public function __construct(ObjectManager $om, DoctrineOrmTypeGuesser $guesser)
    {
        $this->om = $om;
        $this->guesser = $guesser;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options = array())
    {
        $transformer = new ObjectToIdentifierTransformer();
        $builder->addViewTransformer($transformer);

        if($options['class'] === null) {
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($transformer) {
                $form = $event->getForm();
                $class = $form->getParent()->getConfig()->getDataClass();
                $property = $form->getName();
                if (null === $guessedType = $this->guesser->guessType($class, $property)) {
                    throw new \RuntimeException(sprintf('Unable to guess the type for "%s" property.', $property));
                }
                $options = $guessedType->getOptions();
                $repository = $this->om->getRepository($options['class']);
                $transformer->setRepository($repository);
            });
        } else {
            $repository = $this->om->getRepository($options['class']);<commit_msg>Fix hidden entity form type<commit_after><?php

namespace Ekyna\Bundle\CoreBundle\Form\Type;

use Doctrine\Common\Persistence\ObjectManager;
use Ekyna\Bundle\CoreBundle\Form\DataTransformer\ObjectToIdentifierTransformer;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Class HiddenEntityType
 * @package Ekyna\Bundle\CoreBundle\Form\Type
 * @author <NAME> <<EMAIL>>
 */
class HiddenEntityType extends AbstractType
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @var DoctrineOrmTypeGuesser
     */
    private $guesser;

    /**
     * @param ObjectManager $om
     * @param DoctrineOrmTypeGuesser $guesser
     */
    public function __construct(ObjectManager $om, DoctrineOrmTypeGuesser $guesser)
    {
        $this->om = $om;
        $this->guesser = $guesser;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options = array())
    {
        $transformer = new ObjectToIdentifierTransformer();
        $builder->addViewTransformer($transformer);

        if(0 === strlen($options['class'])) {
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($transformer) {
                $form = $event->getForm();
                $class = $form->getParent()->getConfig()->getDataClass();
                $property = $form->getName();
                if (null === $guessedType = $this->guesser->guessType($class, $property)) {
                    throw new \RuntimeException(sprintf('Unable to guess the type for "%s" property.', $property));
                }
                $typeOptions = $guessedType->getOptions();
                $repository = $this->om->getRepository($typeOptions['class']);
                $transformer->setRepository($repository);
            });
        } else {
            $repository = $this->om->getRepository($options['class']);
{'fraction_non_alphanumeric': 0.1340860973888497, 'fraction_numerical': 0.0002352387673488591, 'mean_word_length': 2.91889400921659, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '6486524', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1183, 'n_words': 209}