<commit_before><?php

namespace Nhlpool;

use Illuminate\Database\Eloquent\Model;

class PoolType extends Model
{
    protected $fillable = ['name', 'rules', 'created_by'];

    /**
     * Get the pools for the pool type.
     */
    public function pools()
    {
        return $this->hasMany('Nhlpool\Pool');
    }

    /**
     * Get the user that owns the pool type.
     */
    public function user()
    {
        return $this->belongsTo('Nhlpool\User', 'created_by');
    }
}
<commit_msg>Methods to save and load the rules in a dynamic column. MariaDB magic -_-<commit_after><?php

namespace Nhlpool;

use Illuminate\Database\Eloquent\Model;

class PoolType extends Model
{
    protected $fillable = ['name', 'created_by'];

    /**
     * Get the pools for the pool type.
     */
    public function pools()
    {
        return $this->hasMany('Nhlpool\Pool');
    }

    /**
     * Get the user that owns the pool type.
     */
    public function user()
    {
        return $this->belongsTo('Nhlpool\User', 'created_by');
    }

    /**
     * Transform a [key => value] array to a [key, value] array
     * @param  array $array key-value array
     * @return array key,value array
     */
    public static function flatten(array $array) : array
    {
        $return = [];
        array_walk_recursive($array, function($val, $key) use (&$return) {
            $return[] = $key;
            $return[] = $val;
        });
        return $return;
    }

    /**
     * [setRules description]
     * @param [type] $pool_type_id [description]
     * @param array  $rules        [description]
     */
    public function setRules(array $rules)
    {
        $pool_type_id = $this->id;
        $flatRules = static::flatten($rules);
        $stringRules = implode("','", $flatRules);
        // UPDATE pool_types SET rules = COLUMN_CREATE('color', 'blue', 'size', 'XL') WHERE id = 1;
        \DB::table('pool_types')
            ->where('id', '=', $pool_type_id)
            ->update(['rules' => \DB::raw("COLUMN_CREATE('$stringRules')")]);
    }

    public function getRules()
    {
        $pool_type_id = $this->id;
        // SELECT COLUMN_JSON(rules) FROM pool_types WHERE id = 1;
        $rules = \DB::table('pool_types')
             ->select(\DB::raw('COLUMN_JSON(rules) AS rules'))
             ->where('id', '=', $pool_type_id)
             ->get();
        return json_decode($rules[0]->rules);
    }
}

{'fraction_non_alphanumeric': 0.15330578512396695, 'fraction_numerical': 0.0012396694214876034, 'mean_word_length': 2.2584118438761775, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '3550151', 'n_tokens_mistral': 769, 'n_tokens_neox': 759, 'n_words': 188}