AlterFloatPropertyTrait
Provides a standardized way to cast a property value to `float` within the AlterDocument system.
This alteration is typically declared as:
'price' => Alter::FLOAT
or inside a chained definition:
'prices' => [ Alter::ARRAY, Alter::FLOAT ]
Behavior details:
- If the input is already a
float, it is returned unchanged and$modifiedremainsfalse. - If the input is an array, each element is converted using
floatval(). - If the input is any scalar (string, int, bool), it is cast using
floatval(). - Non-scalar, non-array values should generally not occur, but if they do,
PHP's
floatval()semantics apply.
This alteration is useful for ensuring numeric consistency when working with user input, JSON data, APIs, DTO hydration, or internal normalization.
Tags
Table of Contents
Methods
- alterFloatProperty() : float|array<string|int, mixed>
- Casts a value (or every element of an array) to float.
Methods
alterFloatProperty()
Casts a value (or every element of an array) to float.
public
alterFloatProperty(mixed $value[, bool &$modified = false ]) : float|array<string|int, mixed>
This method ensures that the resulting value is always of type float
(or an array of floats). It also sets $modified to true when any
transformation occurs.
Rules:
- If
$valueis already a float → returned as is, not marked modified. - If
$valueis an array → each element converted viafloatval(). - Otherwise →
$valueis cast to float.
Parameters
- $value : mixed
-
The value to convert. Can be scalar or array.
- $modified : bool = false
-
Output flag set to
trueif a cast was performed.
Tags
Return values
float|array<string|int, mixed> —The float-cast value or an array of float-cast values.