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

static int|null
to_int(mixed $value)

Converts any scalar value to a nullable integer.

static int|null
to_positive_int(mixed $value)

Converts a value to a positive integer or null if zero or negative.

static bool
to_bool(mixed $value)

Performs strict boolean normalization from various scalar representations.

static string
to_string(mixed $value)

Performs strict string normalization.

static float|null
to_float(mixed $value)

Performs strict float normalization.

static int|null
normalize_id(mixed $value)

Converts to nullable integer ID.

static int
normalize_id_or_zero(mixed $value)

Converts to integer ID or false.

static stdClass
normalize_record(array|stdClass $record, array $spec)

Normalizes fields within a Moodle record (stdClass or array) based on a specification.

static int|null
normalize(mixed $value)

Semantic alias for to_int() used during record normalization.

static array
cast_array_of_ints(array $array)

Casts all elements of an array to integers (or null).

static array
cast_array_of_strings(array $array)

Casts all elements of an array to strings.

static bool
is_numeric_string(mixed $value)

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

Parameters

mixed $value

value to convert

Return Value

int|null

normalized integer

at line 80
static int|null to_positive_int(mixed $value)

Converts a value to a positive integer or null if zero or negative.

Parameters

mixed $value

value to convert

Return Value

int|null

positive integer or null

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.

Parameters

mixed $value

value to convert

Return Value

bool

normalized boolean

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

Parameters

mixed $value

value to convert

Return Value

string

normalized string

at line 194
static float|null to_float(mixed $value)

Performs strict float normalization.

Parameters

mixed $value

value to convert

Return Value

float|null

normalized float

at line 223
static int|null normalize_id(mixed $value)

Converts to nullable integer ID.

Returns null for any non-positive value.

Parameters

mixed $value

ID to normalize

Return Value

int|null

positive ID or null if invalid/non-positive

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.

Parameters

mixed $value

ID to normalize

Return Value

int

positive ID or 0

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.

Parameters

array|stdClass $record

record object or associative array

array $spec

specification, e.g., ['id' => 'int', 'enabled' => 'bool'].

Return Value

stdClass

normalized record as stdClass

at line 293
static int|null normalize(mixed $value)

Semantic alias for to_int() used during record normalization.

Parameters

mixed $value

value to normalize

Return Value

int|null

normalized integer

at line 311
static array cast_array_of_ints(array $array)

Casts all elements of an array to integers (or null).

Parameters

array $array

input array

Return Value

array

array with casted integers

at line 323
static array cast_array_of_strings(array $array)

Casts all elements of an array to strings.

Parameters

array $array

input array

Return Value

array

array with string values

at line 341
static bool is_numeric_string(mixed $value)

Checks whether a string contains only digits.

Parameters

mixed $value

value to check

Return Value

bool

True if it's a numeric string