Casts the given value to the specified type.
@param mixed $value value to be type-casted.
@param string|callable $type type name or typecast callable.
@return mixed typecast result.
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);
    }
510945
public static function typeCast($value, $type) {
		switch ($type) {
			case 'int':
				$value = (int)$value;
				break;
			case 'float':
				$value = (float)$value;
				break;
			case 'double':
				$value = (double)$value;
				break;
			case 'array':
				$value = (array)$value;
				break;
			case 'bool':
				$value = (bool)$value;
				break;
			case 'string':
				$value = (string)$value;
				break;
			default:
				return null;
		}
		return $value;
	}
387919
public function cast($value)
    {
        if (!isset($this->typecast)) {
            throw new LogicException("Type cast for multiple handler not set");
        }

        $type = $this->typeGuess->guess($value, $this->types);

        return isset($type) ? $this->typecast->to($type)->cast($value) : $this->dontCast($value);
    }
998198
public static function setType($value, $type, $nullable = TRUE)
	{
		if (!$nullable || NULL !== $value) {
			settype($value, $type);
		}

		return $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;
    }
1275520
protected function castArrayValue($value)
    {
        if (!is_object($value)) {
            return $this->key($value);
        }

        try {
            if ($key = $this->objectCategorizer->key($value)) {
                return $this->hashIfToLong($key);
            }
        } catch (Exception $e) {
        }

        return $this->hashIfToLong($this->objectToKey($value));
    }
1231895
protected function phpTypecastValue($value)
    {
        if ($value === null) {
            return null;
        }

        switch ($this->type) {
            case Schema::TYPE_BOOLEAN:
                switch (strtolower($value)) {
                    case 't':
                    case 'true':
                        return true;
                    case 'f':
                    case 'false':
                        return false;
                }
                return (bool) $value;
            case Schema::TYPE_BIT:
                return bindec($value);
            case Schema::TYPE_BINARY:
                return is_string($value) && strncmp($value, '\\x', 2) === 0 ? pack('H*', substr($value, 2)) : $value;
            case Schema::TYPE_JSON:
                return json_decode($value, true);
            case Schema::TYPE_TIMESTAMP:
            case Schema::TYPE_TIME:
            case Schema::TYPE_DATE:
            case Schema::TYPE_DATETIME:
                return new \DateTime($value);
            case Schema::TYPE_COMPOSITE:
                return $this->phpTypecastComposite($value);
        }

        return $this->typecast($value);
    }
221781
protected function prepareValue($value)
    {
        if ($this->hasMutator()) {
            return call_user_func($this->getMutator(), $value);
        } elseif ($value && $this->isElementOfDate() && $this->isDateCastable()) {
            $value = $this->castValueAsDateTime($value);
            if ($this instanceof Timestamp) {
                $value = $value->getTimestamp();
            }

            return $value;
        }

        if (! is_null($value)) {
            if ($this->isJsonCastable()) {
                return $this->castValueAsJson($value);
            } elseif ($this->isCommaCastable()) {
                return $this->castValueAsCommaSeparated($value);
            }
        }

        return $value;
    }
1080940
private function parserParamType(string $type, $value)
    {
        switch ($type) {
            case 'int':
                $value = (int)$value;
                break;
            case 'string':
                $value = (string)$value;
                break;
            case 'bool':
                $value = (bool)$value;
                break;
            case 'float':
                $value = (float)$value;
                break;
            case 'double':
                $value = (double)$value;
                break;
        }

        return $value;
    }
652041
public static function trasferTypes($type, $value)
    {
        if ($value === null) {
            $value = null;
        } elseif ($type === Types::INT || $type === Types::NUMBER) {
            $value = (int)$value;
        } elseif ($type === Types::STRING) {
            $value = null === $value ? null : (string)$value;
        } elseif ($type === Types::BOOLEAN) {
            $value = (bool)$value;
        } elseif ($type === Types::FLOAT) {
            $value = (float)$value;
        }

        return $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;
    }
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;
    }
1031501
public function handleTypeConversion($type, $value)
    {
        switch ($type):
            case PropertyTypeEnum::STRING:
                $value = (string) $value;
                break;
            case PropertyTypeEnum::BOOLEAN:
                $value = (boolean) $value;
                break;
            case PropertyTypeEnum::INTEGER:
                $value = (integer) $value;
                break;
            case PropertyTypeEnum::DATETIME;
                $value = new \DateTime($value);
                break;
        endswitch;

        return $value;
    }
786262
public static function fromValue($value) : Type
    {
        switch (gettype($value)) {
            case 'string':
                return Text::long();

            case 'integer':
                return Integer::big();

            case 'double':
                return new Decimal(30, 15);

            case 'boolean':
                return new Boolean();

            case $value instanceof \DateTimeInterface:
                return new DateTime();

            default:
                throw InvalidArgumentException::format(
                        'Invalid call to %s: unknown value type \'%s\' given',
                        __METHOD__, gettype($value)
                );
        }
    }
103100
protected function toPhpType($value, $type)
    {
        switch ($type) {
            case 'int':
                return (int) $value;

            case 'string':
                return (string) $value;

            case 'datetime':
                return $value === '0000-00-00 00:00:00' ? '' : $value;

            case 'date':
                return $value === '0000-00-00' ? '' : $value;

            case 'bool':
                return (bool) $value;

            case 'array':
            case 'json':
                // Ignore default array value
                return is_array($value) ? $value : $this->cacheJsonDecode($value, true);

            case 'float':
                return (float) $value;

            default:
                throw new InvalidArgumentException('Unsupported cast type: ' . $type);
        }
    }
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;
    }
1080250
protected function castToInt($value, bool $nullable = true)
    {
        if (null === $value && $nullable) {
            return null;
        }

        return (int) $value;
    }