Cast an attribute to a PHP variable type.

@param  string $key
@param  mixed $value
@throws EloquentTypecastException
@return  mixed
546210
protected function castAttribute($key, $value)
	{
		$type = $this->cast[$key];

		try {
			if ( is_null($value) ) {
				return null;
			}
			
			if ( settype($value, $type) ) {
				return $value;
			}
			throw new EloquentTypecastException("Value could not be cast to type \"$type\"", 1);
		} catch (\Exception $e) {
			throw new EloquentTypecastException("Value could not be cast to type \"$type\"", 1);
		}
	}
235215
public function setAttribute($key, $value)
    {
        $key = snake_case($key);
        $value = empty($value) ? null : $value;
        $value = is_string($value)? trim($value) : $value;
        return parent::setAttribute($key, $value);
    }
222797
protected function convertAttributeValue($key, $value)
    {
        return $this->store->convertAttributeValue($this->getDataType($key), $value);
    }
743521
protected function castToDb($value, $column)
    {
        if ($this->hasCast($column) && !$this->isIgnoreCast($value)) {
            $value = $this->toDbType($value, $this->casts[$column]);
        }

        return $value;
    }
202530
public function phpTypecastComposite($value)
    {
        if (is_string($value)) {
            $value = $this->getCompositeParser()->parse($value);
        }
        if (is_array($value)) {
            $result = [];
            $fields = array_keys($this->columns);
            foreach ($value as $i => $item) {
                $field = is_int($i) ? $fields[$i] : $i;
                if (isset($this->columns[$field])) {
                    $result[$field] = $this->columns[$field]->phpTypecast($item);
                }
            }

            return $this->createCompositeObject($result);
        } elseif (!$value instanceof $this->phpType) {
            return null;
        }

        return $value;
    }
610032
public function setAttribute($key, $value)
    {
        // Verificar alias para _id
        $key = ($key == 'id') ? '_id' : $key;

        // First we will check for the presence of a mutator for the set operation
        // which simply lets the developers tweak the attribute as it is set on
        // the model, such as "json_encoding" an listing of data for storage.
        if ($this->hasSetMutator($key)) {
            $this->changed[$key] = true;

            return $this->setMutateAttribute($key, $value);
        }

        // Verificar se foi definido o cast do key
        if ($this->hasCast($key)) {
            $value = $this->setCast($key, $value);
        }

        return $this->setAttributeToArray($key, $value);
    }
785588
public function setAttribute($key, $value)
    {
        if ($this->isEnumAttribute($key)) {
            $enumClass = $this->getEnumClass($key);
            if (! $value instanceof $enumClass) {
                $value = new $enumClass($value);
            }

            $this->attributes[$key] = $value->value();

            return $this;
        }

        parent::setAttribute($key, $value);
    }
676851
public function cast($value)
    {
        if (isset($this->traversable) && !is_a($this->traversable, Traversable::class, true)) {
            return $this->dontCast($value, "{$this->traversable} is not Traversable");
        }

        $fn = 'cast' . ucfirst(gettype($value));
        $result = method_exists($this, $fn) ? $this->$fn($value) : (array)$value;

        if (!is_iterable($result)) {
            return $result;
        }

        //Commented out as there's no HandlerRepositoryInterface
        // if (isset($this->subtype)) {
        //     $result = $this->typecast instanceof HandlerRepositoryInterface
        //         ? $this->castEachWithHandler($result, $this->typecast)
        //         : $this->castEach($result);
        // }

        if (isset($this->subtype)) {
            $result = $this->castEach($result);
        }

        if (is_array($result) && isset($this->traversable)) {
            $class = $this->traversable;
            $result = new $class($result);
        }

        return $result;
    }
1205617
protected function needCasting($key, $value): bool
    {
        if (is_null($value) || is_scalar($value) || is_object($value)) {
            return false;
        }
        return true;
    }
546703
protected function castAttribute($key, $value)
    {
        if (is_null($value)) {
            return $value;
        }

        switch ($this->getCastType($key)) {
            case 'int':
            case 'integer':
                return (int)$value;
            case 'real':
            case 'float':
            case 'double':
                return (float)$value;
            case 'string':
                return (string)$value;
            case 'bool':
            case 'boolean':
                return filter_var($value, FILTER_VALIDATE_BOOLEAN);
            case 'object':
                return is_array($value)
                    ? json_decode(json_encode($value))
                    : json_decode($value);
            case 'array':
            case 'json':
                return json_decode($value, true);
            case 'date':
            case 'datetime':
                return $this->asDateTime($value);
            case 'timestamp':
                return $this->asTimeStamp($value);
            default:
                return $value;
        }
    }
299353
protected function mutateAttribute($key, $value)
	{
		if ($this->isCastableAttribute($key))
		{
			return $this->castAttribute($key, $value);
		}

		return parent::mutateAttribute($key, $value);
	}
112413
public static function cast($value)
    {
        $currentClass = get_called_class();
        if (!is_object($value) || get_class($value) !== $currentClass) {
            $value = new $currentClass($value);
        }
        return $value;
    }
1154447
protected function castToPropertyValue($value, $columnName)
    {
        $propertyMap = $this->getPropertyMapping();
        $castFunction = $propertyMap[$columnName]['cast'];

        return $castFunction($value);
    }
201991
public function castValue($field, $value, $cast = true)
    {
        if (! array_key_exists($field, $this->casts) || ! $cast)
            // no cast configured for said field. Nothing done.
            return $value;

        $types = [
            "boolean", "bool", "integer", "int", "float", "double",
            "string", "array", "object", "null"
        ];

        // validate type cast is valid
        $type = $this->casts[$field];
        if (! in_array($type, $types)) {
            throw new RuntimeException("Cast of field '" . $field . "' to type '" . $type . "' not possible. Please define only scalar type casts.");
        }

        $output = $value;
        $result = settype($output, $type);

        if ($result === true)
            return $output;

        return $value;
    }
1367713
public function withAttribute(string $key, $value): ElementInterface
    {
        if ($key === 'value' && is_array($value)) {
            $this->withData($value);
        }

        parent::withAttribute($key, $value);

        return $this;
    }
137778
protected function setCast($key, $value)
    {
        if (is_null($value)) {
            return $value;
        }

        $type   = $this->getCastType($key);
        $method = 'setCast' . Str::studly($type) . 'Type';

        if (method_exists($this, $method)) {
            return $this->$method($key, $value);
        }

        return $value;
    }
241711
protected function castAttributeAsJson($key, $value)
    {
        $value = $this->asJson($value);

        if ($value === false) {
            throw JsonEncodingException::forAttribute(
                $this, $key, json_last_error_msg()
            );
        }

        return $value;
    }