<commit_before><?php

namespace Bluora\LaravelModelChangeTracking;

use Auth;
use Config;
use Diff\Differ\MapDiffer;

trait LogChangeTrait
{
    /**
     * Disable trimming of column values.
     *
     * @param var
     */
    protected static $disable_trim_values = false;

    /**
     * No logging of the standard date columns.
     *
     * @param array
     */
    protected static $do_not_log_fields = ['created_at', 'updated_at', 'archived_at', 'deleted_at'];

    /**
     * Boot the log changes trait for a model.
     *
     * @return void
     */
    public static function bootLogChangeTrait()
    {
        // Saving model event
        static::saving(function ($model) {

            // Ensure empty string values for dates are converted to null
            $casts = $model->getDates();
            foreach ($model->getDirty() as $column_name => $value) {
                if (in_array($column_name, $casts) && $value === '') {
                    $model->$column_name = null;
                }
            }

            // Trim all values
            if (!static::$disable_trim_values) {
                foreach ($model->getDirty() as $column_name => $value) {
                    if (!is_null($value)) {
                        $model->$column_name = trim($value);
                    }
                }
            }
        });

        // Saved model event
        static::saved(function ($model) {

            // Track all changes to every column
            $casts = $model->getCasts();
            foreach ($model->getDirty(true) as $column_name => $value) {

                // Ignore columns found in the do_not_log variable, and ignore non-json columns<commit_msg>Run a check on the dirty values to ensure they are appropiate for insert/update<commit_after><?php

namespace Bluora\LaravelModelChangeTracking;

use Auth;
use Config;
use Diff\Differ\MapDiffer;

trait LogChangeTrait
{
    /**
     * Disable trimming of column values.
     *
     * @param var
     */
    protected static $disable_trim_values = false;

    /**
     * No logging of the standard date columns.
     *
     * @param array
     */
    protected static $do_not_log_fields = ['created_at', 'updated_at', 'archived_at', 'deleted_at'];

    /**
     * Run a dirty check before returning what is dirty.
     *
     * @return array
     */
    public function preDirtyCheck()
    {
        // Ensure empty string values for dates are converted to null
        $casts = $this->getDates();
        foreach ($this->getDirty() as $column_name => $value) {
            if (in_array($column_name, $casts) && $value === '') {
                $this->$column_name = null;
            }
        }

        // Trim all values
        if (!static::$disable_trim_values) {
            foreach ($this->getDirty() as $column_name => $value) {
                if (!is_null($value)) {
                    $this->$column_name = trim($value);
                }
            }
        }
        return $this->getDirty();
    }

    /**
     * Boot the log changes trait for a model.
     *
     * @return void
     */
    public static function bootLogChangeTrait()
    {
        // Saving model event
        static::saving(function ($model) {
            $model->preDirtyCheck();
        });

        // Saved model event
        static::saved(function ($model) {

            // Track all changes to every column
            $casts = $model->getCasts();
            foreach ($model->getDirty(true) as $column_name => $value) {

                // Ignore columns found in the do_not_log variable, and ignore non-json columns
{'fraction_non_alphanumeric': 0.1173370319001387, 'fraction_numerical': 0.0, 'mean_word_length': 1.7953488372093023, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '733907', 'n_tokens_mistral': 1025, 'n_tokens_neox': 996, 'n_words': 275}