Skip to main content

TypeMapper

Passes an input value through a TypeConstraint, writing the coerced result back. It performs no validation — if the value does not satisfy the constraint, a BindMismatchException is thrown rather than a validation error.

Mappers::type() applies the constraint to every value (the constraint itself may allow null). Mappers::typeNotNull() is a shortcut that leaves null values untouched instead of passing them through the constraint.

Example: array-of-int constraint​

$result = Bind::attrs(['userIds' => [1, 2, 3]])
->props(['userIds'], Mappers::type(TypeConstraints::array(false, 'int')))
->toArray()
->exec();
var_dump($result->get());

Output:

array(1) {
["userIds"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}

A value that violates the constraint — e.g. ['userIds' => [1, 2, [3, 4]]] where a nested array is not an int — throws BindMismatchException instead of producing a validation error.

Example: typeNotNull leaves null untouched​

$result = Bind::values(null)->map(Mappers::typeNotNull(TypeConstraints::int()))->toArray()->exec();
var_dump($result->get());

Output:

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

Mappers::type() would instead pass null through the int constraint (which rejects it); typeNotNull() skips the constraint entirely for null.