typing
class typing
| internal |
Central utility for type normalization and casting.
Prevents values from core or database (usually strings) from reaching higher layers without proper typing (int/bool/float), ensuring adherence to service contracts.
Methods
Converts any scalar value to a nullable integer.
Converts a value to a positive integer or null if zero or negative.
Performs strict boolean normalization from various scalar representations.
Performs strict string normalization.
Performs strict float normalization.
Converts to nullable integer ID.
Converts to integer ID or false.
Normalizes fields within a Moodle record (stdClass or array) based on a specification.
Semantic alias for to_int() used during record normalization.
Casts all elements of an array to integers (or null).
Casts all elements of an array to strings.
Checks whether a string contains only digits.
Details
at line 54
static int|null
to_int(mixed $value)
Converts any scalar value to a nullable integer.
Expected behavior:
- null → null (not 0)
- "" → null
- true → 1
- false → 0
- "10" → 10
- "10abc" → exception
at line 80
static int|null
to_positive_int(mixed $value)
Converts a value to a positive integer or null if zero or negative.
at line 108
static bool
to_bool(mixed $value)
Performs strict boolean normalization from various scalar representations.
Accepted true values:
- true, 1, "1", "true", "yes", "on"
Accepted false values:
- false, 0, "0", "false", "no", "off", ""
Anything else throws exception.
at line 162
static string
to_string(mixed $value)
Performs strict string normalization.
Behavior:
- null => ""
- scalar => (string) cast
- Stringable object => __toString
- other => throws coding_exception
at line 194
static float|null
to_float(mixed $value)
Performs strict float normalization.
at line 223
static int|null
normalize_id(mixed $value)
Converts to nullable integer ID.
Returns null for any non-positive value.
at line 238
static int
normalize_id_or_zero(mixed $value)
Converts to integer ID or false.
Useful for compatibility with Moodle APIs that use false instead of null.
at line 259
static stdClass
normalize_record(array|stdClass $record, array $spec)
Normalizes fields within a Moodle record (stdClass or array) based on a specification.
at line 293
static int|null
normalize(mixed $value)
Semantic alias for to_int() used during record normalization.
at line 311
static array
cast_array_of_ints(array $array)
Casts all elements of an array to integers (or null).
at line 323
static array
cast_array_of_strings(array $array)
Casts all elements of an array to strings.
at line 341
static bool
is_numeric_string(mixed $value)
Checks whether a string contains only digits.