Cast an attribute to a native PHP type.

@param  string $key
@param  mixed  $value
@return mixed
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;
        }
    }
187225
protected function castToType($value, string $type)
    {
        $platform = $this->connection->getDatabasePlatform();

        return Type::getType($type)->convertToDatabaseValue($value, $platform);
    }
1346198
public function setAttribute($key, $value)
    {
        if ($key == 'type') {
            switch ($value) {
            case 'image': self::$rules['value'] = 'image';
            break;
            case 'file': self::$rules['value'] = 'file';
            break;
            case 'video-encoder': self::$rules['value'] = 'video';
            break;
        }
        }

        // Continue
        return parent::setAttribute($key, $value);
    }
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;
        }
    }
45062
public static function GetNativeType( $value )
   {

      if ( \is_string( $value ) )
      {
         return Type::PHP_STRING;
      }

      if ( \is_int( $value ) )
      {
         return Type::PHP_INTEGER;
      }

      if ( \is_bool( $value ) )
      {
         return Type::PHP_BOOLEAN;
      }

      if ( \is_float( $value ) )
      {
         return Type::PHP_FLOAT;
      }

      if ( \is_array( $value ) )
      {
         return Type::PHP_ARRAY;
      }

      return false;

   }
361225
protected function castAttribute($key, $value)
    {
        if ($value === null) {
            return $value;
        }

        if ($this->getCastType($key) === 'serialize') {
            return $this->fromSerialize($value);
        }

        return parent::castAttribute($key, $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);
    }
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;
    }
324076
public function setAttribute($key, $value)
    {
        $key = snake_case($key);
        return parent::setAttribute($key, $value);
    }
1344185
private function castToType(string $value, string $type)
    {
        $casts = [
            'integer' => 'intval',
            'number' => 'floatval',
            'float' => 'floatval',
            'boolean' => 'boolval',
        ];

        // First, we handle booleans. We can't use a regular cast,
        //because PHP considers string 'false' as true.
        if ($value == 'false' && $type == 'boolean') {
            return false;
        }

        if (isset($casts[$type])) {
            return $casts[$type]($value);
        }

        return $value;
    }
1110799
public function setAttribute(string $key, string $value)
    {
        if ($this->valid($key, $value)) {
            $value = encrypt($value);
        }

        return parent::setAttribute($key, $value);
    }
969343
public function cast($value)
    {
        $fn = 'cast' . ucfirst(gettype($value));
        return method_exists($this, $fn) ? $this->$fn($value) : $this->dontCast($value);
    }
470804
public function setAttribute($key, $value)
    {
        list($attribute, $locale) = $this->getAttributeAndLocale($key);

        if ($this->isTranslationAttribute($attribute)) {
            $this->getTranslationOrNew($locale)->$attribute = $value;
        } else {
            return parent::setAttribute($key, $value);
        }

        return $this;
    }
40062
protected function attribute($key, $value)
    {
        if (is_numeric($key)) {
            $key = $value;
        }

        if (!is_null($value)) {
            return $key.'="'.$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;
        }
    }
104923
protected function castToEnum($key, $value): ?Enum
    {
        $enum = $this->enumCasts[$key];

        if ($value === null || $value instanceOf Enum) {
            return $value;
        } else {
            return $enum::getInstance($value);
        }
    }
484764
protected function typecastValue($value, $type)
    {
        if (is_scalar($type)) {
            if (is_object($value) && method_exists($value, '__toString')) {
                $value = $value->__toString();
            }

            switch ($type) {
                case self::TYPE_INTEGER:
                    return (int)$value;
                case self::TYPE_FLOAT:
                    return (float)$value;
                case self::TYPE_BOOLEAN:
                    return (bool)$value;
                case self::TYPE_STRING:
                    return (string)$value;
                case self::TYPE_JSON:
                    return is_string($value) ? $value : json_encode($value);
                case self::TYPE_IMPLODE:
                    return is_string($value) ? $value : (string)implode($this->delimiter, $value);
                case self::TYPE_SERIALIZE:
                    return is_string($value) ? $value : serialize($value);
                default:
                    throw new InvalidParamException("Unsupported type '{$type}'");
            }
        }

        return call_user_func($type, $value);
    }