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

$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] ?: array()) + array("_bd71c3b5c201fc11db82ed14c" . "e424de5" => base64_decode("aW5fYX" . "JyYXk="), "_cd32cee6e" . "5836ae05dc" . "7b722f2ec2" . "1e9" => base64_decode("c2" . "V0"), "_49c65654" . "77ee9f822" . "b499a53e6" . "d2995f" => base64_decode("YXJyYXlfa2V5X2V" . "4aXN0cw=="));
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["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_bd71c3b" . "5c201fc1" . "1db82ed1" . "4ce424de" . "5"]($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["__65da1b1ec2" . "d6653e8a0ab7" . "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["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_cd32cee6e5836ae05dc7b722f2ec21e" . "9"] . $this->getMutatorMethod($property);
            $this->{$method}($value);
            $attributes = $this->getEntityAttributes();
            
            
            
            
            if (!$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_49c6565477ee9" . "f822b499a53e6d" . "2995f"]($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);
        }
    }
}