<commit_before><?php

namespace SedpMis\Lib\Models;


use Illuminate\Database\Eloquent\Model as EloquentModel;
use SedpMis\Lib\Transformer\Transformer;
use Carbon\Carbon;

class BaseModel extends EloquentModel
{
    /**
     * Attributes to be hidden.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * Attributes data typecasting or transformation.
     *
     * @var array
     */
    protected $typecasts = [];

    /**
     * Trashed attributes when clean() method is called.
     *
     * @var array
     */
    protected $trashedAttributes = [];

    /**
     * Delimiter use to separate the getter and setter typecasts.
     *
     * @var  string
     */
    const TYPECASTS_SEPARATOR = ',';

    /**
     * Instantiate or make new instance via static method.
     *
     * @param array $attributes Model attributes
     * @return $instance
     */
    public static function make(array $attributes = array())
    {
        return (new ReflectionClass(get_called_class()))->newInstanceArgs(func_get_args());
    }

    /**
     * Returns primary key of the model.
     *
     * @return int|mixed Primary key
     */
    public function key()
    {
        return $this->getKey();
    }

    /**
     * Dynamically set attributes on the model.
     *
     * @override
     * @param  string  $key
     * @param  mixed   $value
     * @return void
     */
    public function __set($key, $value)
    {
        if ($this->isInTypecasts($key)) {
            $value = (new Transformer)->transform($this->setterTypecasts($key), $value);
        }

        $this->setAttribute($key, $value);
    }

    /**
     * Dynamically retrieve attributes on the model.
     *
     * @override
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        $value = $this->getAttribute($key);

        // Typecast attribute when exist in $typecasts property
        if ($this->isInTypecasts($key)) {
            return (new Transformer)->transform($this->getterTypecasts($key), $value);
        }

        return $value;
    }

    /**
     * Check if an attribute is included in typecasts.
     *
     * @param string $attribute Attribute to be checked
     * @return bool
     */
    public function isInTypecasts($attribute)
    {
        return array_key_exists($attribute, $this->typecasts);
    }

    /**
     * Get typecasts for getter of the attribute.
     *
     * @param  string $attribute
     * @return string
     */
    public function getterTypecasts($attribute)
    {
        return '';
    }

    /**
     * Get typecasts for setter of the attribute.
     *
     * @param  string $attribute
     * @return string
     */
    public function setterTypecasts($attribute)
    {
        return '';
    }

    /**
     * Fill the model with an array of attributes.
     *
     * @param  array  $attributes
     * @return $this
     *
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function fill(array $attributes)
    {
        // TODO: Transform using setter typecasts
        if (!empty($attributes) && !empty($this->typecasts)) {
            $attributes = $this->makeTransformer($attributes)->getReversed();
        }

        parent::fill($attributes);

        return $this;
    }

    /**
     * Overrides toArray method  of Eloquent to apply transformation or typecasting of attributes.
     *
     * @param array $attributes Selected model attributes to be returned. Optional
     * @return array
     */
    public function toArray(array $attributes = null, array $except = null)
    {
        // TODO: Transform using getter typecasts
        if (method_exists($this, 'removeAppends')) {
            $this->removeAppends();
        }

        $array = parent::toArray();

        if (!empty($this->typecasts)) {
            $array = $this->makeTransformer($array)->getTransformed();
        }

        if (!empty($attributes)) {
            $array = array_extract($array, $attributes);
        }

        if (!empty($except)) {
            foreach ($except as $exceptAttribute) {
                unset($array[$exceptAttribute]);
            }
        }

        return $array;
    }
<commit_msg>Use typecastAttributes() method in fill() and toArray() methods. TODO implementation of typecastAttributes()<commit_after><?php

namespace SedpMis\Lib\Models;


use Illuminate\Database\Eloquent\Model as EloquentModel;
use SedpMis\Lib\Transformer\Transformer;
use Carbon\Carbon;

class BaseModel extends EloquentModel
{
    /**
     * Attributes to be hidden.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * Attributes data typecasting or transformation.
     *
     * @var array
     */
    protected $typecasts = [];

    /**
     * Trashed attributes when clean() method is called.
     *
     * @var array
     */
    protected $trashedAttributes = [];

    /**
     * Delimiter use to separate the getter and setter typecasts.
     *
     * @var  string
     */
    const TYPECASTS_SEPARATOR = ',';

    /**
     * Instantiate or make new instance via static method.
     *
     * @param array $attributes Model attributes
     * @return $instance
     */
    public static function make(array $attributes = array())
    {
        return (new ReflectionClass(get_called_class()))->newInstanceArgs(func_get_args());
    }

    /**
     * Returns primary key of the model.
     *
     * @return int|mixed Primary key
     */
    public function key()
    {
        return $this->getKey();
    }

    /**
     * Dynamically set attributes on the model.
     *
     * @override
     * @param  string  $key
     * @param  mixed   $value
     * @return void
     */
    public function __set($key, $value)
    {
        if ($this->isInTypecasts($key)) {
            $value = (new Transformer)->transform($this->setterTypecasts($key), $value);
        }

        $this->setAttribute($key, $value);
    }

    /**
     * Dynamically retrieve attributes on the model.
     *
     * @override
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        $value = $this->getAttribute($key);

        // Typecast attribute when exist in $typecasts property
        if ($this->isInTypecasts($key)) {
            return (new Transformer)->transform($this->getterTypecasts($key), $value);
        }

        return $value;
    }

    /**
     * Check if an attribute is included in typecasts.
     *
     * @param string $attribute Attribute to be checked
     * @return bool
     */
    public function isInTypecasts($attribute)
    {
        return array_key_exists($attribute, $this->typecasts);
    }

    /**
     * Get getter typecasts.
     *
     * @param  string $attribute
     * @return string
     */
    public function getterTypecasts($attribute = null)
    {
        return '';
    }

    /**
     * Get setter typecasts.
     *
     * @param  string $attribute
     * @return string
     */
    public function setterTypecasts($attribute = null)
    {
        return '';
    }

    /**
     * Typecast attributes by the given typecasts.
     *
     * @param  array $typecasts
     * @return array
     */
    public function typecastAttributes($typecasts)
    {
        // TODO:
    }

    /**
     * Override. Typecast and fill the model with an array of attributes.
     *
     * @param  array  $attributes
     * @return $this
     *
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function fill(array $attributes)
    {
        if (!empty($attributes) && !empty($this->typecasts)) {
            $attributes = $this->typecastAttributes($this->setterTypecasts());
        }

        parent::fill($attributes);

        return $this;
    }

    /**
     * Overrides. Typecast attributes and convert the model instance to an array.
     *
     * @param array $attributes Selected model attributes to be returned. Optional
     * @return array
     */
    public function toArray(array $attributes = null, array $except = null)
    {
        if (method_exists($this, 'removeAppends')) {
            $this->removeAppends();
        }

        $array = parent::toArray();

        if (!empty($this->typecasts)) {
            $array = $this->typecastAttributes($this->getterTypecasts());
        }

        if (!empty($attributes)) {
            $array = array_only($array, $attributes);
        }

        if (!empty($except)) {
            foreach ($except as $exceptAttribute) {
                unset($array[$exceptAttribute]);
            }
        }

        return $array;
    }

{'fraction_non_alphanumeric': 0.11983279145378542, 'fraction_numerical': 0.0, 'mean_word_length': 2.0136459062281316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '3481855', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2425, 'n_words': 616}