<commit_before><?php

namespace Casa\Generator\Generators\Common;

use Casa\Generator\Utils\DataBaseHelper;
use Casa\Generator\Utils\StringUtils;
use Config;
use Casa\Generator\CommandData;
use Casa\Generator\Generators\GeneratorProvider;
use Casa\Generator\Utils\GeneratorUtils;

class ModelGenerator implements GeneratorProvider
{
    /** @var  CommandData */
    private $commandData;

    /** @var string */
    private $path;

    public function __construct($commandData)
    {
        $this->commandData = $commandData;
        $this->path = Config::get('generator.path_model', app_path('Entities/'));
    }

    public function generate()
    {
        $templateName = 'Model';

        $templateData = $this->commandData->templatesHelper->getTemplate($templateName, 'common');

        $templateData = $this->fillTemplate($templateData);

        $fileName = $this->commandData->modelName.'.php';

        if (!file_exists($this->path)) {
            mkdir($this->path, 0755, true);
        }

        $path = $this->path.$fileName;

        $this->commandData->fileHelper->writeFile($path, $templateData);
        $this->commandData->commandObj->comment("\nModel created: ");
        $this->commandData->commandObj->info($fileName);
    }

    private function fillTemplate($templateData)
    {
        if (!$this->commandData->useSoftDelete) {
            $templateData = str_replace('$SOFT_DELETE_IMPORT$', '', $templateData);
            $templateData = str_replace('$SOFT_DELETE$', '', $templateData);
            $templateData = str_replace('$SOFT_DELETE_DATES$', '', $templateData);
        }

        $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData);

        $fillables = [];

        foreach ($this->commandData->inputFields as $field) {
            $fillables[] = '"'.$field['fieldName'].'"';
        }

        $templateData = str_replace('$FIELDS$', implode(",\n\t\t", $fillables), $templateData);

        $templateData = str_replace('$RULES$', implode(",\n\t\t", $this->generateRules()), $templateData);

        $templateData = str_replace('$CAST$', implode(",\n\t\t", $this->generateCasts()), $templateData);

        $templateData = str_replace('$RELATIONS$', $this->generateRelations(), $templateData);

        return $templateData;
    }

    private function generateRelations()
    {
        if ($this->commandData->tableName == '') return '';
        $code = '';

        //Get what tables it belongs to
        $relations = DataBaseHelper::getForeignKeysFromTable($this->commandData->tableName);
        foreach($relations as $r)
        {
            $code .= "\tpublic function " . StringUtils::singularize($r->REFERENCED_TABLE_NAME) ."() {\n";
            $code .= "\t\t" . '$this->belongsTo(' .  "'" . ucfirst(StringUtils::singularize($r->REFERENCED_TABLE_NAME)) ."', '". $r->COLUMN_NAME . "'); \n";
            $code .= "\t}\n\n";
        }

        //Get what tables it is referended
        $relations = DataBaseHelper::getReferencesFromTable($this->commandData->tableName);
        foreach($relations as $r)
        {
            $code .= "\tpublic function " . StringUtils::singularize($r->REFERENCED_TABLE_NAME) ."() {\n";
            $code .= "\t\t" . '$this->hasMany(' .  "'" . ucfirst(StringUtils::singularize($r->REFERENCED_TABLE_NAME)) ."', '". $r->COLUMN_NAME . "'); \n";
            $code .= "\t}\n\n";
        }

        return $code;

    }

    private function generateRules()
    {
        $rules = [];

        foreach ($this->commandData->inputFields as $field) {<commit_msg>template  modified<commit_after><?php

namespace Casa\Generator\Generators\Common;

use Casa\Generator\Utils\DataBaseHelper;
use Casa\Generator\Utils\StringUtils;
use Config;
use Casa\Generator\CommandData;
use Casa\Generator\Generators\GeneratorProvider;
use Casa\Generator\Utils\GeneratorUtils;

class ModelGenerator implements GeneratorProvider
{
    /** @var  CommandData */
    private $commandData;

    /** @var string */
    private $path;

    public function __construct($commandData)
    {
        $this->commandData = $commandData;
        $this->path = Config::get('generator.path_model', app_path('Entities/'));
    }

    public function generate()
    {
        $templateName = 'Model';

        $templateData = $this->commandData->templatesHelper->getTemplate($templateName, 'common');

        $templateData = $this->fillTemplate($templateData);

        $fileName = $this->commandData->modelName.'.php';

        if (!file_exists($this->path)) {
            mkdir($this->path, 0755, true);
        }

        $path = $this->path.$fileName;

        $this->commandData->fileHelper->writeFile($path, $templateData);
        $this->commandData->commandObj->comment("\nModel created: ");
        $this->commandData->commandObj->info($fileName);
    }

    private function fillTemplate($templateData)
    {
        if (!$this->commandData->useSoftDelete) {
            $templateData = str_replace('$SOFT_DELETE_IMPORT$', '', $templateData);
            $templateData = str_replace('$SOFT_DELETE$', '', $templateData);
            $templateData = str_replace('$SOFT_DELETE_DATES$', '', $templateData);
        }

        $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData);

        $fillables = [];

        foreach ($this->commandData->inputFields as $field) {
            $fillables[] = '"'.$field['fieldName'].'"';
        }

        $templateData = str_replace('$FIELDS$', implode(",\n\t\t", $fillables), $templateData);

        $templateData = str_replace('$RULES$', implode(",\n\t\t", $this->generateRules()), $templateData);

        $templateData = str_replace('$CAST$', implode(",\n\t\t", $this->generateCasts()), $templateData);

        $templateData = str_replace('$RELATIONS$', $this->generateRelations(), $templateData);

        return $templateData;
    }

    private function generateRelations()
    {
        if ($this->commandData->tableName == '') return '';
        $code = '';

        //Get what tables it belongs to
        $relations = DataBaseHelper::getForeignKeysFromTable($this->commandData->tableName);
        foreach($relations as $r)
        {
            $code .= "\tpublic function " . StringUtils::singularize($r->REFERENCED_TABLE_NAME) ."() {\n";
            $code .= "\t\t" . '$this->belongsTo(' .  "'" . ucfirst(StringUtils::singularize($r->REFERENCED_TABLE_NAME)) ."', '". $r->COLUMN_NAME . "'); \n";
            $code .= "\t}\n\n";
        }

        //Get what tables it is referended
        $relations = DataBaseHelper::getReferencesFromTable($this->commandData->tableName);
        foreach($relations as $r)
        {
            $code .= "\tpublic function " . StringUtils::singularize($r->TABLE_NAME) ."() {\n";
            $code .= "\t\t" . '$this->hasMany(' .  "'" . ucfirst(StringUtils::singularize($r->TABLE_NAME)) ."', '". $r->COLUMN_NAME . "'); \n";
            $code .= "\t}\n\n";
        }

        return $code;

    }

    private function generateRules()
    {
        $rules = [];

        foreach ($this->commandData->inputFields as $field) {
{'fraction_non_alphanumeric': 0.17374246882443603, 'fraction_numerical': 0.001120919153706039, 'mean_word_length': 2.88991825613079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, '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': '5559804', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2086, 'n_words': 339}