Skip to main content

FloatMapper

Maps an input value to a float (or null).

Accepts the following input types:

  • int or float.
  • string (or Stringable) representing a number — coerced to float.
  • null — left as null, subject to validation.

Non-null values are coerced to float before validation.

Validation possibilities​

ParameterTypeDefaultDescription
$mandatoryboolfalseRejects null input.
$minfloat?-100000Rejects numbers below this.
$maxfloat?100000Rejects numbers above this.
$stepfloat?0.01Rejects 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]"