Skip to content
docsv0.17.0

FieldCoercion

The single source of scalar coercion shared by SchemaFormParser (enum members, defaults) and FormBinder (input). Centralizing it here removes divergence between the two seams. The rules are frozen: integer requires /^-?\d+$/ then a verbatim (int) cast (overflow clamps silently); number uses is_numeric then (float); the empty check is always strict `=== ''` so "0" is not empty.

FieldCoercion::toInt()

public static function toInt(string $str): ?int

A normalized (trimmed, non-empty) string to an int, or null if it is not an integer literal.

Parameters

Parameters of toInt()
NameTypeDescription
$strstring

FieldCoercion::toFloat()

public static function toFloat(string $str): ?float

A normalized (trimmed, non-empty) string to a float, or null if it is not numeric.

Parameters

Parameters of toFloat()
NameTypeDescription
$strstring

FieldCoercion::toBool()

public static function toBool(string $str): ?bool

A normalized (lowercased, trimmed) string to a bool via the frozen token sets, or null.

Parameters

Parameters of toBool()
NameTypeDescription
$strstring

FieldCoercion::coerceEnumMember()

public static function coerceEnumMember(?mixed $value, Milpa\Live\Schema\FieldType $type): string|int|float|null

Coerces one enum member (or any non-boolean schema value) to the field's base type. Non-scalar or boolean members are rejected (null) BEFORE any string cast — no E_WARNING is ever emitted.

Parameters

Parameters of coerceEnumMember()
NameTypeDescription
$value?mixed
$typeMilpa\Live\Schema\FieldType

FieldCoercion::coerceDefault()

public static function coerceDefault(?mixed $value, Milpa\Live\Schema\FieldType $type): string|int|float|bool|null

Coerces a field default to the base type. Boolean accepts a native bool or a token string; the other types reuse coerceEnumMember. null means "not coercible to this type".

Parameters

Parameters of coerceDefault()
NameTypeDescription
$value?mixed
$typeMilpa\Live\Schema\FieldType