<commit_before><?php


namespace BasicTablePackage\View\FormView;


use BasicTablePackage\FieldConfigurationOverride\EntityFieldOverrides;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldType;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldTypeReader;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldTypes;
use BasicTablePackage\FieldTypeDetermination\ReferencingPersistenceFieldType;
use BasicTablePackage\View\FormView\ValueTransformers\ValueTransformerConfiguration;
use function BasicTablePackage\Lib\collect as collect;

class FormViewConfigurationFactory
{

    /**
     * @var PersistenceFieldTypeReader
     */
    private $persistenceFieldTypeReader;
    /**
     * @var WysiwygEditorFactory
     */
    private $wysiwygEditorFactory;
    /**
     * @var EntityFieldOverrides
     */
    private $entityFieldOverrides;
    /**
     * @var ValueTransformerConfiguration
     */
    private $valueTransformerConfiguration;

    /**
     * @param PersistenceFieldTypeReader $persistenceFieldTypeReader
     * @param WysiwygEditorFactory $wysiwygEditorFactory
     * @param EntityFieldOverrides $entityFieldOverrides
     * @param ValueTransformerConfiguration $valueTransformerConfiguration
     */
    public function __construct(
        PersistenceFieldTypeReader $persistenceFieldTypeReader,
        WysiwygEditorFactory $wysiwygEditorFactory,
        EntityFieldOverrides $entityFieldOverrides,
        ValueTransformerConfiguration $valueTransformerConfiguration
    ) {
        $this->persistenceFieldTypeReader = $persistenceFieldTypeReader;
        $this->wysiwygEditorFactory = $wysiwygEditorFactory;
        $this->entityFieldOverrides = $entityFieldOverrides;
        $this->valueTransformerConfiguration = $valueTransformerConfiguration;
    }

    public function createConfiguration(): FormViewFieldConfiguration
    {
        $fieldTypes =
            collect($this->persistenceFieldTypeReader->getPersistenceFieldTypes())
                ->map(function ($persistenceFieldType, $key) {
                    return self::createFieldTypeOf($persistenceFieldType,
                        $key);
                });
        return new FormViewFieldConfiguration($fieldTypes->toArray());
    }

    private function createFieldTypeOf(PersistenceFieldType $persistenceFieldType, string $key)
    {
        if (isset($this->entityFieldOverrides[$key]) &&
            isset($this->entityFieldOverrides[$key][Field::class])) {
            return $this->entityFieldOverrides[$key][Field::class];
        }
        $valueTransformer = $this->valueTransformerConfiguration->getTransformerFor($persistenceFieldType);
        switch ($persistenceFieldType->getType()) {
            case PersistenceFieldTypes::STRING:
                return function ($entity) use ($key, $valueTransformer) {
                    return new TextField($key,
                        $key,
                        $valueTransformer->transform(self::extractSqlValueOfEntity($entity, $key)));
                };
            case PersistenceFieldTypes::INTEGER:
                return function ($entity) use ($key, $valueTransformer) {
                    return new IntegerField($key,
                        $key,
                        $valueTransformer->transform(self::extractSqlValueOfEntity($entity, $key)));
                };
            case PersistenceFieldTypes::DATE:
                return function ($entity) use ($key, $valueTransformer) {
                    return new DateField($key,
                        $key,
                        $valueTransformer->transform(self::extractSqlValueOfEntity($entity, $key)));
                };
            case PersistenceFieldTypes::DATETIME:
                return function ($entity) use ($key, $valueTransformer) {
                    return new DateTimeField($key,
                        $key,
                        $valueTransformer->transform(self::extractSqlValueOfEntity($entity, $key)));
                };
            case PersistenceFieldTypes::TEXT:
                return function ($entity) use ($key) {
                    return new WysiwygField($this->wysiwygEditorFactory->createEditor(),
                        $key,
                        $key,
                        self::extractSqlValueOfEntity($entity, $key));
                };
            case PersistenceFieldTypes::MANY_TO_ONE:
                return function ($entity) use ($key, $persistenceFieldType) {
                    /** @var ReferencingPersistenceFieldType $persistenceFieldType */<commit_msg>refactor/FormViewConfigurationFactory: curry ValueTransformer::transform call<commit_after><?php


namespace BasicTablePackage\View\FormView;


use BasicTablePackage\FieldConfigurationOverride\EntityFieldOverrides;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldType;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldTypeReader;
use BasicTablePackage\FieldTypeDetermination\PersistenceFieldTypes;
use BasicTablePackage\FieldTypeDetermination\ReferencingPersistenceFieldType;
use BasicTablePackage\View\FormView\ValueTransformers\ValueTransformerConfiguration;
use function BasicTablePackage\Lib\collect as collect;

class FormViewConfigurationFactory
{

    /**
     * @var PersistenceFieldTypeReader
     */
    private $persistenceFieldTypeReader;
    /**
     * @var WysiwygEditorFactory
     */
    private $wysiwygEditorFactory;
    /**
     * @var EntityFieldOverrides
     */
    private $entityFieldOverrides;
    /**
     * @var ValueTransformerConfiguration
     */
    private $valueTransformerConfiguration;

    /**
     * @param PersistenceFieldTypeReader $persistenceFieldTypeReader
     * @param WysiwygEditorFactory $wysiwygEditorFactory
     * @param EntityFieldOverrides $entityFieldOverrides
     * @param ValueTransformerConfiguration $valueTransformerConfiguration
     */
    public function __construct(
        PersistenceFieldTypeReader $persistenceFieldTypeReader,
        WysiwygEditorFactory $wysiwygEditorFactory,
        EntityFieldOverrides $entityFieldOverrides,
        ValueTransformerConfiguration $valueTransformerConfiguration
    ) {
        $this->persistenceFieldTypeReader = $persistenceFieldTypeReader;
        $this->wysiwygEditorFactory = $wysiwygEditorFactory;
        $this->entityFieldOverrides = $entityFieldOverrides;
        $this->valueTransformerConfiguration = $valueTransformerConfiguration;
    }

    public function createConfiguration(): FormViewFieldConfiguration
    {
        $fieldTypes =
            collect($this->persistenceFieldTypeReader->getPersistenceFieldTypes())
                ->map(function ($persistenceFieldType, $key) {
                    return self::createFieldTypeOf($persistenceFieldType,
                        $key);
                });
        return new FormViewFieldConfiguration($fieldTypes->toArray());
    }

    private function createFieldTypeOf(PersistenceFieldType $persistenceFieldType, string $key)
    {
        if (isset($this->entityFieldOverrides[$key]) &&
            isset($this->entityFieldOverrides[$key][Field::class])) {
            return $this->entityFieldOverrides[$key][Field::class];
        }
        $valueTransformer = $this->valueTransformerConfiguration->getTransformerFor($persistenceFieldType);
        $getValue = function ($entity, $key) use ($valueTransformer) {
            return $valueTransformer->transform(self::extractSqlValueOfEntity($entity, $key));
        };
        switch ($persistenceFieldType->getType()) {
            case PersistenceFieldTypes::STRING:
                return function ($entity) use ($key, $getValue) {
                    return new TextField($key, $key, $getValue($entity, $key));
                };
            case PersistenceFieldTypes::INTEGER:
                return function ($entity) use ($key, $getValue) {
                    return new IntegerField($key, $key, $getValue($entity, $key));
                };
            case PersistenceFieldTypes::DATE:
                return function ($entity) use ($key, $getValue) {
                    return new DateField($key, $key, $getValue($entity, $key));
                };
            case PersistenceFieldTypes::DATETIME:
                return function ($entity) use ($key, $getValue) {
                    return new DateTimeField($key, $key, $getValue($entity, $key));
                };
            case PersistenceFieldTypes::TEXT:
                return function ($entity) use ($key) {
                    return new WysiwygField($this->wysiwygEditorFactory->createEditor(),
                        $key,
                        $key,
                        self::extractSqlValueOfEntity($entity, $key));
                };
            case PersistenceFieldTypes::MANY_TO_ONE:
                return function ($entity) use ($key, $persistenceFieldType) {
                    /** @var ReferencingPersistenceFieldType $persistenceFieldType */
{'fraction_non_alphanumeric': 0.09246231155778895, 'fraction_numerical': 0.0, 'mean_word_length': 2.725457570715474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '3012528', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2123, 'n_words': 372}