Skip to main content

IntMapper

Maps an input value to an int (or null).

Accepts the following input types:

  • int.
  • string (or Stringable) representing an integer — coerced to int.
  • null — left as null, subject to validation.

Non-null values are coerced to int before validation.

Validation possibilities​

ParameterTypeDefaultDescription
$mandatoryboolfalseRejects null input.
$minint?-100000Rejects integers below this.
$maxint?100000Rejects integers above this.

When multiple rules are violated, only the first is reported per value, in the order mandatory → min → max.

Example: basic coercion​

$result = Bind::values('42', null)->map(Mappers::int())->toArray()->exec();
var_dump($result->get());

Output:

array(2) {
[0]=>
int(42)
[1]=>
NULL
}

Example: mandatory and range validation​

$result = Bind::attrs(['age' => null, 'count' => 0, 'total' => 100])
->props(['age', 'count', 'total'], Mappers::int(true, 20, 80))
->toArray()
->exec();

// result will be invalid with error messages provided for all properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('age')->isEmpty()); // false because mandatory
var_dump($result->getErrorMap()->getChild('count')->isEmpty()); // false due to min
var_dump($result->getErrorMap()->getChild('total')->isEmpty()); // false due to max