FloatMapper
Maps an input value to a float (or null).
Accepts the following input types:
intorfloat.string(orStringable) representing a number — coerced tofloat.null— left asnull, subject to validation.
Non-null values are coerced to float before validation.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
| $min | float? | -100000 | Rejects numbers below this. |
| $max | float? | 100000 | Rejects numbers above this. |
| $step | float? | 0.01 | Rejects numbers not on this step grid (relative to $min). |
When multiple rules are violated, only the first is reported per value, in the order mandatory → min → max → step.
Example: basic coercion
$result = Bind::values('20', null)->map(Mappers::float(min: 0, max: 100, step: 10))->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
float(20)
[1]=>
NULL
}
Example: mandatory, range and step validation
$result = Bind::attrs(['valuenull' => null, 'valuemin' => 0, 'value1' => 20, 'value2' => 60, 'valuemax' => 100])
->props(['valuenull', 'valuemin', 'value1', 'value2', 'valuemax'],
Mappers::float(mandatory: true, min: 20, max: 80, step: 30))
->toArray()
->exec();
// result will be invalid; each value reports its first violated rule.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('valuenull')->jsonSerialize()['messages'][0]); // "Mandatory"
var_dump($result->getErrorMap()->getChild('valuemin')->jsonSerialize()['messages'][0]); // "Min [min = 20]"
var_dump($result->getErrorMap()->getChild('value1')->jsonSerialize()['messages'][0]); // "Step [step = 30]"
var_dump($result->getErrorMap()->getChild('valuemax')->jsonSerialize()['messages'][0]); // "Max [max = 80]"