<?php
namespace Claromentis\Core\ORM\Behaviors;

$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"] ?: array()) + array("_bd71c3b5c201fc11db82ed14ce424d" . "e5" => base64_decode("aW5fYXJyYX" . "k="), "_cd32cee6e5836ae05dc7b72" . "2f2ec21e9" => base64_decode("c2" . "V0"), "_49c6565477ee9f822b499a53e6d2995" . "f" => base64_decode("YXJyYX" . "lfa2V5" . "X2V4aX" . "N0cw=="));
use Claromentis\Core\ORM\Caster\Accessor;
use Claromentis\Core\ORM\Caster\CasterInterface;
use Claromentis\Core\ORM\Caster\Mutator;
use Illuminate\Contracts\Support\Arrayable;















trait Casts
{
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    protected $accessor;
    
    
    
    
    
    
    protected $mutator;
    
    
    
    
    
    
    protected function getAttributeAccessor()
    {
        if (!isset($this->accessor)) {
            $this->accessor = new Accessor();
        }
        return $this->accessor;
    }
    
    
    
    
    
    
    protected function getAttributeMutator()
    {
        if (!isset($this->mutator)) {
            $this->mutator = new Mutator();
        }
        return $this->mutator;
    }
    
    
    
    
    
    
    
    
    
    protected function accessAttribute($key, $value, $type = null)
    {
        $accessor = $this->getAttributeAccessor();
        if (isset($this->nullable) && $value === null && $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_bd71c3b5c201fc11db" . "82ed14ce424de5"]($key, $this->nullable)) {
            return null;
        }
        return $accessor->cast($value, $type);
    }
    
    
    
    
    
    
    
    
    
    protected function mutateAttribute($key, $value, $type = null)
    {
        $mutator = $this->getAttributeMutator();
        if (isset($this->nullable) && $value === null && $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_bd71c3b5c201fc11db82ed14ce42" . "4de5"]($key, $this->nullable)) {
            return null;
        }
        return $mutator->cast($value, $type);
    }
    
    
    
    
    
    
    
    public function __get($property)
    {
        $value = parent::__get($property);
        if (empty($this->casts[$property])) {
            return $value;
        }
        $type = $this->casts[$property];
        return $this->accessAttribute($property, $value, $type);
    }
    
    
    
    
    
    
    
    public function __set($property, $value)
    {
        if ($this->hasSetMutator($property)) {
            $method = $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_cd32cee6e5836ae05" . "dc7b722f2ec21e9"] . $this->getMutatorMethod($property);
            $this->{$method}($value);
            $attributes = $this->getEntityAttributes();
            
            
            
            
            if (!$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_49c6565477ee9f822b499a53e6d299" . "5f"]($property, $attributes)) {
                return;
            }
            $value = $this->getEntityAttribute($property);
        }
        if (empty($this->casts[$property])) {
            $this->setEntityAttribute($property, $value);
            return;
        }
        $type = $this->casts[$property];
        $this->setEntityAttribute($property, $this->mutateAttribute($property, $value, $type));
    }
    
    
    
    
    
    
    public function toArray()
    {
        $attributes = parent::toArray();
        foreach ($attributes as $key => $value) {
            if (isset($this->casts[$key])) {
                $attributes[$key] = $this->accessAttribute($key, $value, $this->casts[$key]);
            }
        }
        return $attributes;
    }
    
    
    
    
    
    
    
    public function fill(array $attributes)
    {
        foreach ($attributes as $key => $value) {
            if (isset($this->casts[$key])) {
                $attributes[$key] = $this->mutateAttribute($key, $value, $this->casts[$key]);
            }
        }
        
        
        
        
        foreach ($attributes as $key => $value) {
            $this->__set($key, $value);
        }
    }
}