<commit_before><?php
namespace PSFS\base\types\helpers;

use Propel\Generator\Model\PropelTypes;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\DataFetcher\ArrayDataFetcher;
use Propel\Runtime\Formatter\ObjectFormatter;
use Propel\Runtime\Map\ColumnMap;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Map\TableMapTrait;
use PSFS\base\config\Config;
use PSFS\base\dto\Field;
use PSFS\base\dto\Form;
use PSFS\base\Logger;
use PSFS\base\Router;
use PSFS\base\types\Api;

/**
 * Class ApiHelper
 * @package PSFS\base\types\helpers
 */
class ApiHelper
{
    /**
     * @param string $map
     * @param string $domain
     * @return Form
     */
    public static function generateFormFields($map, $domain)
    {
        $form = new Form();
        /** @var TableMap $tableMap */
        $tableMap = $map::getTableMap();
        $behaviors = $tableMap->getBehaviors();
        foreach ($map::getFieldNames() as $field) {
            $fDto = self::parseFormField($domain, $tableMap, $field, $behaviors);
            if(null !== $fDto) {
                $form->addField($fDto);
            }
        }

        if(array_key_exists('i18n', $behaviors)) {
            $relateI18n = $tableMap->getRelation($tableMap->getPhpName() . 'I18n');
            if(null !== $relateI18n) {
                $i18NTableMap = $relateI18n->getLocalTable();
                foreach($i18NTableMap->getColumns() as $columnMap) {
                    if(!$form->fieldExists($columnMap->getPhpName())) {
                        $fDto = self::parseFormField($domain, $i18NTableMap, $columnMap->getPhpName(), $i18NTableMap->getBehaviors());
                        if(null !== $fDto) {
                            $fDto->pk = false;
                            $form->addField($fDto);
                        }
                    }
                }
            }
        }

        return $form;
    }

    /**
     * Extract the foreign relation field
     * @param ColumnMap $mappedColumn
     * @param string $field
     * @param string $domain
     * @return Field
     */
    public static function extractForeignModelsField(ColumnMap $mappedColumn, $field, $domain)
    {
        $fDto = new Field($field, t($field));
        $fDto->type = Field::COMBO_TYPE;
        $fDto->required = $mappedColumn->isNotNull();
        $foreignTable = $mappedColumn->getRelation()->getForeignTable();
        $relatedModel = strtolower($foreignTable->getPhpName());
        $fDto->entity = $relatedModel;
        $relatedField = $foreignTable->getColumn($mappedColumn->getRelatedColumnName());
        $fDto->relatedField = $relatedField->getPhpName();
        $fDto->url = Router::getInstance()->getRoute(strtolower($domain) . '-api-' . $relatedModel);
        return $fDto;
    }

    /**
     * @param $field
     * @param string string $type
     * @param boolean $required
     * @return Field
     */
    private static function createField($field, $type = Field::TEXT_TYPE, $required = false)
    {
        $fDto = new Field($field, t($field));
        $fDto->type = $type;
        $fDto->required = $required;
        return $fDto;
    }

    /**
     * Extract primary key field
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generatePrimaryKeyField($field, $required = false)
    {
        $fDto = self::createField($field, Field::HIDDEN_TYPE, $required);
        $fDto->required = false;
        $fDto->pk = true;
        return $fDto;
    }

    /**
     * Extract numeric field
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateNumericField($field, $required = false)
    {
        return self::createField($field, Field::NUMBER_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateStringField($field, $required = false)
    {
        return self::createField($field, Field::TEXT_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateBooleanField($field, $required = false)
    {
        return self::createField($field, Field::SWITCH_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generatePasswordField($field, $required = false)
    {
        return self::createField($field, Field::PASSWORD_FIELD, $required);
    }

    /**
     * Extract date fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateDateField($field, $required = false)
    {
        return self::createField($field, Field::DATE, $required);
    }

    /**
     * @param $field
     * @param bool $required
     * @return Field
     */
    public static function generateEnumField($field, $required = false)
    {
        return self::createField($field, Field::COMBO_TYPE, $required);
    }

    /**
     * Check if parametrized field exists in api model
     * @param TableMap $tableMap
     * @param $field
     * @return \Propel\Runtime\Map\ColumnMap|null
     */
    public static function checkFieldExists(TableMap $tableMap, $field)
    {
        $column = null;
        try {
            foreach($tableMap->getColumns() as $tableMapColumn) {
                $columnName = $tableMapColumn->getPhpName();
                if(preg_match('/'.$field.'/i', $columnName) && strlen($field) === strlen($columnName)) {
                    $column = $tableMapColumn;
                    break;
                }
            }
        } catch (\Exception $e) {
            Logger::log($e->getMessage(), LOG_DEBUG);
        }
        return $column;
    }
<commit_msg>Fix case insensitive query filters II<commit_after><?php
namespace PSFS\base\types\helpers;

use Propel\Generator\Model\PropelTypes;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\DataFetcher\ArrayDataFetcher;
use Propel\Runtime\Formatter\ObjectFormatter;
use Propel\Runtime\Map\ColumnMap;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Map\TableMapTrait;
use PSFS\base\config\Config;
use PSFS\base\dto\Field;
use PSFS\base\dto\Form;
use PSFS\base\Logger;
use PSFS\base\Router;
use PSFS\base\types\Api;

/**
 * Class ApiHelper
 * @package PSFS\base\types\helpers
 */
class ApiHelper
{
    /**
     * @param string $map
     * @param string $domain
     * @return Form
     */
    public static function generateFormFields($map, $domain)
    {
        $form = new Form();
        /** @var TableMap $tableMap */
        $tableMap = $map::getTableMap();
        $behaviors = $tableMap->getBehaviors();
        foreach ($map::getFieldNames() as $field) {
            $fDto = self::parseFormField($domain, $tableMap, $field, $behaviors);
            if(null !== $fDto) {
                $form->addField($fDto);
            }
        }

        if(array_key_exists('i18n', $behaviors)) {
            $relateI18n = $tableMap->getRelation($tableMap->getPhpName() . 'I18n');
            if(null !== $relateI18n) {
                $i18NTableMap = $relateI18n->getLocalTable();
                foreach($i18NTableMap->getColumns() as $columnMap) {
                    if(!$form->fieldExists($columnMap->getPhpName())) {
                        $fDto = self::parseFormField($domain, $i18NTableMap, $columnMap->getPhpName(), $i18NTableMap->getBehaviors());
                        if(null !== $fDto) {
                            $fDto->pk = false;
                            $form->addField($fDto);
                        }
                    }
                }
            }
        }

        return $form;
    }

    /**
     * Extract the foreign relation field
     * @param ColumnMap $mappedColumn
     * @param string $field
     * @param string $domain
     * @return Field
     */
    public static function extractForeignModelsField(ColumnMap $mappedColumn, $field, $domain)
    {
        $fDto = new Field($field, t($field));
        $fDto->type = Field::COMBO_TYPE;
        $fDto->required = $mappedColumn->isNotNull();
        $foreignTable = $mappedColumn->getRelation()->getForeignTable();
        $relatedModel = strtolower($foreignTable->getPhpName());
        $fDto->entity = $relatedModel;
        $relatedField = $foreignTable->getColumn($mappedColumn->getRelatedColumnName());
        $fDto->relatedField = $relatedField->getPhpName();
        $fDto->url = Router::getInstance()->getRoute(strtolower($domain) . '-api-' . $relatedModel);
        return $fDto;
    }

    /**
     * @param $field
     * @param string string $type
     * @param boolean $required
     * @return Field
     */
    private static function createField($field, $type = Field::TEXT_TYPE, $required = false)
    {
        $fDto = new Field($field, t($field));
        $fDto->type = $type;
        $fDto->required = $required;
        return $fDto;
    }

    /**
     * Extract primary key field
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generatePrimaryKeyField($field, $required = false)
    {
        $fDto = self::createField($field, Field::HIDDEN_TYPE, $required);
        $fDto->required = false;
        $fDto->pk = true;
        return $fDto;
    }

    /**
     * Extract numeric field
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateNumericField($field, $required = false)
    {
        return self::createField($field, Field::NUMBER_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateStringField($field, $required = false)
    {
        return self::createField($field, Field::TEXT_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateBooleanField($field, $required = false)
    {
        return self::createField($field, Field::SWITCH_TYPE, $required);
    }

    /**
     * Extract string fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generatePasswordField($field, $required = false)
    {
        return self::createField($field, Field::PASSWORD_FIELD, $required);
    }

    /**
     * Extract date fields
     * @param string $field
     * @param boolean $required
     * @return Field
     */
    public static function generateDateField($field, $required = false)
    {
        return self::createField($field, Field::DATE, $required);
    }

    /**
     * @param $field
     * @param bool $required
     * @return Field
     */
    public static function generateEnumField($field, $required = false)
    {
        return self::createField($field, Field::COMBO_TYPE, $required);
    }

    /**
     * Check if parametrized field exists in api model
     * @param TableMap $tableMap
     * @param $field
     * @return \Propel\Runtime\Map\ColumnMap|null
     */
    public static function checkFieldExists(TableMap $tableMap, $field)
    {
        $column = null;
        try {
            foreach($tableMap->getColumns() as $tableMapColumn) {
                $columnName = $tableMapColumn->getPhpName();
                if(preg_match('/^'.$field.'$/i', $columnName)) {
                    $column = $tableMapColumn;
                    break;
                }
            }
        } catch (\Exception $e) {
            Logger::log($e->getMessage(), LOG_DEBUG);
        }
        return $column;
    }

{'fraction_non_alphanumeric': 0.13213287288274955, 'fraction_numerical': 0.0029600394671928957, 'mean_word_length': 2.289075175770687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '6798480', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3549, 'n_words': 743}