<commit_before><?php

namespace Sectorr\Core\Database;

use Sectorr\Core\Config;
use Sectorr\Core\Contracts\CrudContract;

abstract class Model implements CrudContract
{

    protected $db;
    protected $where = ["AND" => []];

    /**
     * Model constructor.
     */
    public function __construct()
    {
        $this->db = new Database();
    }

    /**
     * Find item by ID.
     *
     * @param $id
     * @return mixed
     */
    public function _find($id)
    {
        return $this->db->get($this->table, '*', ['id' => $id]);
    }

    /**
     * Adds a where statement to the current query.
     *
     * @param $field
     * @param $input
     * @return $this
     */
    public function _where($field, $input)
    {
        $this->where['AND'][$field] = $input;
        return $this;
    }

    /**
     * Get's the first result.
     *
     * @param string $columns
     * @return bool
     */
    public function _first($columns = '*')
    {
        return $this->db->get($this->table, $columns, $this->where);
    }

    /**
     * Get all results filtered on set where.
     *
     * @param string $columns
     * @return array|bool
     */
    public function _get($columns = '*')
    {
        return $this->db->select($this->table, $columns, $this->where);
    }

    /**
     * Get all results from table.
     *
     * @param string $columns
     * @return array|bool
     */
    public function _all($columns = '*')
    {
        return $this->db->select($this->table, $columns);
    }

    /**
     * Insert new item into table.
     *
     * @param array $data
     * @return array
     */
    public function _create(array $data)
    {
        return $this->db->insert($this->table, $data);
    }

    /**
     * Updates item in table.
     *
     * @param $id
     * @param array $data
     * @return bool|int
     */
    public function _update($id, array $data)
    {
        return $this->db->update($this->table, $data, ['id' => $id]);
    }

    /**
     * Deletes item from table.
     *
     * @param $id
     * @return bool|int
     */
    public function _delete($id)
    {
        return $this->db->delete($this->table, ['id' => $id]);
    }

    /**
     * Call method normally.
     *
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        return call_user_func_array([$this, '_' . $name], $arguments);
    }

    /**
     * Call method statically.
     *
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return call_user_func_array([(new static()), '_' . $name], $arguments);
    }
}
<commit_msg>Dynamic properties in model<commit_after><?php

namespace Sectorr\Core\Database;

use Sectorr\Core\Config;
use Sectorr\Core\Contracts\CrudContract;

abstract class Model
{

    protected $db;
    protected $where = ["AND" => []];
    private $fields = [];

    /**
     * Model constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = [])
    {
        $this->db = new Database();
        $this->setProperties($fields);
    }

    /**
     * Find item by ID.
     *
     * @param $id
     * @return mixed
     */
    protected function _find($id)
    {
        return $this->setProperties($this->db->get($this->table, '*', ['id' => $id]));
    }

    /**
     * Adds a where statement to the current query.
     *
     * @param $field
     * @param $input
     * @return $this
     */
    protected function _where($field, $input)
    {
        $this->where['AND'][$field] = $input;
        return $this;
    }

    /**
     * Get's the first result.
     *
     * @param string $columns
     * @return bool
     */
    protected function _first($columns = '*')
    {
        return $this->setProperties($this->db->get($this->table, $columns, $this->where));
    }

    /**
     * Get all results filtered on set where.
     *
     * @param string $columns
     * @return array|bool
     */
    protected function _get($columns = '*')
    {
        return $this->getModelObjects($this->db->select($this->table, $columns, $this->where));
    }

    /**
     * Get all results from table.
     *
     * @param string $columns
     * @return array|bool
     */
    protected function _all($columns = '*')
    {
        return $this->getModelObjects($this->db->select($this->table, $columns));
    }

    /**
     * Insert new item into table.
     *
     * @param array $data
     * @return array
     */
    protected function _create(array $data)
    {
        return $this->db->insert($this->table, $data);
    }

    /**
     * Updates item in table.
     *
     * @param $id
     * @param array $data
     * @return bool|int
     */
    protected function _update($id, array $data)
    {
        return $this->db->update($this->table, $data, ['id' => $id]);
    }

    /**
     * Deletes item from table.
     *
     * @param $id
     * @return bool|int
     */
    protected function _delete($id)
    {
        return $this->db->delete($this->table, ['id' => $id]);
    }

    public function getModelObjects(array $results)
    {
        foreach($results as $key => $result) {
            $results[$key] = new $this($result);
        }

        return $results;
    }

    /**
     * Get dynamic property.
     *
     * @param $key
     * @return null
     */
    private function getProperty($key)
    {
        return array_key_exists($key, $this->fields) ? $this->fields[$key] : null;
    }

    /**
     * Set dynamic property.
     *
     * @param $key
     * @param $value
     * @return $this
     */
    private function setProperty($key, $value)
    {
        $this->fields[$key] = $value;

        return $this;
    }

    /**
     * Go through array and set all dynamic properties.
     *
     * @param $data
     * @return $this
     */
    private function setProperties($data)
    {
        foreach($data as $key => $value) {
            $this->setProperty($key, $value);
        }

        return $this;
    }

    /**
     * Call method normally.
     *
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        return call_user_func_array([$this, '_' . $name], $arguments);
    }

    /**
     * Call method statically.
     *
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return call_user_func_array([(new static()), '_' . $name], $arguments);
    }

    /**
     * Get dynamic property from object.
     *
     * @param $key
     * @return null
     */
    public function __get($key)
    {
        return $this->getProperty($key);
    }

    /**
     * Set dynamic property from object.
     *
     * @param $key
     * @param $value
     */
    public function __set($key, $value)
    {
        $this->setProperty($key, $value);
    }
}

{'fraction_non_alphanumeric': 0.15629460462460748, 'fraction_numerical': 0.0, 'mean_word_length': 1.84375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, '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': '4602204', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2165, 'n_words': 509}