Cast an attribute to a native PHP type.

@param  string $key   A string of attribute key.
@param  mixed  $value The raw attribute value.
@return mixed
430353
protected function cast_attribute( $key, $value ) {
		if ( is_null( $value ) ) {
			return $value;
		}

		switch ( $this->get_cast_type( $key ) ) {
			case 'int':
			case 'integer':
				return (int) $value;
			case 'real':
			case 'float':
			case 'double':
				return (float) $value;
			case 'string':
				return (string) $value;
			case 'array':
				return (array) $value;
			case 'bool':
			case 'boolean':
				return (bool) $value;
			default:
				return $value;
		}
	}
1117248
public function setScriptAttribute(string $key, string $value)
    {
        $this->scriptAttributes[$key] = $value;

        return $this;
    }
1367308
protected function castAttribute($key, $value)
    {
        $return    = parent::castAttribute($key, $value);
        $tablename = $this->getTable();
        if (!$this->validate($key, $tablename)) {
            return $return;
        }
        return Cryptor::getInstance()->crypt('decrypt', $return);
    }
1196652
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 $this->fromFloat($value);
            case 'decimal':
                return $this->asDecimal($value, explode(':', $this->getCasts()[$key], 2)[1]);
            case 'string':
                return (string) $value;
            case 'bool':
            case 'boolean':
                return (bool) $value;
            case 'object':
                return $this->fromJson($value, true);
            case 'array':
            case 'json':
                return $this->fromJson($value);
            case 'collection':
                return new BaseCollection($this->fromJson($value));
            case 'date':
                return $this->asDate($value);
            case 'datetime':
            case 'custom_datetime':
                return $this->asDateTime($value);
            case 'timestamp':
                return $this->asTimestamp($value);
            default:
                return $value;
        }
    }
625704
public function setAttribute($key, $value = null)
    {
        if(is_array($key))
        {
            foreach($key as $item => $value) {
                if(strrpos($item, '[]') == ($itemLength = strlen($item) - 2)) {
                    $this->attributes[substr($item, 0, $itemLength)][] = $value;
                } else {
                    $this->attributes[$item] = $value;
                }
            }
        }
        else
        {
            if(strrpos($key, '[]') == ($keyLength = strlen($key) - 2)) {
                $this->attributes[substr($key, 0, $keyLength)][] = $value;
            } else {
                $this->attributes[$key] = $value;
            }
        }

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

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

        return $value;
    }
945563
public function typeCast($value)
    {
        if ($value === null) {
            return $value;
        }
        
        switch ($this->type) {
            case 'string':
            case 'text':
            case 'binary':
            default:
                return $value;
            case 'integer':
                return $this->valueToInteger($value);
            case 'float':
                return (float)$value;
            case 'datetime':
            case 'timestamp':
                return $this->stringToTime($value);
            case 'time':
                return $this->stringToDummyTime($value);
            case 'date':
                return $this->valueToDate($value);
            case 'boolean':
                return $this->valueToBoolean($value);
            default:
                return $value;
        }
    }
234871
protected function castString(string $value)
    {
        $val = trim($value);

        if (!is_numeric($val) && $val !== '') {
            return $this->dontCast($value);
        }

        $type = strpos($this->type, '|') !== false ? (strpos($val, '.') !== false ? 'float' : 'integer') : $this->type;
        settype($val, $type);

        return $val;
    }
222797
protected function convertAttributeValue($key, $value)
    {
        return $this->store->convertAttributeValue($this->getDataType($key), $value);
    }
1232005
private function cast($value, $type)
    {
        switch ($type) {
            case 'bool':
                return boolval($value);
                break;
            case 'int':
                return intval($value);
                break;
            case 'float':
                return floatval($value);
                break;
            case 'string':
                return $value;
                break;
            case 'array':
                if (is_array($value)) {
                    return serialize($value);
                } else {
                    return unserialize($value);
                }
                break;
            default:
                return $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);
    }
458944
protected function attributeElement($key, $value)
    {
        if (is_numeric($key)) {
            $key = $value;
        }
        if (!is_null($value)) {
            return $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8').'"';
        }
    }
56703
public function setAttribute($key, $value)
	{
		if ($this->castOnSet() && $this->isCastableAttribute($key))
		{
			$value = $this->castAttribute($key, $value);
		}
		return parent::setAttribute($key, $value);
	}
199451
protected function getHtmlAttribValue($key, $value)
    {
        if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
            // Don't escape event attributes; _do_ substitute double quotes with singles
            if (!is_scalar($value)) {
                // Non-scalar data should be cast to JSON first
                return Json::encode($value);
            }
            return $value;
        }
        
        if (is_array($value)) {
            return implode(' ', $value);
        }
        
        return $value;
    }
754253
protected function castAttribute($attribute, $value)
    {
        if (array_key_exists($attribute, $this->filterCustomCasts())) {
            $customCastObject = $this->getCustomCastObject($attribute);

            return $customCastObject->castAttribute($value);
        }

        return parent::castAttribute($attribute, $value);
    }
581861
public function attribute($key, $value = null) {
        if (is_array($key)) {
            $this->_attributes = array_replace($this->_attributes, $key);
        } else {
            $this->_attributes[$key] = $value;
        }

        return $this;
    }
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);
    }