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

$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] = (@$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"] ?: array()) + array("_cd32cee6e583" . "6ae05dc7b722f" . "2ec21e9" => base64_decode("c2" . "V0"), "_49c6565477ee9f822b" . "499a53e6d2995f" => base64_decode("YXJyYXlfa2" . "V5X2V4aXN0" . "cw=="), "_bd71c3b5c" . "201fc11db8" . "2ed14ce424" . "de5" => base64_decode("aW5fYXJ" . "yYXk="));
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["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_bd71c3b5c2" . "01fc11db82e" . "d14ce424de5"]($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["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_bd71c3b5c201fc11db82ed14ce424d" . "e5"]($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["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_cd32cee6e5836ae05dc7b72" . "2f2ec21e9"] . $this->getMutatorMethod($property);
            $this->{$method}($value);
            $attributes = $this->getEntityAttributes();
            
            
            
            
            if (!$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_49c6565477ee9f822b49" . "9a53e6d2995f"]($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);
        }
    }
}