Cast the values to PHP types.

@param $type
@param $value
@param $php_type
@return bool|\DateTimeInterface|float|int|string
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;
        }
    }
1243493
private static function castTo($val, $type) {
		
		$app = Application::getInstance();
		
		switch ($type) {
				
			case 'string':
			case 'text':
				$val = (string)$val;
				break;
		
			case 'int':
			case 'integer':
				$val = (int)$val;
				break;
		
			case 'bool':
			case 'boolean':
				$val = (bool)$val;
				break;
				
			// creates a DateTime object from datepicker
			case 'date':
				if ($val) {
                    if ((defined('PAIR_FORM_DATE_FORMAT') and self::usingCustomDatepicker())) {
                        $format = PAIR_FORM_DATE_FORMAT;
                    } else {
                        $format = 'Y-m-d';
                    }
					$val = \DateTime::createFromFormat('!' . $format, $val, $app->currentUser->getDateTimeZone());
                    // DateTime is not set
					if (FALSE === $val) $val = NULL;
				} else {
					$val = NULL;
				}
				break;

            // creates a DateTime object from datetimepicker
            case 'datetime':
                if ($val) {
                    if ((defined('PAIR_FORM_DATETIME_FORMAT') and self::usingCustomDatetimepicker())) {
                        $format = PAIR_FORM_DATETIME_FORMAT;
                    } else {
                        $format = 'Y-m-d H:i:s';
                    }
                    $val = \DateTime::createFromFormat('!' . $format, $val, $app->currentUser->getDateTimeZone());
                    // DateTime is not set
                    if (FALSE === $val) $val = NULL;
                } else {
                    $val = NULL;
                }
                break;

		}
		
		return $val;
		
	}
454981
protected function generateDefault( $type, $value )
    {
        switch ( $type )
        {
            case 'boolean':
                return ( $value && $value != 'false' ) ? 'true' : 'false';

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

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

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

            default:
                return "'$value'";
        }
    }
779208
public static function castToString($type, $value)
    {
        if ($value === '') {
            return '';
        }

        switch ($type) {
            case self::PROPERTY_TYPE_BOOLEAN:
                return $value ? 'true' : 'false';

            case self::PROPERTY_TYPE_DATE:
                /**
                 * @var \DateTimeInterface
                 */
                return $value->format('Y-m-d');

            case self::PROPERTY_TYPE_TIMESTAMP:
                /**
                 * @var \DateTimeInterface
                 */
                return $value->format('c');

            case self::PROPERTY_TYPE_OBJECT:
                if ($value instanceof self) {
                    return $value->toStringArray();
                }

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

                return '';
        }
    }
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)
                );
        }
    }
1050059
function value($type,$val) {
		switch ($type) {
			case self::PARAM_FLOAT:
				if (!is_string($val))
					$val=str_replace(',','.',$val);
				return $val;
			case \PDO::PARAM_NULL:
				return NULL;
			case \PDO::PARAM_INT:
				return (int)$val;
			case \PDO::PARAM_BOOL:
				return (bool)$val;
			case \PDO::PARAM_STR:
				return (string)$val;
			case \PDO::PARAM_LOB:
				return (binary)$val;
		}
	}
467107
protected function _type_assignment_get( $type, $value )
	{
		switch ( $type ) 
		{
			case 'int':
			case 'timestamp':
				return (int) $value;
			break;

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

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

			// json datatype try to decode return array on failure
			case 'json':
				if ( is_array( $value ) )
				{
					return $value;
				}
				if ( is_array( $value = json_decode( $value, true ) ) )
				{
					return $value;
				}
				return array();
			break;
		}

		return $value;
	}
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;
    }
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;
    }
1104262
public function validate(string $type, $value): bool
    {
        switch ($type) {
            case self::TYPE_NULL:
                return is_null($value);
            case self::TYPE_BOOLEAN:
                return is_bool($value);
            case self::TYPE_INTEGER:
                return is_integer($value);
            case self::TYPE_FLOAT:
                return is_float($value);
            case self::TYPE_STRING:
                return is_string($value);
            case self::TYPE_ARRAY:
                if (!is_array($value)) {
                    return false;
                }
                return $this->isArrayType($value);
            case self::TYPE_OBJECT:
                if (!is_array($value)) {
                    return false;
                }
                return $this->isObjectType($value);
            case self::TYPE_BINARY:
                return is_string($value);
        }

        throw new InvalidValueException("Invalid value type: $type");
    }
552033
protected function typecast($value)
    {
        if ($value === '' && $this->type !== Schema::TYPE_STRING) {
            return null;
        }
        if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
            return $value;
        }
        switch ($this->phpType) {
            case 'resource':
            case 'string':
                return is_resource($value) ? $value : (string) $value;
            case 'int':
            case 'integer':
                return (int) $value;
            case 'bool':
            case 'boolean':
                return (bool) $value;
            case 'double':
                return (double) $value;
        }

        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;
    }
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);
        }
    }
415594
private function convertValue($value, $type){

        switch($type){
            case "is_numeric":
                $value = floatval($value);
                break;
            case "is_float":
                $value = floatval($value);
                break;
            case "is_string":
                $value = strval($value);
                break;
            default:
                $value = strval($value);
        }

        return $value;
    }
787295
protected function convert ($type, $value)
		{
			switch ($type)
			{
				case 'string':
					return (string) $value;

				case 'number':
					return is_float($value)
						? (float) $value
						: (int) $value;

				case 'boolean':
					return $value === true || $value === 'true' || $value === '1' || $value === 'on';

				default:
					if (is_float($value))
					{
						return $this->convert('number', $value);
					}
					if (is_numeric($value))
					{
						return strpos($value, '.') !== FALSE
							? (float) $value
							: (int) $value;
					}
					if ($value === 'true' || $value === 'false')
					{
						return $value === 'true';
					}
					if ($value === '')
					{
						return null;
					}
			}
			return $value;
		}
843711
private static function isSimpleType($value, string $type): ?bool
    {
        switch ($type) {
            case 'array':
                return static::isArray($value);
                break;
            case 'object':
                return static::isObject($value);
                break;
            case 'bool':
                return static::isBool($value);
                break;
            case 'int':
                return static::isInt($value);
                break;
            case 'float':
                return static::isFloat($value);
                break;
            case 'string':
                return static::isString($value);
                break;
            case 'callable':
                return static::isCallable($value);
                break;
            default:
                break;
        }

        return null;
    }
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;
        }
    }