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

$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"] ?: array()) + array("_49c6565477ee9f822b499a53" . "e6d2995f" => base64_decode("YXJyYXlfa" . "2V5X2V4aX" . "N0cw=="), "_cd32cee6e5836ae05dc7b722" . "f2ec21e9" => base64_decode("c" . "2" . "V" . "0"), "_bd71c3b5c201" . "fc11db82ed14c" . "e424de5" => base64_decode("aW5fY" . "XJyYX" . "k="));
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["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_bd71c3b5c201fc11db82ed14" . "ce424de5"]($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["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_bd71c3b5c201fc11db82ed14ce424de" . "5"]($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["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_cd32cee6e5836ae05dc7b722" . "f2ec21e9"] . $this->getMutatorMethod($property);
            $this->{$method}($value);
            $attributes = $this->getEntityAttributes();
            
            
            
            
            if (!$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_49c6565477ee9f822b499" . "a53e6d2995f"]($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);
        }
    }
}