<commit_before><?php

namespace App\Models;

use Nova\Database\ORM\Model as BaseModel;


class Option extends BaseModel
{
    protected $table = 'options';

    protected $primaryKey = 'id';

    protected $fillable = array('group', 'item', 'value');

    public $timestamps = false;


    public function getValueAttribute($value)
    {
        return $this->maybeDecode($value);
    }

    public function setValueAttribute($value)
    {
        $this->attributes['value'] = $this->maybeEncode($value);
    }

    public static function set($key, $value)
    {
        list($namespace, $group, $item) = static::parseKey($key);

        // Prepare the record variables.
        $attributes = array(
            'namespace' => $namespace,
            'group'     => $group,
            'item'      => $item
        );

        $values = array(
            'value' => $value
        );

        return static::updateOrCreate($attributes, $values);
    }

    /**
     * Parse a key into namespace, group, and item.
     *
     * @param  string  $key
     * @return array
     */
    protected static function parseKey($key)
    {
        if (strpos($key, '::') === false) {
            $segments = explode('.', $key);

            return static::parseBasicSegments($segments);
        }

        return static::parseNamespacedSegments($key);
    }

    /**
     * Parse an array of basic segments.
     *
     * @param  array  $segments
     * @return array
     */
    protected static function parseBasicSegments(array $segments)
    {
        $group = $segments[0];

        if (count($segments) == 1) {
            return array(null, $group, null);
        }

        $item = implode('.', array_slice($segments, 1));

        return array(null, $group, $item);
    }

    /**
     * Parse an array of namespaced segments.
     *
     * @param  string  $key
     * @return array
     */
    protected static function parseNamespacedSegments($key)
    {
        list($namespace, $item) = explode('::', $key);

        $itemSegments = explode('.', $item);

        $groupAndItem = array_slice(static::parseBasicSegments($itemSegments), 1);

        return array_merge(array($namespace), $groupAndItem);
    }

    /**
     * Decode value only if it was encoded to JSON.
     *
     * @param  string  $original
     * @param  bool  $assoc
     * @return mixed
     */
    protected function maybeDecode($original, $assoc = true)
    {
        if (is_numeric($original)) return $original;

        $data = json_decode($original, $assoc);

        return (is_object($data) || is_array($data)) ? $data : $original;
    }

    /**
     * Encode data to JSON, if needed.
     *
     * @param  mixed  $data
     * @return mixed
     */
    protected function maybeEncode($data)
    {<commit_msg>Improve the App\Models\Option<commit_after><?php

namespace App\Models;

use Nova\Database\ORM\Model as BaseModel;


class Option extends BaseModel
{
    protected $table = 'options';

    protected $primaryKey = 'id';

    protected $fillable = array('namespace', 'group', 'item', 'value');

    public $timestamps = false;


    public function getValueAttribute($value)
    {
        return $this->maybeDecode($value);
    }

    public function setValueAttribute($value)
    {
        $this->attributes['value'] = $this->maybeEncode($value);
    }

    public static function set($key, $value)
    {
        list($namespace, $group, $item) = static::parseKey($key);

        // Prepare the record variables.
        $attributes = array(
            'namespace' => $namespace,
            'group'     => $group,
            'item'      => $item
        );

        $values = array(
            'value' => $value
        );

        return static::updateOrCreate($attributes, $values);
    }

    /**
     * Parse a key into namespace, group, and item.
     *
     * @param  string  $key
     * @return array
     */
    protected static function parseKey($key)
    {
        if (strpos($key, '::') === false) {
            $segments = explode('.', $key);

            return static::parseBasicSegments($segments);
        }

        return static::parseNamespacedSegments($key);
    }

    /**
     * Parse an array of basic segments.
     *
     * @param  array  $segments
     * @return array
     */
    protected static function parseBasicSegments(array $segments)
    {
        $group = $segments[0];

        if (count($segments) == 1) {
            return array(null, $group, null);
        }

        $item = implode('.', array_slice($segments, 1));

        return array(null, $group, $item);
    }

    /**
     * Parse an array of namespaced segments.
     *
     * @param  string  $key
     * @return array
     */
    protected static function parseNamespacedSegments($key)
    {
        list($namespace, $item) = explode('::', $key);

        $itemSegments = explode('.', $item);

        $groupAndItem = array_slice(
            static::parseBasicSegments($itemSegments), 1
        );

        return array_merge(array($namespace), $groupAndItem);
    }

    /**
     * Decode value only if it was encoded to JSON.
     *
     * @param  string  $original
     * @param  bool  $assoc
     * @return mixed
     */
    protected function maybeDecode($original, $assoc = true)
    {
        if (is_numeric($original)) return $original;

        $data = json_decode($original, $assoc);

        return (is_object($data) || is_array($data)) ? $data : $original;
    }

    /**
     * Encode data to JSON, if needed.
     *
     * @param  mixed  $data
     * @return mixed
     */
    protected function maybeEncode($data)
    {
{'fraction_non_alphanumeric': 0.1373733783543629, 'fraction_numerical': 0.0014217167229429537, 'mean_word_length': 2.1128318584070795, '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': '4472468', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1616, 'n_words': 402}