Force a value into the appropriate php type based on its SQL type.

@param mixed $value The value to force.
@param string $type The sqlite type name.
@return mixed Returns $value cast to the appropriate type.
189412
protected function forceType($value, $type) {
        $type = strtolower($type);

        if ($type === 'null') {
            return null;
        } elseif ($type === 'boolean') {
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
        } elseif (in_array($type, ['int', 'integer', 'tinyint', 'smallint',
            'mediumint', 'bigint', 'unsigned big int', 'int2', 'int8', 'boolean'])) {
            return filter_var($value, FILTER_VALIDATE_INT);
        } elseif (in_array($type, ['real', 'double', 'double precision', 'float',
            'numeric', 'number', 'decimal(10,5)'])) {
            return filter_var($value, FILTER_VALIDATE_FLOAT);
        } else {
            return (string)$value;
        }
    }
696725
private function isType(string $type, $value)
    {
        switch ($type) {
            case 'bool':
                return is_bool($value);

            case 'int':
                return is_int($value);

            case 'float':
                return is_float($value) || is_int($value);

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

            case 'array':
                return is_array($value);

            default:
                throw new InvalidParamsException('Type match error');
        }
    }
564185
private function formatParameter($value, $type)
    {
        if (is_string($type) && Type::hasType($type)) {
            return Type::getType($type)->convertToDatabaseValue(
                $value,
                $this->connection->getDatabasePlatform()
            );
        }

        return $this->parameterToString($value);
    }
136131
protected function convertType($value, $type){
		$dateFormat = $this->connection['result']['formatDate'];
		if($value === null)
			return null;
		switch($type){
			case Manager::TEXT:
				return (string) $value;

			case Manager::INT:
				return (int) $value;

			case Manager::FLOAT:
				return (float) $value;

			case Manager::BOOL:
				return ((bool) $value) && $value !== 'f' && $value !== 'F';

			case Manager::BINARY:
				return $this->connection->getDriver()->unescape($value, $type);

			case Manager::DATETIME:
				if((int) $value === 0)
					return null;
				elseif(!$dateFormat)
					return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
				elseif($dateFormat == 'U')
					return is_numeric($value) ? (int) $value : strtotime($value);
				elseif(is_numeric($value))
					return date($dateFormat, $value);
				else{
					$d = new DateTime($value);
					return $d->format($dateFormat);
				}

			default:
				return $value;
		}
	}
333065
public static function castFromString($type, $value, $php_type)
    {
        //Here should maybe handle locale specific tz overrides in the future.
        $timezone = null;

        switch ($type) {

            case self::PROPERTY_TYPE_INT:
                return (int) $value;

            case self::PROPERTY_TYPE_FLOAT:
                return (float) $value;

            case self::PROPERTY_TYPE_BOOLEAN:
                return in_array(strtolower($value), ['true', '1', 'yes'], true);

            /** @noinspection PhpMissingBreakStatementInspection */
            case self::PROPERTY_TYPE_TIMESTAMP:
                $timezone = new \DateTimeZone('UTC');

                // no break
            case self::PROPERTY_TYPE_DATE:
                if (preg_match('/Date\\((?<timestamp>[0-9\\+\\.]+)\\)/', $value, $matches)) { //to catch stupid .net date serialisation
                    $value = $matches['timestamp'];
                }

                return new \DateTime($value, $timezone);

            case self::PROPERTY_TYPE_OBJECT:
                $php_type = sprintf('\\XeroPHP\\Models\\%s', $php_type);
                /** @var self $instance */
                $instance = new $php_type();
                $instance->fromStringArray($value);

                return $instance;

            default:
                if (is_scalar($value)) {
                    return (string) $value;
                }

                return (object) $value;
        }
    }
345274
protected function process($value, string $type)
    {
        if ($type === 'integer') {
            return (int) $value;
        }

        if ($type === 'float') {
            return (float) $value;
        }

        if ($type === 'boolean') {
            return (bool) $value;
        }

        return (string) $value;
    }
577457
protected function detectType($value): int
    {
        switch (gettype($value)) {
            case 'boolean':
                return \PDO::PARAM_BOOL;
            case 'integer':
                return \PDO::PARAM_INT;
            case 'NULL':
                return \PDO::PARAM_NULL;
            default:
                return \PDO::PARAM_STR;
        }
    }
343337
protected function getDataTypeByValue($value)
    {
        if(null === $value)
        {
            return \PDO::PARAM_NULL;
        }
        if(is_bool($value))
        {
            return \PDO::PARAM_BOOL;
        }
        if(is_int($value))
        {
            return \PDO::PARAM_INT;
        }
        return \PDO::PARAM_STR;
    }
1112246
protected function inferParamType($value) : int
    {
        if (is_bool($value) || is_int($value)) {
            return  \PDO::PARAM_INT;
        }

        return  \PDO::PARAM_STR;
    }
92562
protected function castValueForUpdate($value)
    {
        switch ($this->type) {
            case 'string':
                $value = [(string)$value];
                break;
            case 'list':
            case 'set':
                $value = (array)$value;
                break;
            case 'zset':
                $casted = [];
                foreach ($value as $k => $v) {
                    $casted[] = $v;
                    $casted[] = $k;
                }
                $value = $casted;
                break;
            case 'hash':
                $casted = [];
                foreach ($value as $k => $v) {
                    $casted[] = $k;
                    $casted[] = $v;
                }
                $value = $casted;
                break;
            default:
                break;
        }

        return $value;
    }
1207332
protected function getType($param, $value, $type = null)
		{
			// attempt to get the type
			$type = method_exists($param, 'getType')
				? $param->getType()
				: $type
					? $type
					: gettype($value);

			// coerce type to something javascript will understand
			if($type == 'double' || $type == 'float' || $type == 'integer' || $type == 'int')
			{
				$type = 'number';
			}
			else if($type == 'bool')
			{
				$type = 'boolean';
			}
			else if($type == 'NULL')
			{
				$type = 'null';
			}

			// return
			return $type;
		}
1153308
protected function castParameter($value, $type = null) {
		//check null value
		if (is_null($value))
			return 'NULL';
		elseif (is_null($type)) {
			//obtain default type handler
			$typeHandler = $this->getDefaultTypeHandler($value);
			if (is_null($typeHandler))
				return 'NULL';
			if (is_array($value))
				return $this->castArray($value, $typeHandler, '_');
		}
		else {
			//cast value to the specified type
			$typeHandler = $this->typeManager->getTypeHandler($type);
				
			if ($typeHandler === false)
				throw new \RuntimeException("No type handler found for type '$type'");
				
			if (is_array($value))
				return $this->castArray($value, $typeHandler, '_');
			
			//check if returned value is null
			$value = $typeHandler->castParameter($value);
			if (is_null($value))
				return 'NULL';
		}
	
		//get parameter expression
		$value = $typeHandler->setParameter($value);
	
		//check null value
		if (is_null($value))
			return 'NULL';
		//cast to string
		if (!is_string($value))
			$value = strval($value);
		return $value;
	}
1291720
private function inferType($value)
    {
        if (is_integer($value)) {
            return \PDO::PARAM_INT;
        }

        if (is_bool($value)) {
            return \PDO::PARAM_BOOL;
        }

        if (is_array($value)) {
            return is_integer(current($value))
                ? Connection::PARAM_INT_ARRAY
                : Connection::PARAM_STR_ARRAY;
        }

        return \PDO::PARAM_STR;
    }
823176
public static function to_type($value, string $type='', bool $flatten=false) {
		ref\cast::to_type($value, $type, $flatten);
		return $value;
	}
326074
protected function castValue($value, $type)
    {
        if (isset($this->scalarMap[$type])) {
            $cast = $this->castToScalar($value, $this->scalarMap[$type]);

            return $cast == $value ? $cast : $value;
        }

        if (is_null($value)) {
            return null;
        }

        if (!is_array($value) && $type === 'array') {
            return (array) $value;
        }

        if (!is_object($value) && $type === 'object') {
            return (object) $value;
        }

        return $this->castToClass($value, $this->resolveNamespace($type));
    }
1311103
protected function getTypeSwapValue($type, $value)
    {
        switch (strtolower($type)) {
            case 'int':
            case 'integer':
                return (int) $value;
            case 'real':
            case 'float':
            case 'double':
                return (float) $value;
            case 'string':
                return (string) $value;
            default:
                return $value;
        }
    }
29906
protected function convert($value, int $type)
    {
        switch ($type) {
            case FieldsInterface::TYPE_INT:
                $result = (int)$value;
                break;
            case FieldsInterface::TYPE_FLOAT:
                $result = (float)$value;
                break;
            case FieldsInterface::TYPE_BOOL:
                $result = is_numeric($value) ? (bool)$value : $value === 't';
                break;
            case FieldsInterface::TYPE_DATE:
                $result = strtotime($value);
                break;
            case FieldsInterface::TYPE_UNIXTIME:
                $result = new \DateTime();
                $result->setTimestamp((int)$value);
                break;
            case FieldsInterface::TYPE_STRING:
                $result = $value ?? '';
                break;
            case FieldsInterface::TYPE_ARRAY:
            default:
                $result = $value;
        }

        return $result;
    }