<commit_before><?php namespace Qlcorp\VextFramework;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Contracts\JsonableInterface;

/**
 * Class VextBlueprint
 *
 * Available Validation Rules:
 * required
 * date
 * min
 * max
 * range
 * grid
 *
 */
class VextBlueprint extends Blueprint implements JsonableInterface, ArrayableInterface {

    protected $current_column;
    protected $current_name;
    protected $model_name = '';
    protected $fillable = array();
    protected $timestamps = 'false';
    protected $userstamps = 'false';
    protected $tree = false;
    protected $parentKey = null;
    protected $pretend = false;
    protected $relationships = array();
    protected $with = array();
    protected $appends = array();

    public function appends($name, $type) {

        //$this->appends[] = $name;
        //$this->fillable = array_merge($this->fillable, $attributes);
        $attributes = compact('name', 'type');
        $this->appends[] = $this->current_column = new VextFluent($attributes, $this);

        return $this->current_column;

    }

    public function hasMany($related, $foreignKey = null, $localKey = null) {
        $type = 'hasMany';
        $this->relationships[] = compact('type', 'related', 'foreignKey', 'localKey');
        $this->with[] = camel_case($related);
    }

    public function addFillable($col) {
        $this->fillable[] = $col;
        return $this;
    }

    public function timestamp($column) {
        return parent::timestamp($column)
            ->fieldConfig(array(
                'fieldLabel' => ucfirst(str_replace('_', ' ', $column))
            ));
    }

    public function timestamps() {
        $this->timestamps = 'true';
        return parent::timestamps();
    }

    public function userstamp($column) {
        //$this->with[] = camel_case($column);
        $this->integer($column)
            ->fieldConfig(array(
                'fieldLabel' => ucfirst(str_replace('_', ' ', $column))
            ));

        $this->foreign($column)
            ->references('id')->on('users');
    }

    public function userstamps() {
        $this->userstamps = 'true';

        $this->userstamp('created_by');
        $this->userstamp('updated_by');

        return $this;
    }

    public function pretend() {
        $this->pretend = true;
        return $this->pretend;
    }

    public function isPretend() {
        return $this->pretend;
    }

    //todo: make this happen after blueprint is done so primarykey can be dynamic
    public function tree() {
        $this->tree = true;

        $this->integer('parentId')->nullable()->fillable();
        $this->integer('index')->fillable()->required();

        //$this->string('text', 200)->fillable()->required();

        $this->boolean('leaf')->fillable()->required();

        //Composite Unique Key ParentId-Index
        //$this->unique(array('parentId', 'index'));

        $this->foreign('parentId')
            ->references('id')
            ->on($this->table)
            ->onDelete('cascade');

        return $this;
    }

    //todo: make this a function of VextFluent for foreignKey purposes
    public function parent($parentKey) {
        $this->parentKey = $parentKey;
        return $this;
    }

    /**
     * Add a new column to the blueprint.
     *
     * @param  string  $type
     * @param  string  $name
     * @param  array   $parameters
     * @return \Illuminate\Support\Fluent
     */
    protected function addColumn($type, $name, array $parameters = array()) {
        $attributes = array_merge(compact('type', 'name'), $parameters);
        $this->columns[] = $this->current_column = new VextFluent($attributes, $this);
        //$column = parent::addColumn($type, $name, $parameters);
        //$this->current_column = new VextFluent($column->getAttributes(),$this);

        $this->current_name = $name;

        return $this->current_column;
    }

    public function model($model) {
        $this->model_name = $model;
    }

    public function getModel() {
        return $this->model_name;
    }

    public function getCurrentColumn() {
        return $this->current_column;
    }
    public function getCurrentName() {
        return $this->current_name;
    }

    public function laravelModel() {
        $stub = $this->getStub('model.stub');
        $stub = str_replace('{{model}}', $this->model_name , $stub);
        return $stub;
    }

    public function laravelBaseModel() {
        $fillable = $this->prettyPrintArray($this->fillable);
        $rules = array();
        $messages = array();
        foreach ($this->columns as $column) {
            //$config = $column->getFieldConfig();
            $laravelRules = array();
            $extJsRules = $column->getRules();
            $name = $column->getName();

            if ( ($required = $column->getRequired()) !== null )  {
                $laravelRules[] = 'required';
            }

            $this->addRule($laravelRules, $extJsRules, 'min', 'minLength');
            $this->addRule($laravelRules, $extJsRules, 'max', 'maxLength');
            $this->addRule($laravelRules, $extJsRules, 'min', 'minValue');
            $this->addRule($laravelRules, $extJsRules, 'max', 'maxValue');
            if ( isset($extJsRules['minText']) ) {
                $messages[] = "'$name.min' => '" . $extJsRules['minText'] . '\'';
            }

            if ( !empty($laravelRules) ) {
                $laravelRules = implode($laravelRules, '|');
                $rules[] = "'$name' => '$laravelRules'";
            }
        }

        $rules = implode($rules, ",\r\n\t\t");
        $messages = implode($messages, "\r\n\t\t");
        if ($this->tree) {
            $stub = $this->getStub('base_tree_model.stub');
        } else {
            $stub = $this->getStub('base_model.stub');
        }

        $parentKey = $this->parentKey;

        $appends = $this->prettyPrintArray($this->getColNames($this->appends));

        $stub = $this->populateStub($stub, compact('fillable', 'rules', 'messages', 'parentKey', 'appends'));
        $stub = $this->addRelationships($stub);
        return $stub;

    }

    protected function getColNames($cols) {
        $names = array();

        foreach ($cols as $col) {
            $names[] = $col->name;
        }

        return $names;
    }

    private function prettyPrintArray($array) {
        return '\'' . implode("',\r\n\t\t'", $array) . '\'';
    }

    protected function addRelationships($stub) {
        $relationships = $this->relationships;
        $relation_stubs = array();

        foreach ($relationships as $relationship) {
            $name = camel_case($relationship['related']);
            $model = studly_case($name);
            $type = $relationship['type'];
            $param_array = array($model, $relationship['foreignKey'], $relationship['localKey']);<commit_msg>Issue 3 - Remove empty string in appends array<commit_after><?php namespace Qlcorp\VextFramework;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Contracts\JsonableInterface;

/**
 * Class VextBlueprint
 *
 * Available Validation Rules:
 * required
 * date
 * min
 * max
 * range
 * grid
 *
 */
class VextBlueprint extends Blueprint implements JsonableInterface, ArrayableInterface {

    protected $current_column;
    protected $current_name;
    protected $model_name = '';
    protected $fillable = array();
    protected $timestamps = 'false';
    protected $userstamps = 'false';
    protected $tree = false;
    protected $parentKey = null;
    protected $pretend = false;
    protected $relationships = array();
    protected $with = array();
    protected $appends = array();

    public function appends($name, $type) {

        //$this->appends[] = $name;
        //$this->fillable = array_merge($this->fillable, $attributes);
        $attributes = compact('name', 'type');
        $this->appends[] = $this->current_column = new VextFluent($attributes, $this);

        return $this->current_column;

    }

    public function hasMany($related, $foreignKey = null, $localKey = null) {
        $type = 'hasMany';
        $this->relationships[] = compact('type', 'related', 'foreignKey', 'localKey');
        $this->with[] = camel_case($related);
    }

    public function addFillable($col) {
        $this->fillable[] = $col;
        return $this;
    }

    public function timestamp($column) {
        return parent::timestamp($column)
            ->fieldConfig(array(
                'fieldLabel' => ucfirst(str_replace('_', ' ', $column))
            ));
    }

    public function timestamps() {
        $this->timestamps = 'true';
        return parent::timestamps();
    }

    public function userstamp($column) {
        //$this->with[] = camel_case($column);
        $this->integer($column)
            ->fieldConfig(array(
                'fieldLabel' => ucfirst(str_replace('_', ' ', $column))
            ));

        $this->foreign($column)
            ->references('id')->on('users');
    }

    public function userstamps() {
        $this->userstamps = 'true';

        $this->userstamp('created_by');
        $this->userstamp('updated_by');

        return $this;
    }

    public function pretend() {
        $this->pretend = true;
        return $this->pretend;
    }

    public function isPretend() {
        return $this->pretend;
    }

    //todo: make this happen after blueprint is done so primarykey can be dynamic
    public function tree() {
        $this->tree = true;

        $this->integer('parentId')->nullable()->fillable();
        $this->integer('index')->fillable()->required();

        //$this->string('text', 200)->fillable()->required();

        $this->boolean('leaf')->fillable()->required();

        //Composite Unique Key ParentId-Index
        //$this->unique(array('parentId', 'index'));

        $this->foreign('parentId')
            ->references('id')
            ->on($this->table)
            ->onDelete('cascade');

        return $this;
    }

    //todo: make this a function of VextFluent for foreignKey purposes
    public function parent($parentKey) {
        $this->parentKey = $parentKey;
        return $this;
    }

    /**
     * Add a new column to the blueprint.
     *
     * @param  string  $type
     * @param  string  $name
     * @param  array   $parameters
     * @return \Illuminate\Support\Fluent
     */
    protected function addColumn($type, $name, array $parameters = array()) {
        $attributes = array_merge(compact('type', 'name'), $parameters);
        $this->columns[] = $this->current_column = new VextFluent($attributes, $this);
        //$column = parent::addColumn($type, $name, $parameters);
        //$this->current_column = new VextFluent($column->getAttributes(),$this);

        $this->current_name = $name;

        return $this->current_column;
    }

    public function model($model) {
        $this->model_name = $model;
    }

    public function getModel() {
        return $this->model_name;
    }

    public function getCurrentColumn() {
        return $this->current_column;
    }
    public function getCurrentName() {
        return $this->current_name;
    }

    public function laravelModel() {
        $stub = $this->getStub('model.stub');
        $stub = str_replace('{{model}}', $this->model_name , $stub);
        return $stub;
    }

    public function laravelBaseModel() {
        $fillable = $this->prettyPrintArray($this->fillable);
        $rules = array();
        $messages = array();
        foreach ($this->columns as $column) {
            //$config = $column->getFieldConfig();
            $laravelRules = array();
            $extJsRules = $column->getRules();
            $name = $column->getName();

            if ( ($required = $column->getRequired()) !== null )  {
                $laravelRules[] = 'required';
            }

            $this->addRule($laravelRules, $extJsRules, 'min', 'minLength');
            $this->addRule($laravelRules, $extJsRules, 'max', 'maxLength');
            $this->addRule($laravelRules, $extJsRules, 'min', 'minValue');
            $this->addRule($laravelRules, $extJsRules, 'max', 'maxValue');
            if ( isset($extJsRules['minText']) ) {
                $messages[] = "'$name.min' => '" . $extJsRules['minText'] . '\'';
            }

            if ( !empty($laravelRules) ) {
                $laravelRules = implode($laravelRules, '|');
                $rules[] = "'$name' => '$laravelRules'";
            }
        }

        $rules = implode($rules, ",\r\n\t\t");
        $messages = implode($messages, "\r\n\t\t");
        if ($this->tree) {
            $stub = $this->getStub('base_tree_model.stub');
        } else {
            $stub = $this->getStub('base_model.stub');
        }

        $parentKey = $this->parentKey;

        $appends = $this->prettyPrintArray($this->getColNames($this->appends));

        $stub = $this->populateStub($stub, compact('fillable', 'rules', 'messages', 'parentKey', 'appends'));
        $stub = $this->addRelationships($stub);
        return $stub;

    }

    protected function getColNames($cols) {
        $names = array();

        foreach ($cols as $col) {
            $names[] = $col->name;
        }

        return $names;
    }

    private function prettyPrintArray($array) {
        if ( empty($array) ) {
            return '';
        } else {
            return '\'' . implode("',\r\n\t\t'", $array) . '\'';
        }
    }

    protected function addRelationships($stub) {
        $relationships = $this->relationships;
        $relation_stubs = array();

        foreach ($relationships as $relationship) {
            $name = camel_case($relationship['related']);
            $model = studly_case($name);
            $type = $relationship['type'];
            $param_array = array($model, $relationship['foreignKey'], $relationship['localKey']);
{'fraction_non_alphanumeric': 0.16281227694503925, 'fraction_numerical': 0.0004996431120628123, 'mean_word_length': 2.3802171290711702, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 165, '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': '4812407', 'n_tokens_mistral': 4142, 'n_tokens_neox': 4029, 'n_words': 814}