<commit_before><?php namespace Maer\Entity;

use JsonSerializable;

abstract class Entity implements JsonSerializable
{
    /**
     * Parameter values
     *
     * @var array
     */
    protected $_params  = [];

    /**
     * Removed on json serialization
     *
     * @var array
     */
    protected $_protect = [];

    /**
     * Map one key as another
     *
     * @var array
     */
    protected $_map     = [];

    /**
     * @var boolean
     */
    protected $_ignoreExisting = false;


    /**
     * Create new instance
     *
     * @param array     $data
     */
    public function __construct(array $data = [])
    {
        $this->_ignoreExisting = true;
        foreach($data as $key => $value) {

            if (array_key_exists($key, $this->_map)) {
                $this->{$this->_map[$key]} = $value;
            } else {
                $this->{$key} = $value;
            }

        }
        $this->_ignoreExisting = false;
    }


    /**
     * Get a value from the parameter pool
     *
     * @throws UnknownPropertyException
     */
    public function __get($key)
    {
        if (!array_key_exists($key, $this->_params)) {
            throw new UnknownPropertyException("Unknown property: '{$key}'");
        }

        return $this->_params[$key];
    }


    /**
     * Set a value in the parameter pool and cast it to the same type as
     * the default value
     *
     * @throws UnknownPropertyException
     */
    public function __set($key, $value)
    {
        if (!array_key_exists($key, $this->_params)) {
            if ($this->_ignoreExisting) {
                return;
            }

            throw new UnknownPropertyException("Unknown property: '{$key}'");
        }

        switch(gettype($this->_params[$key])) {

            case "boolean":
                $this->_params[$key] = (bool) $value;
                break;
            case "integer":
                $this->_params[$key] = (integer) $value;
                break;
            case "double":
                $this->_params[$key] = (float) $value;
                break;
            case "string":
                $this->_params[$key] = (string) $value;
                break;
            default:
                $this->_params[$key] = $value;
                break;
        }
    }


    /**
     * Check if a property is set.
     */
    public function __isset($key)
    {
        return isset($this->_params[$key]);
    }


    /**
     * Check if a property exists
     *
     * This is used since property_exists doesn't work to
     * check if a property exists in the $this->_params array
     */
    public function has($key)
    {
        return array_key_exists($key, $this->_params);
    }


    /**
     * Return the params as array and remove the protected keys
     *
     * @return json
     */
    public function toArray($protect = null)
    {
        $protect = is_array($protect)
            ? $protect
            : $this->_protect;

        if ($protect) {
            $new = $this->_params;
            foreach($protect as $key) {
                unset($new[$key]);
            }
            return $new;
        }

        return $this->_params;
    }


    /**
     * Return the params as json and remove the protected keys
     *
     * @return json
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }


    /**
     * Return a parameter as a formatted date string
     *
     * @param  string   $key
     * @param  string   $format
     * @return string
     */
    public function date($key, $format = "F j, Y")
    {
        if (is_numeric($this->_params[$key])) {
            return date($format, $this->_params[$key]);
        }

        return date($format, strtotime($this->_params[$key]));
    }


    /**
     * Convert array to entities
     *
     * @param  array    $data
     * @param  string   $index Set the value in this key as index
     * @return Entity|array
     */
    public static function make($data = null, $index = null)
    {
        if (!is_array($data)) {
            return null;
        }

        if (count($data) < 1) {
            return [];
        }

        // Check if it is a multi dimensional array
        $values = array_values($data);
        $multi  = array_key_exists(0, $values) && is_array($values[0]);


        if ($multi) {
            $list = [];
            foreach($data as $item) {
                if ($index && array_key_exists($index, $item)) {
                    $key = $item[$index];
                    $list[$key] = static::populate($item);
                    continue;
                }
                $list[] = static::populate($item);
            }

            return $list;
        }

        return static::populate($data);
    }


    /**
     * Populate the entity
     *
     * @param  array  $data
     * @return Static
     */
    protected static function populate(array $data)
    {
        return new static($data);
    }

}<commit_msg>Moved __set() action to separate method<commit_after><?php namespace Maer\Entity;

use JsonSerializable;

abstract class Entity implements JsonSerializable
{
    /**
     * Parameter values
     *
     * @var array
     */
    protected $_params  = [];

    /**
     * Removed on json serialization
     *
     * @var array
     */
    protected $_protect = [];

    /**
     * Map one key as another
     *
     * @var array
     */
    protected $_map     = [];

    /**
     * @var boolean
     */
    protected $_ignoreExisting = false;


    /**
     * Create new instance
     *
     * @param array     $data
     */
    public function __construct(array $data = [])
    {
        $this->_ignoreExisting = true;
        foreach($data as $key => $value) {

            if (array_key_exists($key, $this->_map)) {
                $this->{$this->_map[$key]} = $value;
            } else {
                $this->{$key} = $value;
            }

        }
        $this->_ignoreExisting = false;
    }


    /**
     * Get a value from the parameter pool
     *
     * @throws UnknownPropertyException
     */
    public function __get($key)
    {
        if (!array_key_exists($key, $this->_params)) {
            throw new UnknownPropertyException("Unknown property: '{$key}'");
        }

        return $this->_params[$key];
    }


    /**
     * Alias for ::setParam()
     *
     * @throws UnknownPropertyException
     */
    public function __set($key, $value)
    {
        $this->setParam($key, $value);
    }


    /**
     * Set a value in the parameter pool and cast it to the same type as
     * the default value
     *
     * @param  string $key
     * @param  mixed  $value
     *
     * @throws UnknownPropertyException
     */
    protected function setParam($key, $value)
    {
        if (!array_key_exists($key, $this->_params)) {
            if ($this->_ignoreExisting) {
                return;
            }

            throw new UnknownPropertyException("Unknown property: '{$key}'");
        }

        switch(gettype($this->_params[$key])) {

            case "boolean":
                $this->_params[$key] = (bool) $value;
                break;
            case "integer":
                $this->_params[$key] = (integer) $value;
                break;
            case "double":
                $this->_params[$key] = (float) $value;
                break;
            case "string":
                $this->_params[$key] = (string) $value;
                break;
            default:
                $this->_params[$key] = $value;
                break;
        }
    }

    /**
     * Check if a property is set.
     */
    public function __isset($key)
    {
        return isset($this->_params[$key]);
    }


    /**
     * Check if a property exists
     *
     * This is used since property_exists doesn't work to
     * check if a property exists in the $this->_params array
     */
    public function has($key)
    {
        return array_key_exists($key, $this->_params);
    }


    /**
     * Return the params as array and remove the protected keys
     *
     * @return json
     */
    public function toArray($protect = null)
    {
        $protect = is_array($protect)
            ? $protect
            : $this->_protect;

        if ($protect) {
            $new = $this->_params;
            foreach($protect as $key) {
                unset($new[$key]);
            }
            return $new;
        }

        return $this->_params;
    }


    /**
     * Return the params as json and remove the protected keys
     *
     * @return json
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }


    /**
     * Return a parameter as a formatted date string
     *
     * @param  string   $key
     * @param  string   $format
     * @return string
     */
    public function date($key, $format = "F j, Y")
    {
        if (is_numeric($this->_params[$key])) {
            return date($format, $this->_params[$key]);
        }

        return date($format, strtotime($this->_params[$key]));
    }


    /**
     * Convert array to entities
     *
     * @param  array    $data
     * @param  string   $index Set the value in this key as index
     * @return Entity|array
     */
    public static function make($data = null, $index = null)
    {
        if (!is_array($data)) {
            return null;
        }

        if (count($data) < 1) {
            return [];
        }

        // Check if it is a multi dimensional array
        $values = array_values($data);
        $multi  = array_key_exists(0, $values) && is_array($values[0]);


        if ($multi) {
            $list = [];
            foreach($data as $item) {
                if ($index && array_key_exists($index, $item)) {
                    $key = $item[$index];
                    $list[$key] = static::populate($item);
                    continue;
                }
                $list[] = static::populate($item);
            }

            return $list;
        }

        return static::populate($data);
    }


    /**
     * Populate the entity
     *
     * @param  array  $data
     * @return Static
     */
    protected static function populate(array $data)
    {
        return new static($data);
    }

}
{'fraction_non_alphanumeric': 0.1296656298600311, 'fraction_numerical': 0.0005832037325038881, 'mean_word_length': 1.4894749576578756, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 58, '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': '3253435', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2821, 'n_words': 732}