<commit_before><?php namespace Teepluss\Harvey;

use Closure;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

abstract class Harvey extends Model {

    public static $rules = array();

    public static $messages = array();

    protected $errors;

    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);

        // Message bag.
        $this->errors = new MessageBag;
    }

    public function validate(array $rules, array $messages = array(), array $inputs = array())
    {
        // Define passed state.
        $passed = false;

        // Validation rules.
        $rules = ($rules) ?: static::$rules;

        // Validation custom messages.
        $messages = ($messages) ?: static::$messages;

        // Input fill in.
        $inputs = ($inputs) ?: Input::all();

        // Validator instance.
        $validator = Validator::make($inputs, $rules, $messages);

        if ($validator->fails())
        {
            $passed = false;

            $this->errors = $validator->messages();
        }

        return $passed;
    }

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

    protected function transform($rules, &$laws = array())
    {
        // Event.
        $event = $this->exists ? 'update' : 'create';

        foreach ($rules as $key => $rule)
        {
            if (preg_match('|on([A-Z][a-z]+)|', $key, $matches))
            {
                $on = strtolower($matches[1]);

                if ($on == $event)
                {
                    $this->transform($rule, $laws);
                }
            }
            else
            {
                $rule = is_array($rule) ? $rule : explode('|', $rule);

                if (array_key_exists($key, $laws))
                {
                    $rule = array_merge($laws[$key], $rule);

                    $rule = array_unique($rule);
                }

                $laws[$key] = $rule;
            }
        }

        return $laws;
    }

    protected function internalSave(array $options)
    {
        // Rule defined.
        $rules = static::$rules;

        // Compare with changed fields.
        $changes = $this->getDirty();

        // Transform format to Laravel rules.
        $laws = $this->transform($rules);

        if ($this->validate($laws))
        {
            return parent::save($options);
        }

        return false;
    }

    public function save(array $options = array())
    {
        return $this->internalSave($options);
    }

}<commit_msg>validate<commit_after><?php namespace Teepluss\Harvey;

use Closure;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

abstract class Harvey extends Model {

    public static $rules = array();

    public static $messages = array();

    protected $errors;

    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);

        // Message bag.
        $this->errors = new MessageBag;
    }

    public function validate(array $rules, array $messages = array(), array $inputs = array())
    {
        // Define passed state.
        $passed = true;

        // Validation custom messages.
        $messages = ($messages) ?: static::$messages;

        // Input fill in.
        $inputs = ($inputs) ?: $this->getDirty();

        //sd($rules);

        // Validator instance.
        $validator = Validator::make($inputs, $rules, $messages);

        if ($validator->fails())
        {
            $passed = false;

            $this->errors = $validator->messages();
        }

        return $passed;
    }

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

    protected function transform($rules, &$laws = array())
    {
        // Event.
        $event = $this->exists ? 'update' : 'create';

        // Compare with changed fields.
        $changes = $this->getDirty();

        foreach ($rules as $key => $rule)
        {
            if (preg_match('|on([A-Z][a-z]+)|', $key, $matches))
            {
                $on = strtolower($matches[1]);

                if ($on == $event)
                {
                    $this->transform($rule, $laws);
                }
            }
            else
            {
                if ( ! array_key_exists($key, $changes)) continue;

                $rule = is_array($rule) ? $rule : explode('|', $rule);

                if (array_key_exists($key, $laws))
                {
                    $rule = array_merge($laws[$key], $rule);

                    $rule = array_unique($rule);
                }

                $laws[$key] = $rule;
            }
        }

        return $laws;
    }

    protected function internalSave(array $options)
    {
        // Rule defined.
        $rules = static::$rules;

        // Transform format to Laravel rules.
        $laws = $this->transform($rules);

        if ($this->validate($laws))
        {
            return parent::save($options);
        }

        return false;
    }

    public function save(array $options = array())
    {
        return $this->internalSave($options);
    }

}
{'fraction_non_alphanumeric': 0.1409559512652296, 'fraction_numerical': 0.00037488284910965324, 'mean_word_length': 1.6586945690084705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '643133', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1434, 'n_words': 336}