CRUDlex\Entity::toType PHP Method

toType() protected method

Converts a given value to the given type.
protected toType ( mixed $value, string $type ) : mixed
$value mixed the value to convert
$type string the type to convert to like 'integer' or 'float'
return mixed the converted value
    protected function toType($value, $type)
    {
        if (in_array($type, ['integer', 'float']) && $value !== '' && $value !== null) {
            settype($value, $type);
        } else {
            if ($type == 'boolean') {
                $value = (bool) $value;
            } else {
                if ($type == 'many') {
                    $value = $value ?: [];
                } else {
                    if (in_array($type, ['datetime', 'date', 'reference'])) {
                        $value = $value === '' ? null : $value;
                    }
                }
            }
        }
        return $value;
    }