<commit_before><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace Sonata\CoreBundle\Form\Type;

use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\RegistryInterface;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Metadata\MetadataFactoryInterface;

/**
 * Class DoctrineORMSerializationType
 *
 * This is a doctrine serialization form type that generates a form type from class serialization metadata
 * and doctrine metadata
 *
 * @author <NAME> <<EMAIL>>
 */
class DoctrineORMSerializationType extends AbstractType
{
    /**
     * @var RegistryInterface
     */
    protected $registry;

    /**
     * @var MetadataFactoryInterface
     */
    protected $metadataFactory;

    /**
     * @var string
     */
    protected $class;

    /**
     * @var string
     */
    protected $group;

    /**
     * @var boolean
     */
    protected $allowOverride;

    /**
     * Constructor
     *
     * @param MetadataFactoryInterface $metadataFactory Serializer metadata factory
     * @param ManagerRegistry          $registry        Doctrine registry
     * @param string                   $name            Form type name
     * @param string                   $class           Data class name
     * @param string                   $group           Serialization group name
     */
    public function __construct(MetadataFactoryInterface $metadataFactory, ManagerRegistry $registry, $name, $class, $group, $allowOverride = false)
    {
        $this->metadataFactory = $metadataFactory;
        $this->registry = $registry;
        $this->name  = $name;
        $this->class = $class;
        $this->group = $group;
        $this->allowOverride = $allowOverride;
    }

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

        $manager = $this->registry->getManagerForClass($this->class);
        $doctrineMetadata = $manager->getClassMetadata($this->class);

        foreach ($serializerMetadata->propertyMetadata as $propertyMetadata) {
            $name = $propertyMetadata->name;

            if (in_array($name, $doctrineMetadata->getIdentifierFieldNames()) && !$this->allowOverride) {
                continue;
            }

            if (!in_array($this->group, $propertyMetadata->groups)) {
                continue;
            }

            $type = null;
            $nullable = true;

            if (isset($doctrineMetadata->fieldMappings[$name])) {
                $fieldMetadata = $doctrineMetadata->fieldMappings[$name];
                $type = isset($fieldMetadata['type']) ? $fieldMetadata['type'] : null;
                $nullable = isset($fieldMetadata['nullable']) ? $fieldMetadata['nullable'] : false;
            } else if (isset($doctrineMetadata->associationMappings[$name])) {
                $associationMetadata = $doctrineMetadata->associationMappings[$name];

                if (isset($associationMetadata['joinColumns']['nullable'])) {
                    $nullable = $associationMetadata['joinColumns']['nullable'];
                } else if (isset($associationMetadata['inverseJoinColumns']['nullable'])) {
                    $nullable = $associationMetadata['inverseJoinColumns']['nullable'];
                }
            }

            switch ($type) {<commit_msg>Add allow override Doctrine Metadata dentifierFieldNames<commit_after><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace Sonata\CoreBundle\Form\Type;

use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\RegistryInterface;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Metadata\MetadataFactoryInterface;

/**
 * Class DoctrineORMSerializationType
 *
 * This is a doctrine serialization form type that generates a form type from class serialization metadata
 * and doctrine metadata
 *
 * @author <NAME>ieux <<EMAIL>>
 */
class DoctrineORMSerializationType extends AbstractType
{
    /**
     * @var RegistryInterface
     */
    protected $registry;

    /**
     * @var MetadataFactoryInterface
     */
    protected $metadataFactory;

    /**
     * @var string
     */
    protected $class;

    /**
     * @var string
     */
    protected $group;

    /**
     * @var boolean
     */
    protected $identifierOverwrite;

    /**
     * Constructor
     *
     * @param MetadataFactoryInterface $metadataFactory Serializer metadata factory
     * @param ManagerRegistry          $registry        Doctrine registry
     * @param string                   $name            Form type name
     * @param string                   $class           Data class name
     * @param string                   $group           Serialization group name
     */
    public function __construct(MetadataFactoryInterface $metadataFactory, ManagerRegistry $registry, $name, $class, $group, $identifierOverwrite = false)
    {
        $this->metadataFactory = $metadataFactory;
        $this->registry = $registry;
        $this->name  = $name;
        $this->class = $class;
        $this->group = $group;
        $this->identifierOverwrite = $identifierOverwrite;
    }

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

        $manager = $this->registry->getManagerForClass($this->class);
        $doctrineMetadata = $manager->getClassMetadata($this->class);

        foreach ($serializerMetadata->propertyMetadata as $propertyMetadata) {
            $name = $propertyMetadata->name;

            if (in_array($name, $doctrineMetadata->getIdentifierFieldNames()) && !$this->identifierOverwrite) {
                continue;
            }

            if (!in_array($this->group, $propertyMetadata->groups)) {
                continue;
            }

            $type = null;
            $nullable = true;

            if (isset($doctrineMetadata->fieldMappings[$name])) {
                $fieldMetadata = $doctrineMetadata->fieldMappings[$name];
                $type = isset($fieldMetadata['type']) ? $fieldMetadata['type'] : null;
                $nullable = isset($fieldMetadata['nullable']) ? $fieldMetadata['nullable'] : false;
            } else if (isset($doctrineMetadata->associationMappings[$name])) {
                $associationMetadata = $doctrineMetadata->associationMappings[$name];

                if (isset($associationMetadata['joinColumns']['nullable'])) {
                    $nullable = $associationMetadata['joinColumns']['nullable'];
                } else if (isset($associationMetadata['inverseJoinColumns']['nullable'])) {
                    $nullable = $associationMetadata['inverseJoinColumns']['nullable'];
                }
            }

            switch ($type) {
{'fraction_non_alphanumeric': 0.11572023090347698, 'fraction_numerical': 0.0, 'mean_word_length': 2.5207939508506616, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 63, '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': '5281075', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1850, 'n_words': 417}