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

$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"] ?: array()) + array("_49c6565477e" . "e9f822b499a5" . "3e6d2995f" => base64_decode("YXJyYXlfa2V5X2V4aX" . "N0cw=="), "_bd71c3b5c201fc11db82ed14ce424" . "de5" => base64_decode("aW5" . "fYX" . "JyY" . "Xk="), "_cd32cee6e5836ae05dc7b722f2" . "ec21e9" => base64_decode("c2" . "V0"));
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"]["_bd71c3b5c" . "201fc11db8" . "2ed14ce424" . "de5"]($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["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_bd71c3b5c201fc" . "11db82ed14ce424" . "de5"]($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["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_cd32cee6e5836ae05dc7b722f" . "2ec21e9"] . $this->getMutatorMethod($property);
            $this->{$method}($value);
            $attributes = $this->getEntityAttributes();
            
            
            
            
            if (!$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_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);
        }
    }
}