Convenience function for automatic casting in form methods etc.

@param mixed $value
@param string $type
@return mixed Safe value for DB query, or NULL if type was not a valid one
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;
	}
107813
protected function handleTypeConversions($value, $typeOfField)
    {
        switch ($typeOfField) {
            case 'datetimetz':
            case 'datetime':
            case 'time':
            case 'date':
                if ('' === $value) {
                    return null;
                }

                if (is_int($value)) {
                    $dateTime = new DateTime();
                    $dateTime->setTimestamp($value);
                    $value = $dateTime;
                } elseif (is_string($value)) {
                    $value = new DateTime($value);
                }

                break;
            default:
        }

        return $value;
    }
309192
private function checkType($type, $value)
    {        
        switch($type)
        {
            case 'bool':
            case 'boolean':
                $codeException = (!is_bool($value)) ? 1 : 0;
                break;
            case 'int':
            case 'integer':
                $codeException = (!is_int($value)) ? 1 : 0;
                break;
            case 'string':
                $codeException = (!is_string($value)) ? 1 : 0;
                break;
            case 'not_resource':
                $codeException = (is_resource($value)) ? 2 : 0;
                break;
            default :
                $codeException = 3;
                break;
        }
        
        return $codeException;
    }
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;
	}
991584
public static function encode($value, string $type = null): string
    {
        return self::run(self::ENCODER, $value, $type);
    }
850369
public function elseValue($value = null, $type = null)
    {
        if (is_array($value)) {
            end($value);
            $value = key($value);
        }

        if ($value !== null && !$value instanceof ExpressionInterface) {
            $value = $this->_castToExpression($value, $type);
        }

        if (!$value instanceof ExpressionInterface) {
            $value = ['value' => $value, 'type' => $type];
        }

        $this->_elseValue = $value;
    }
1143296
protected function verifyCorrectType($type, $value)
    {
        // Switch various types
        switch ($type) {
            case 'string':
                if (!is_string($value)) {
                    throw new SetterGetterException('String type expected', 3);
                }
                break;

            case 'number':
                if (!is_numeric($value)) {
                    throw new SetterGetterException('Number type expected', 4);
                }
                break;

            case 'array':
                if (!is_array($value)) {
                    throw new SetterGetterException('Array type expected', 5);
                }
                break;

            case 'object':
                if (!is_object($value)) {
                    throw new SetterGetterException('Object type expected.', 6);
                }
                break;

            default:
                // If a @var type is given in annotation and we haven't received
                // proper type.
                if (!is_null($type) && !$value instanceof $type) {
                    throw new SetterGetterException('Instance of ' . $type . ' expected.', 7);
                }
                break;
        }
    }
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;
        }
    }
1187917
protected function decode($value, string $type)
    {
        try {
            return $this->encoders->getEncoderForType($type)
                ->decode($value, $type, new EncodingContext());
        } catch (EncodingException | InvalidArgumentException | OutOfBoundsException $ex) {
            throw new SerializationException('Failed to deserialize value', 0, $ex);
        }
    }
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;
		}
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;
    }
823176
public static function to_type($value, string $type='', bool $flatten=false) {
		ref\cast::to_type($value, $type, $flatten);
		return $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);
    }
26102
public function cast($value)
    {
        $fn = 'cast' . ucfirst(gettype($value));
        
        return method_exists($this, $fn) ? $this->$fn($value) : (string)$value;
    }
364639
public static function decodeFromBase64(string $value, string $type = null)
    {
        return self::run(self::DECODER, \base64_decode($value), $type);
    }
915632
public function bindValue($param, $value, $type=null)
    {
        $ok = false;
        try {
            $param = $this->_getBindVar($param);
            if (PDO::PARAM_LOB == $type) {
                $lob = \oci_new_descriptor($this->_con, \OCI_D_LOB);
                $ok  = \oci_bind_by_name($this->_stmt, $param, $lob, -1, \OCI_B_BLOB);
                $this->_bindsLob[$param] = array(
                    'lob'   => $lob,
                    'value' => $value,
                );
            } else {
                $ok = \oci_bind_by_name($this->_stmt, $param, $value);
                $this->_binds[$param] = $value;
            }
        } catch (\Exception $e) {
            throw new \PDOException($e->getMessage());
        }
        return $ok;
    }
1183444
public function normalizeValue($type, $value)
    {
        if ($value === null) {
            return null;
        }

        if ($type === 'string') {
            return is_string($value) ? $value : (string)$value;
        } elseif ($type === 'int') {
            return is_int($value) ? $value : (int)$value;
        } elseif ($type === 'float') {
            return is_float($value) ? $value : (float)$value;
        } elseif ($type === 'objectid') {
            return is_scalar($type) ? new ObjectID($value) : $value;
        } elseif ($type === 'bool') {
            return is_bool($value) ? $value : (bool)$value;
        } elseif ($type === 'array') {
            return (array)$value;
        } else {
            throw new InvalidValueException(['`:model` model is not supported `:type` type', 'model' => static::class, 'type' => $type]);
        }
    }