Cast
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
Safely converts a value to a specified type. Supported types: bool, int, float, string, array.
Safely converts a value to a specified type or returns null if the value is null.
Safely converts a value to a boolean.
Safely converts a value to an integer.
Safely converts a value to a float.
Safely converts a value to a string.
Wraps the value in an array if it is not already one or returns empty array if the value is null.
Safely converts a value to a boolean or returns null if the value is null.
Safely converts a value to an integer or returns null if the value is null.
Safely converts a value to a float or returns null if the value is null.
Safely converts a value to a string or returns null if the value is null.
Wraps the value in an array if it is not already one or returns null if the value is null.
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.
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.
at line 56
static bool
toBool(mixed $value)
Safely converts a value to a boolean.
at line 73
static int
toInt(mixed $value)
Safely converts a value to an integer.
at line 94
static float
toFloat(mixed $value)
Safely converts a value to a float.
at line 113
static string
toString(mixed $value)
Safely converts a value to a string.
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.
at line 143
static bool|null
toBoolOrNull(mixed $value)
Safely converts a value to a boolean or returns null if the value is null.
at line 153
static int|null
toIntOrNull(mixed $value)
Safely converts a value to an integer or returns null if the value is null.
at line 163
static float|null
toFloatOrNull(mixed $value)
Safely converts a value to a float or returns null if the value is null.
at line 173
static string|null
toStringOrNull(mixed $value)
Safely converts a value to a string or returns null if the value is null.
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.
at line 191
static mixed
falseToNull(mixed $value)
Converts false to null, does not change other values.
Traits
Static class.