final class Cast

Provides safe, lossless type casting. Throws TypeError if conversion would result in data loss. Supports bool, int, float, string, and array types.

Methods

static mixed
to(mixed $value, string $type)

Safely converts a value to a specified type. Supported types: bool, int, float, string, array.

static mixed
toOrNull(mixed $value, string $type)

Safely converts a value to a specified type or returns null if the value is null.

static bool
toBool(mixed $value)

Safely converts a value to a boolean.

static int
toInt(mixed $value)

Safely converts a value to an integer.

static float
toFloat(mixed $value)

Safely converts a value to a float.

static string
toString(mixed $value)

Safely converts a value to a string.

static array
toArray(mixed $value)

Wraps the value in an array if it is not already one or returns empty array if the value is null.

static bool|null
toBoolOrNull(mixed $value)

Safely converts a value to a boolean or returns null if the value is null.

static int|null
toIntOrNull(mixed $value)

Safely converts a value to an integer or returns null if the value is null.

static float|null
toFloatOrNull(mixed $value)

Safely converts a value to a float or returns null if the value is null.

static string|null
toStringOrNull(mixed $value)

Safely converts a value to a string or returns null if the value is null.

static array|null
toArrayOrNull(mixed $value)

Wraps the value in an array if it is not already one or returns null if the value is null.

static mixed
falseToNull(mixed $value)

Converts false to null, does not change other values.

Details

at line 28
static mixed to(mixed $value, string $type)

Safely converts a value to a specified type. Supported types: bool, int, float, string, array.

Parameters

mixed $value
string $type

Return Value

mixed

Exceptions

TypeError

at line 46
static mixed toOrNull(mixed $value, string $type)

Safely converts a value to a specified type or returns null if the value is null.

Supported types: bool, int, float, string, array.

Parameters

mixed $value
string $type

Return Value

mixed

Exceptions

TypeError

at line 56
static bool toBool(mixed $value)

Safely converts a value to a boolean.

Parameters

mixed $value

Return Value

bool

Exceptions

TypeError

at line 73
static int toInt(mixed $value)

Safely converts a value to an integer.

Parameters

mixed $value

Return Value

int

Exceptions

TypeError

at line 94
static float toFloat(mixed $value)

Safely converts a value to a float.

Parameters

mixed $value

Return Value

float

Exceptions

TypeError

at line 113
static string toString(mixed $value)

Safely converts a value to a string.

Parameters

mixed $value

Return Value

string

Exceptions

TypeError

at line 129
static array toArray(mixed $value)

Wraps the value in an array if it is not already one or returns empty array if the value is null.

Parameters

mixed $value

Return Value

array

at line 143
static bool|null toBoolOrNull(mixed $value)

Safely converts a value to a boolean or returns null if the value is null.

Parameters

mixed $value

Return Value

bool|null

Exceptions

TypeError

at line 153
static int|null toIntOrNull(mixed $value)

Safely converts a value to an integer or returns null if the value is null.

Parameters

mixed $value

Return Value

int|null

Exceptions

TypeError

at line 163
static float|null toFloatOrNull(mixed $value)

Safely converts a value to a float or returns null if the value is null.

Parameters

mixed $value

Return Value

float|null

Exceptions

TypeError

at line 173
static string|null toStringOrNull(mixed $value)

Safely converts a value to a string or returns null if the value is null.

Parameters

mixed $value

Return Value

string|null

Exceptions

TypeError

at line 182
static array|null toArrayOrNull(mixed $value)

Wraps the value in an array if it is not already one or returns null if the value is null.

Parameters

mixed $value

Return Value

array|null

at line 191
static mixed falseToNull(mixed $value)

Converts false to null, does not change other values.

Parameters

mixed $value

Return Value

mixed

Traits

Static class.