IntMapper
Maps an input value to an int (or null).
Accepts the following input types:
int.string(orStringable) representing an integer — coerced toint.null— left asnull, subject to validation.
Non-null values are coerced to int before validation.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
| $min | int? | -100000 | Rejects integers below this. |
| $max | int? | 100000 | Rejects 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